chore: cache nfs file handles

feat/pwa
Tienson Qin 2020-11-22 01:42:27 +08:00
parent a6a675045e
commit b9cf077b72
2 changed files with 21 additions and 5 deletions

View File

@ -73,6 +73,14 @@
([dir path option] ([dir path option]
(js/window.pfs.readFile (str dir "/" path) option))) (js/window.pfs.readFile (str dir "/" path) option)))
(defonce nfs-file-handles-cache (atom {}))
(defn get-nfs-file-handle
[handle-path]
(get @nfs-file-handles-cache handle-path))
(defn add-nfs-file-handle!
[handle-path handle]
(swap! nfs-file-handles-cache assoc handle-path handle))
(defn write-file (defn write-file
[dir path content] [dir path content]
(cond (cond
@ -86,8 +94,11 @@
(subs dir 1) (subs dir 1)
(if sub-dir (if sub-dir
(str "/" sub-dir))) (str "/" sub-dir)))
basename-handle-path (str handle-path "/" basename)] basename-handle-path (str handle-path "/" basename)
(p/let [file-handle (idb/get-item basename-handle-path)] file-handle-cache (get-nfs-file-handle basename-handle-path)]
(p/let [file-handle (or file-handle-cache (idb/get-item basename-handle-path))]
(when-not file-handle-cache
(add-nfs-file-handle! basename-handle-path file-handle))
(if file-handle (if file-handle
(utils/writeFile file-handle content) (utils/writeFile file-handle content)
;; create file handle ;; create file handle

View File

@ -11,6 +11,7 @@
[frontend.state :as state] [frontend.state :as state]
[clojure.string :as string] [clojure.string :as string]
[frontend.ui :as ui] [frontend.ui :as ui]
[frontend.fs :as fs]
[frontend.config :as config])) [frontend.config :as config]))
(defn ls-dir-files (defn ls-dir-files
@ -20,7 +21,9 @@
root-handle (nth result 0) root-handle (nth result 0)
dir-name (gobj/get root-handle "name") dir-name (gobj/get root-handle "name")
repo (str config/local-db-prefix dir-name) repo (str config/local-db-prefix dir-name)
_ (idb/set-item! (str "handle-" repo) root-handle) root-handle-path (str "handle-" repo)
_ (idb/set-item! root-handle-path root-handle)
_ (fs/add-nfs-file-handle! root-handle-path root-handle)
result (nth result 1) result (nth result 1)
result (flatten (bean/->clj result)) result (flatten (bean/->clj result))
files (doall files (doall
@ -35,8 +38,10 @@
:file/handle handle})) result)) :file/handle handle})) result))
text-files (filter (fn [file] (contains? #{"org" "md" "markdown"} (util/get-file-ext (:file/path file)))) files)] text-files (filter (fn [file] (contains? #{"org" "md" "markdown"} (util/get-file-ext (:file/path file)))) files)]
(doseq [file text-files] (doseq [file text-files]
(idb/set-item! (str "handle-" repo "/" (:file/path file)) (let [handle-path (str "handle-" repo "/" (:file/path file))
(:file/handle file))) handle (:file/handle file)]
(idb/set-item! handle-path handle)
(fs/add-nfs-file-handle! handle-path handle)))
(-> (p/all (map (fn [file] (-> (p/all (map (fn [file]
(p/let [content (.text (:file/file file))] (p/let [content (.text (:file/file file))]
(assoc file :file/content content))) text-files)) (assoc file :file/content content))) text-files))