فهرست منبع

fix: increases default write ack timeout to 10s

Also, removed idle? check when batch writing.
Removed nil marker to stop the core.async loop too.
Tienson Qin 4 سال پیش
والد
کامیت
790ba856a3

+ 44 - 1
src/main/frontend/debug.cljs

@@ -1,9 +1,52 @@
 (ns frontend.debug
   (:require [cljs.pprint :as pprint]
-            [frontend.state :as state]))
+            [frontend.state :as state]
+            [frontend.util :as util]
+            [frontend.state :as state]
+            [frontend.handler.notification :as notification]))
 
 (defn pprint
   [& xs]
   (when (state/developer-mode?)
     (doseq [x xs]
       (pprint/pprint x))))
+
+(defonce ack-wait-timeouts (atom {}))
+
+(defonce default-write-ack-timeout 10000)
+
+;; For debugging file changes are not saved on disk.
+(defn wait-for-write-ack!
+  [page-title file-path]
+  (when file-path
+    (let [requested-at (util/time-ms)]
+      (state/set-state! [:debug/write-acks file-path :last-requested-at] requested-at)
+      (when-let [timeout (get @ack-wait-timeouts file-path)]
+        (js/clearTimeout timeout))
+      (let [timeout (js/setTimeout (fn []
+                                     (let [last-ack-at (get-in @state/state [:debug/write-acks file-path :last-ack-at])]
+                                       (when-not (and last-ack-at
+                                                      (< requested-at last-ack-at (+ requested-at default-write-ack-timeout)))
+                                         (let [step (get-in @state/state [:debug/write-acks file-path :step])]
+                                           (state/pub-event! [:instrument {:type :debug/write-failed
+                                                                           :payload {:step step}}])
+                                           (notification/show!
+                                            (str "Logseq failed to save the page "
+                                                 page-title
+                                                 " to the file: "
+                                                 file-path
+                                                 ". Stop editing this page anymore, and copy all the blocks of this page to another editor to avoid any data-loss.\n"
+                                                 "Last step: "
+                                                 step)
+                                            :error)))))
+                                   default-write-ack-timeout)]
+        (swap! ack-wait-timeouts assoc file-path timeout)))))
+
+(defn ack-file-write!
+  [file-path]
+  (let [ack-at (util/time-ms)]
+    (state/set-state! [:debug/write-acks file-path :last-ack-at] ack-at)))
+
+(defn set-ack-step!
+  [file-path step]
+  (state/set-state! [:debug/write-acks file-path :step] step))

+ 4 - 3
src/main/frontend/fs/node.cljs

@@ -9,7 +9,8 @@
             [frontend.util :as util]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [frontend.debug :as debug]))
 
 (defn concat-path
   [dir path]
@@ -72,8 +73,8 @@
                  mtime (gobj/get result "mtime")]
            (prn "[DEBUG] 5. The file was saved successfully!" {:path path})
            (when (util/electron?)
-             (state/set-ack-step! path :saved-successfully)
-             (state/ack-file-write! path))
+             (debug/set-ack-step! path :saved-successfully)
+             (debug/ack-file-write! path))
            (db/set-file-last-modified-at! repo path mtime)
            (p/let [content (if (encrypt/encrypted-db? (state/get-current-repo))
                              (encrypt/decrypt content)

+ 3 - 2
src/main/frontend/modules/file/core.cljs

@@ -6,7 +6,8 @@
             [frontend.db :as db]
             [frontend.db.utils :as db-utils]
             [frontend.state :as state]
-            [frontend.util :as util]))
+            [frontend.util :as util]
+            [frontend.debug :as debug]))
 
 (defn- indented-block-content
   [content spaces-tabs]
@@ -97,7 +98,7 @@
       (async/put! chan [repo files opts])
       (prn "[DEBUG] 4. Pushed to the write channel")
       (doseq [file (map first files)]
-        (state/set-ack-step! file :pushed-to-channel))
+        (debug/set-ack-step! file :pushed-to-channel))
       (when chan-callback
         (chan-callback)))))
 

+ 4 - 4
src/main/frontend/modules/outliner/file.cljs

@@ -10,7 +10,8 @@
             [frontend.state :as state]
             [frontend.util :as util]
             [goog.object :as gobj]
-            [lambdaisland.glogi :as log]))
+            [lambdaisland.glogi :as log]
+            [frontend.debug :as debug]))
 
 (def write-chan (async/chan))
 
@@ -26,9 +27,9 @@
                    (string/blank? (:block/content (first blocks)))
                    (nil? (:block/file page-block)))
       (let [tree (tree/blocks->vec-tree blocks (:block/name page-block))]
-        (prn "[DEBUG] 3. Block tree built")
+        (prn "[DEBUG] 3. Block tree built" {:time (util/time-ms)})
         (let [path (:file/path (:block/file (db/entity page-db-id)))]
-          (state/set-ack-step! path :start-writing))
+          (debug/set-ack-step! path :start-writing))
         (file/save-tree page-block tree)))))
 
 (defn write-files!
@@ -54,5 +55,4 @@
 
 (util/batch write-chan
             batch-write-interval
-            #(state/input-idle? (state/get-current-repo))
             write-files!)

+ 6 - 4
src/main/frontend/modules/outliner/pipeline.cljs

@@ -3,7 +3,8 @@
             [frontend.modules.outliner.file :as file]
             [frontend.db :as db]
             [frontend.state :as state]
-            [frontend.util :as util]))
+            [frontend.util :as util]
+            [frontend.debug :as debug]))
 
 (defn updated-block-hook
   [block])
@@ -17,10 +18,11 @@
     (prn "[DEBUG] 2. Start writing file: "
          {:page-id (:db/id page)
           :page (:block/name page)
-          :file path})
+          :file path
+          :time (util/time-ms)})
     (when (util/electron?)
-      (state/set-ack-step! path :start-writing)
-      (state/wait-for-write-ack! page-title path)))
+      (debug/set-ack-step! path :start-writing)
+      (debug/wait-for-write-ack! page-title path)))
   (file/sync-to-file page))
 
 (defn invoke-hooks

+ 0 - 36
src/main/frontend/state.cljs

@@ -1484,39 +1484,3 @@
 
 (defn remove-watch-state [key]
   (remove-watch state key))
-
-(defonce ack-wait-timeouts (atom {}))
-
-;; For debugging file changes are not saved on disk.
-(defn wait-for-write-ack!
-  [page-title file-path]
-  (when file-path
-    (let [requested-at (util/time-ms)]
-      (set-state! [:debug/write-acks file-path :last-requested-at] requested-at)
-      (when-let [timeout (get @ack-wait-timeouts file-path)]
-        (js/clearTimeout timeout))
-      (let [timeout (js/setTimeout (fn []
-                                     (let [last-ack-at (get-in @state [:debug/write-acks file-path :last-ack-at])]
-                                       (when-not (and last-ack-at
-                                                      (< requested-at last-ack-at (+ requested-at 3000)))
-                                         (let [step (get-in @state [:debug/write-acks file-path :step])]
-                                           (pub-event! [:instrument {:type :debug/write-failed
-                                                                     :payload {:step step}}])
-                                           (js/alert (str "Logseq failed to save the page "
-                                                         page-title
-                                                         " to the file: "
-                                                         file-path
-                                                         ". Stop editing this page anymore, and copy all the blocks of this page to another editor to avoid any data-loss.\n"
-                                                         "Last step: "
-                                                         step))))))
-                                   3000)]
-        (swap! ack-wait-timeouts assoc file-path timeout)))))
-
-(defn ack-file-write!
-  [file-path]
-  (let [ack-at (util/time-ms)]
-    (set-state! [:debug/write-acks file-path :last-ack-at] ack-at)))
-
-(defn set-ack-step!
-  [file-path step]
-  (set-state! [:debug/write-acks file-path :step] step))

+ 4 - 11
src/main/frontend/util.cljc

@@ -1218,21 +1218,14 @@
 
 (defn keyname [key] (str (namespace key) "/" (name key)))
 
-(defn batch [in max-time idle? handler]
+(defn batch [in max-time handler]
   (async/go-loop [buf [] t (async/timeout max-time)]
     (let [[v p] (async/alts! [in t])]
       (cond
-        (= p t)
+        (or (= p t) (nil? v))
         (let [timeout (async/timeout max-time)]
-          (if (idle?)
-           (do
-             (handler buf)
-             (recur [] timeout))
-           (recur buf timeout)))
-
-        (nil? v)                        ; stop
-        (when (seq buf)
-          (handler buf))
+          (handler buf)
+          (recur [] timeout))
 
         :else
         (recur (conj buf v) t)))))