From 992914dc2fefbacef11dcc3312b98ed6ec0a4b8e Mon Sep 17 00:00:00 2001 From: Andelf Date: Fri, 9 Dec 2022 21:57:13 +0800 Subject: [PATCH] fix(mobile): skip initial fs watcher notification --- .../main/java/com/logseq/app/FsWatcher.java | 13 +++++++-- ios/App/App/FsWatcher.swift | 19 +++++++++---- src/main/frontend/fs/watcher_handler.cljs | 28 +++++++++---------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/android/app/src/main/java/com/logseq/app/FsWatcher.java b/android/app/src/main/java/com/logseq/app/FsWatcher.java index 839e6138837..5bf70be0cd5 100644 --- a/android/app/src/main/java/com/logseq/app/FsWatcher.java +++ b/android/app/src/main/java/com/logseq/app/FsWatcher.java @@ -201,9 +201,11 @@ public PollingFsWatcher(String path) { @Override public void run() { + this.tick(false); // skip initial notification + while (!Thread.currentThread().isInterrupted()) { try { - this.tick(); + this.tick(true); Thread.sleep(2000); // The same as iOS fswatcher, 2s interval } catch (InterruptedException e) { // e.printStackTrace(); @@ -214,7 +216,7 @@ public void run() { } - private void tick() { + private void tick(boolean shouldNotify) { Map newMetaDb = new HashMap(); Stack paths = new Stack(); @@ -241,7 +243,12 @@ private void tick() { } } } - this.updateMetaDb(newMetaDb); + + if (shouldNotify) { + this.updateMetaDb(newMetaDb); + } else { + this.metaDb = newMetaDb; + } } private void updateMetaDb(Map newMetaDb) { diff --git a/ios/App/App/FsWatcher.swift b/ios/App/App/FsWatcher.swift index f94917a9f40..e50bddad6f9 100644 --- a/ios/App/App/FsWatcher.swift +++ b/ios/App/App/FsWatcher.swift @@ -28,6 +28,7 @@ public class FsWatcher: CAPPlugin, PollingWatcherDelegate { self.baseUrl = url self.watcher = PollingWatcher(at: url) self.watcher?.delegate = self + self.watcher?.start() call.resolve(["ok": true]) @@ -167,15 +168,19 @@ public class PollingWatcher { public init?(at: URL) { url = at - + } + + public func start() { + + self.tick(notify: false) + let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".timer") timer = DispatchSource.makeTimerSource(queue: queue) timer!.setEventHandler(qos: .background, flags: []) { [weak self] in - self?.tick() + self?.tick(notify: true) } timer!.schedule(deadline: .now()) timer!.resume() - } deinit { @@ -187,7 +192,7 @@ public class PollingWatcher { timer = nil } - private func tick() { + private func tick(notify: Bool) { // let startTime = DispatchTime.now() if let enumerator = FileManager.default.enumerator( @@ -223,7 +228,11 @@ public class PollingWatcher { } } - self.updateMetaDb(with: newMetaDb) + if notify { + self.updateMetaDb(with: newMetaDb) + } else { + self.metaDb = newMetaDb + } } // let elapsedNanoseconds = DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds diff --git a/src/main/frontend/fs/watcher_handler.cljs b/src/main/frontend/fs/watcher_handler.cljs index c7d4c970b48..7e3a04bd58c 100644 --- a/src/main/frontend/fs/watcher_handler.cljs +++ b/src/main/frontend/fs/watcher_handler.cljs @@ -122,18 +122,18 @@ (let [dir (config/get-repo-dir graph)] (p/let [files (fs/readdir dir :path-only? true)] (doseq [file files] - (when-let [ext (util/get-file-ext file)] + (when-let [_ext (util/get-file-ext file)] (-> - (when-not (fs-util/ignored-path? dir file) - (p/let [content (fs/read-file dir file) - stat (fs/stat dir file) - type (if (db/file-exists? graph file) - "change" - "add")] - (handle-changed! type - {:dir dir - :path file - :content content - :stat stat}))) - (p/catch (fn [error] - (js/console.dir error)))))))))) + (when-not (fs-util/ignored-path? dir file) + (p/let [content (fs/read-file dir file) + stat (fs/stat dir file) + type (if (db/file-exists? graph file) + "change" + "add")] + (handle-changed! type + {:dir dir + :path file + :content content + :stat stat}))) + (p/catch (fn [error] + (js/console.dir error))))))))))