feat: restore dbs for native file stores

This commit is contained in:
Tienson Qin
2020-11-24 00:05:13 +08:00
parent 0793f991dc
commit 47d2ca04e9
5 changed files with 46 additions and 18 deletions

View File

@@ -270,6 +270,7 @@
#{"now" "later" "todo" "doing" "done" "wait" "waiting"
"canceled" "cancelled" "started" "in-progress"})
(defonce idb-db-prefix "logseq-db/")
(defonce local-db-prefix "logseq_local_")
(defonce local-handle-prefix (str "handle/" local-db-prefix))

View File

@@ -37,7 +37,7 @@
(defn datascript-db
[repo]
(when repo
(str "logseq-db/" (get-repo-path repo))))
(str config/idb-db-prefix (get-repo-path repo))))
(defn datascript-files-db
[repo]

View File

@@ -15,7 +15,8 @@
[frontend.handler.web.nfs :as nfs]
[frontend.ui :as ui]
[goog.object :as gobj]
[frontend.helper :as helper]))
[frontend.helper :as helper]
[frontend.idb :as idb]))
(defn- watch-for-date!
[]
@@ -31,11 +32,8 @@
(defn restore-and-setup!
[me repos logged?]
;; wait until pfs is loaded
(let [pfs-loaded? (atom js/window.pfs)
interval (atom nil)
db-schema-changed-handler (if (state/logged?)
repo-handler/rebuild-index!
(fn [_] nil))
(let [interval (atom nil)
db-schema-changed-handler repo-handler/rebuild-index!
inner-fn (fn []
(when (and @interval js/window.pfs)
(js/clearInterval @interval)
@@ -47,9 +45,14 @@
db-schema-changed-handler))
(p/then
(fn []
(if (and (not logged?)
(not (seq (db/get-files config/local-repo))))
(cond
(and (not logged?)
(not (seq (db/get-files config/local-repo)))
;; Not native local directory
(not (some config/local-db? (map :url repos))))
(repo-handler/setup-local-repo-if-not-exists!)
:else
(state/set-db-restoring! false))
(page-handler/init-commands!)
(if (seq (:repos me))
@@ -119,10 +122,7 @@
(defn start!
[render]
(let [me (and js/window.user (bean/->clj js/window.user))
logged? (:name me)
repos (if logged?
(:repos me)
[{:url config/local-repo}])]
logged? (:name me)]
(when me (state/set-state! :me me))
(state/set-db-restoring! true)
(render)
@@ -134,8 +134,21 @@
(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)))
(nfs/trigger-check!)
(restore-and-setup! me repos logged?)
;; (nfs/trigger-check!)
(p/let [nfs-dbs (idb/get-nfs-dbs)
nfs-dbs (map (fn [db] {:url db}) nfs-dbs)]
(let [repos (cond
logged?
(concat
(:repos me)
nfs-dbs)
(seq nfs-dbs)
nfs-dbs
:else
[{:url config/local-repo}])]
(restore-and-setup! me repos logged?)))
(periodically-persist-repo-to-indexeddb!)

View File

@@ -36,8 +36,10 @@
files (doall
(map (fn [file]
(let [handle (gobj/get file "handle")
get-attr #(gobj/get file %)]
{:file/path (get-attr "webkitRelativePath")
get-attr #(gobj/get file %)
path (-> (get-attr "webkitRelativePath")
(string/replace-first (str dir-name "/") ""))]
{:file/path path
:file/last-modified-at (get-attr "lastModified")
:file/size (get-attr "size")
:file/type (get-attr "type")

View File

@@ -2,7 +2,9 @@
(:require ["localforage" :as localforage]
[cljs-bean.core :as bean]
[goog.object :as gobj]
[promesa.core :as p]))
[promesa.core :as p]
[clojure.string :as string]
[frontend.config :as config]))
;; offline db
(def store-name "dbs")
@@ -32,3 +34,13 @@
(defn get-item
[key]
(.getItem localforage-instance key))
(defn get-keys
[]
(.keys localforage-instance))
(defn get-nfs-dbs
[]
(p/let [ks (get-keys)]
(->> (filter (fn [k] (string/starts-with? k (str config/idb-db-prefix config/local-db-prefix))) ks)
(map #(string/replace-first % config/idb-db-prefix "")))))