Add helper to remove all OPFS files

This commit is contained in:
Tienson Qin
2023-12-11 16:52:21 +08:00
parent b7a7c9b549
commit df7264f016
2 changed files with 29 additions and 22 deletions

View File

@@ -27,9 +27,7 @@
(or @*opfs-pool
(p/let [^js pool (.installOpfsSAHPoolVfs @*sqlite #js {:name (str "logseq-pool-" graph)
:initialCapacity 10})]
;; (.removeVfs ^js pool)
(reset! *opfs-pool pool)
(js/console.dir pool)
pool)))
(defn- init-sqlite-module!
@@ -50,13 +48,6 @@
[repo]
(str "/" repo ".sqlite"))
(comment
(defn- get-file-names
[graph]
(p/let [^js pool (<get-opfs-pool graph)]
(when pool
(.getFileNames pool)))))
(comment
(defn- export-db-file
[repo]
@@ -106,11 +97,13 @@
[repo]
(doseq [[r db] @*sqlite-conns]
(when-not (= repo r)
(swap! *sqlite-conns dissoc r)
(.close ^Object db))))
(defn- close-db!
[repo]
(when-let [db (@*sqlite-conns repo)]
(swap! *sqlite-conns dissoc repo)
(.close ^Object db)))
(defn- create-or-open-db!
@@ -154,6 +147,29 @@
(rest dirs))]
(p/recur result dirs)))))))
(comment
(defn <remove-all-files!
"!! Dangerous: use it only for development."
[]
(p/let [all-files (<list-all-files)
files (filter #(= (.-kind %) "file") all-files)
dirs (filter #(= (.-kind %) "directory") all-files)
_ (p/all (map (fn [file] (.remove file)) files))]
(p/all (map (fn [dir] (.remove dir)) dirs)))))
(defn- <get-pool-files
[^js pool]
(.getFileNames pool))
(defn- remove-vfs!
[repo]
(p/let [^js pool (<get-opfs-pool repo)]
(when pool
(p/let [files (<get-pool-files pool)
_ (p/all (map (fn [file] (.unlink pool file)) files))
_ (.wipeFiles pool)]
(.removeVfs ^js pool)))))
#_:clj-kondo/ignore
(defclass SQLiteDB
(extends js/Object)
@@ -226,22 +242,12 @@
(unsafeUnlinkDB
[_this repo]
(p/let [_ (close-db! repo)
pool (<get-opfs-pool repo)
path (get-repo-path repo)]
(when pool
(.unlink pool path)))))
(p/let [_ (close-db! repo)]
(remove-vfs! repo)
nil)))
(defn init
"web worker entry"
[]
(let [^js obj (SQLiteDB.)]
(Comlink/expose obj)))
(comment
(defn- remove-pfs!
"!! use it only for development"
[]
(p/let [^js pool (<get-opfs-pool)]
(when pool
(.removeVfs ^js pool)))))

View File

@@ -18,6 +18,7 @@
(protocol/<unsafe-delete (get-impl) repo))
;; FIXME: limit repo name's length
;; @shuyu Do we still need this?
(defn <new [repo]
{:pre [(<= (count repo) 56)]}
(protocol/<new (get-impl) repo))