1
0
Эх сурвалжийг харах

fix: don't use opfs for publishing graphs

Tienson Qin 1 жил өмнө
parent
commit
74325f24e1

+ 27 - 10
src/main/frontend/db_worker.cljs

@@ -29,6 +29,7 @@
 (defonce *sqlite-conns worker-state/*sqlite-conns)
 (defonce *datascript-conns worker-state/*datascript-conns)
 (defonce *opfs-pools worker-state/*opfs-pools)
+(defonce *publishing? (atom false))
 
 (defn- get-pool-name
   [graph-name]
@@ -36,16 +37,21 @@
 
 (defn- <get-opfs-pool
   [graph]
-  (or (worker-state/get-opfs-pool graph)
-      (p/let [^js pool (.installOpfsSAHPoolVfs @*sqlite #js {:name (get-pool-name graph)
-                                                             :initialCapacity 20})]
-        (swap! *opfs-pools assoc graph pool)
-        pool)))
+  (when-not @*publishing?
+    (or (worker-state/get-opfs-pool graph)
+        (p/let [^js pool (.installOpfsSAHPoolVfs @*sqlite #js {:name (get-pool-name graph)
+                                                               :initialCapacity 20})]
+          (swap! *opfs-pools assoc graph pool)
+          pool))))
 
 (defn- init-sqlite-module!
   []
   (when-not @*sqlite
-    (p/let [electron? (string/includes? (.. js/location -href) "electron=true")
+    (p/let [href (.. js/location -href)
+            electron? (string/includes? href "electron=true")
+            publishing? (string/includes? href "publishing=true")
+
+            _ (reset! *publishing? publishing?)
             base-url (str js/self.location.protocol "//" js/self.location.host)
             sqlite-wasm-url (if electron?
                               (js/URL. "sqlite3.wasm" (.. js/location -href))
@@ -131,15 +137,26 @@
   (let [{:keys [db search]} (@*sqlite-conns repo)]
     (close-db-aux! repo db search)))
 
-(defn- create-or-open-db!
+(defn- get-db-and-search-db
   [repo]
-  (when-not (worker-state/get-sqlite-conn repo)
+  (if @*publishing?
+    (p/let [^object DB (.-DB ^object (.-oo1 ^object @*sqlite))
+            db (new DB "/db.sqlite" "ct")
+            search-db (new DB "/search-db.sqlite" "ct")]
+      [db search-db])
     (p/let [^js pool (<get-opfs-pool repo)
             capacity (.getCapacity pool)
             _ (when (zero? capacity)   ; file handle already releases since pool will be initialized only once
                 (.acquireAccessHandles pool))
             db (new (.-OpfsSAHPoolDb pool) repo-path)
-            search-db (new (.-OpfsSAHPoolDb pool) (str "search" repo-path))
+            search-db (new (.-OpfsSAHPoolDb pool) (str "search" repo-path))]
+      [db search-db])))
+
+(defn- create-or-open-db!
+  [repo]
+  (when-not (worker-state/get-sqlite-conn repo)
+    (p/let [[db search-db] (get-db-and-search-db repo)
+            _ (prn :debug :db db :search-db search-db)
             storage (new-sqlite-storage repo {})]
       (swap! *sqlite-conns assoc repo {:db db
                                        :search search-db})
@@ -322,7 +339,7 @@
    [_this repo]
    (<export-db-file repo))
 
-  (importDb
+  ( importDb
    [this repo data]
    (when-not (string/blank? repo)
      (p/let [pool (<get-opfs-pool repo)]

+ 2 - 2
src/main/frontend/persist_db/browser.cljs

@@ -65,8 +65,8 @@
   (when-not util/node-test?
     (let [worker-url (if (util/electron?)
                        "js/db-worker.js"
-                       "/static/js/db-worker.js")
-          worker (js/Worker. (str worker-url "?electron=" (util/electron?)))
+                       "static/js/db-worker.js")
+          worker (js/Worker. (str worker-url "?electron=" (util/electron?) "&publishing=" config/publishing?))
           wrapped-worker (Comlink/wrap worker)]
       (worker-handler/handle-message! worker wrapped-worker)
       (reset! *worker wrapped-worker)

+ 5 - 6
src/main/frontend/publishing.cljs

@@ -60,15 +60,11 @@
     (let [repo (-> @state/state :config keys first)]
       (state/set-current-repo! repo)
       (p/do!
-       (p/let [cached-data-length (storage/get :db-cached-str-length)]
-         (when (nil? cached-data-length)
-           (storage/set :db-cached-str-length (count data)))
-         (when (and cached-data-length (not= cached-data-length (count data)))
-           ;; delete old graph after re-published
-           (db-persist/delete-graph! repo)))
 
        (repo-handler/restore-and-setup-repo! repo)
 
+       (state/set-db-restoring! true)
+
        (when-not (db/entity :db/transacted?)
          (let [data (unescape-html data)
                db (db/string->db data)
@@ -78,6 +74,9 @@
                                {:db/ident :db/transacted? :db/transacted? true})
                          {:init-db? true
                           :new-graph? true})))
+
+       (state/set-db-restoring! false)
+
        (ui-handler/re-render-root!)))))
 
 (defn restore-state!