fix: load global config synchronously instead of relying on watcher

This commit also refreshs the shortcuts when config files has been edited.
This commit is contained in:
Tienson Qin
2022-09-06 08:46:47 +08:00
committed by Andelf
parent 7ade955df7
commit 7fa9209e29
6 changed files with 91 additions and 91 deletions

View File

@@ -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!
[]

View File

@@ -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!)
[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!)
;; install after config is restored
(shortcut/unlisten-all)
(shortcut/refresh!)
(->
(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!)
(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!)
: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])
(state/pub-event! [:modal/nfs-ask-permission])
(page-handler/init-commands!)
(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)))))
(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!)
(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)))
(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!)
(react/run-custom-queries-when-idle!)
(events/run!)
(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)))
(-> (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!)
(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))))
(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!"))

View File

@@ -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!)

View File

@@ -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!))

View File

@@ -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)]))

View File

@@ -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]