Skip to content

Commit

Permalink
fix: load global config synchronously instead of relying on watcher
Browse files Browse the repository at this point in the history
This commit also refreshs the shortcuts when config files has been edited.
  • Loading branch information
tiensonqin authored and andelf committed Sep 8, 2022
1 parent 7ade955 commit 7fa9209
Showing 6 changed files with 103 additions and 103 deletions.
9 changes: 3 additions & 6 deletions src/main/frontend/db.cljs
Original file line number Diff line number Diff line change
@@ -179,12 +179,9 @@
(restore-graph-from-text! repo stored)))

(defn restore!
[{:keys [repos]} _old-db-schema restore-config-handler]
(let [repo (or (state/get-current-repo) (:url (first repos)))]
(when repo
(p/let [_ (restore-graph! repo)]
(restore-config-handler repo)
(listen-and-persist! repo)))))
[repo]
(p/let [_ (restore-graph! repo)]
(listen-and-persist! repo)))

(defn run-batch-txs!
[]
158 changes: 79 additions & 79 deletions src/main/frontend/handler.cljs
Original file line number Diff line number Diff line change
@@ -81,47 +81,48 @@
:db/version db-schema/version)))

(defn restore-and-setup!
[repos old-db-schema]
(-> (db/restore!
{:repos repos}
old-db-schema
(fn [repo]
(repo-config-handler/start {:repo repo})
(when (config/global-config-enabled?)
(global-config-handler/start {:repo repo}))))
(p/then
(fn []
;; try to load custom css only for current repo
(ui-handler/add-style-if-exists!)

;; install after config is restored
(shortcut/unlisten-all)
(shortcut/refresh!)

(cond
(and (not (seq (db/get-files config/local-repo)))
;; Not native local directory
(not (some config/local-db? (map :url repos)))
(not (mobile-util/native-platform?)))
;; will execute `(state/set-db-restoring! false)` inside
(repo-handler/setup-local-repo-if-not-exists!)

:else
(state/set-db-restoring! false))))
(p/then
(fn []
(js/console.log "db restored, setting up repo hooks")
(store-schema!)

(state/pub-event! [:modal/nfs-ask-permission])

(page-handler/init-commands!)

(watch-for-date!)
(file-handler/watch-for-current-graph-dir!)
(state/pub-event! [:graph/restored (state/get-current-repo)])))
(p/catch (fn [error]
(log/error :exception error)))))
[repos]
(when-let [repo (or (state/get-current-repo) (:url (first repos)))]
(-> (db/restore! repo)
(p/then
(fn []
;; try to load custom css only for current repo
(ui-handler/add-style-if-exists!)

(->
(p/let [_ (repo-config-handler/start {:repo repo})
_ (when (config/global-config-enabled?)
(global-config-handler/start {:repo repo}))])
(p/finally
(fn []
;; install after config is restored
(shortcut/unlisten-all)
(shortcut/refresh!)

(cond
(and (not (seq (db/get-files config/local-repo)))
;; Not native local directory
(not (some config/local-db? (map :url repos)))
(not (mobile-util/native-platform?)))
;; will execute `(state/set-db-restoring! false)` inside
(repo-handler/setup-local-repo-if-not-exists!)

:else
(state/set-db-restoring! false)))))))
(p/then
(fn []
(js/console.log "db restored, setting up repo hooks")
(store-schema!)

(state/pub-event! [:modal/nfs-ask-permission])

(page-handler/init-commands!)

(watch-for-date!)
(file-handler/watch-for-current-graph-dir!)
(state/pub-event! [:graph/restored (state/get-current-repo)])))
(p/catch (fn [error]
(log/error :exception error))))))

(defn- handle-connection-change
[e]
@@ -197,44 +198,43 @@
(defn start!
[render]
(set-global-error-notification!)
(let [db-schema (storage/get :db-schema)]
(register-components-fns!)
(state/set-db-restoring! true)
(render)
(i18n/start)
(instrument/init)
(set-network-watcher!)

(util/indexeddb-check?
(fn [_error]
(notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
(state/set-indexedb-support! false)))

(react/run-custom-queries-when-idle!)

(events/run!)

(-> (p/let [repos (get-repos)]
(state/set-repos! repos)
(restore-and-setup! repos db-schema))
(p/catch (fn [e]
(js/console.error "Error while restoring repos: " e)))
(p/finally (fn []
(state/set-db-restoring! false))))
(when (mobile-util/native-platform?)
(p/do! (mobile-util/hide-splash)))

(db/run-batch-txs!)
(file/<ratelimit-file-writes!)

(when config/dev?
(enable-datalog-console))
(when (util/electron?)
(el/listen!))
(persist-var/load-vars)
(user-handler/restore-tokens-from-localstorage)
(user-handler/refresh-tokens-loop)
(js/setTimeout instrument! (* 60 1000))))
(register-components-fns!)
(state/set-db-restoring! true)
(render)
(i18n/start)
(instrument/init)
(set-network-watcher!)

(util/indexeddb-check?
(fn [_error]
(notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
(state/set-indexedb-support! false)))

(react/run-custom-queries-when-idle!)

(events/run!)

(-> (p/let [repos (get-repos)]
(state/set-repos! repos)
(restore-and-setup! repos))
(p/catch (fn [e]
(js/console.error "Error while restoring repos: " e)))
(p/finally (fn []
(state/set-db-restoring! false))))
(when (mobile-util/native-platform?)
(p/do! (mobile-util/hide-splash)))

(db/run-batch-txs!)
(file/<ratelimit-file-writes!)

(when config/dev?
(enable-datalog-console))
(when (util/electron?)
(el/listen!))
(persist-var/load-vars)
(user-handler/restore-tokens-from-localstorage)
(user-handler/refresh-tokens-loop)
(js/setTimeout instrument! (* 60 1000)))

(defn stop! []
(prn "stop!"))
3 changes: 3 additions & 0 deletions src/main/frontend/handler/events.cljs
Original file line number Diff line number Diff line change
@@ -527,6 +527,9 @@
(defmethod handle :rebuild-slash-commands-list [[_]]
(page-handler/rebuild-slash-commands-list!))

(defmethod handle :shortcut/refresh [[_]]
(st/refresh!))

(defn- refresh-cb []
(page-handler/create-today-journal!)
(st/refresh!)
6 changes: 4 additions & 2 deletions src/main/frontend/handler/file.cljs
Original file line number Diff line number Diff line change
@@ -116,10 +116,12 @@
(fn [_]
(cond
(= path (config/get-repo-config-path repo))
(repo-config-handler/restore-repo-config! repo)
(p/let [_ (repo-config-handler/restore-repo-config! repo)]
(state/pub-event! [:shortcut/refresh]))

(= path (global-config-handler/global-config-path))
(global-config-handler/restore-global-config! repo)
(p/let [_ (global-config-handler/restore-global-config!)]
(state/pub-event! [:shortcut/refresh]))

(= path (config/get-custom-css-path repo))
(ui-handler/add-style-if-exists!))
15 changes: 6 additions & 9 deletions src/main/frontend/handler/global_config.cljs
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
[frontend.handler.common.file :as file-common-handler]
[frontend.state :as state]
[cljs.reader :as reader]
[frontend.db :as db]
[promesa.core :as p]
[shadow.resource :as rc]
[electron.ipc :as ipc]
@@ -44,15 +43,13 @@
(file-common-handler/reset-file! repo-url config-path default-content)
(set-global-config-state! default-content)))))

(defn- get-global-config-content
[repo-url]
(db/get-file repo-url (global-config-path)))

(defn restore-global-config!
"Sets global config state from db"
[repo-url]
(let [config-content (get-global-config-content repo-url)]
(set-global-config-state! config-content)))
[]
(let [config-dir (global-config-dir)
config-path (global-config-path)]
(p/let [config-content (fs/read-file config-dir config-path)]
(set-global-config-state! config-content))))

(defn- watch-dir!
"Watches global config dir for given repo/db"
@@ -78,6 +75,6 @@
[{:keys [repo]}]
(p/let [root-dir' (ipc/ipc "getLogseqDotDirRoot")
_ (reset! root-dir root-dir')
_ (restore-global-config! repo)
_ (restore-global-config!)
_ (create-global-config-file-if-not-exists repo)
_ (watch-dir! repo)]))
15 changes: 8 additions & 7 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
@@ -379,13 +379,14 @@
conn, or replace the conn in state with a new one."
[repo]
(p/let [_ (state/set-db-restoring! true)
_ (db/restore-graph! repo)]
(repo-config-handler/restore-repo-config! repo)
(global-config-handler/restore-global-config! repo)
;; Don't have to unlisten the old listerner, as it will be destroyed with the conn
(db/listen-and-persist! repo)
(ui-handler/add-style-if-exists!)
(state/set-db-restoring! false)))
_ (db/restore-graph! repo)
_ (repo-config-handler/restore-repo-config! repo)
_ (global-config-handler/restore-global-config!)]
;; Don't have to unlisten the old listerner, as it will be destroyed with the conn
(db/listen-and-persist! repo)
(state/pub-event! [:shortcut/refresh])
(ui-handler/add-style-if-exists!)
(state/set-db-restoring! false)))

(defn rebuild-index!
[url]

0 comments on commit 7fa9209

Please sign in to comment.