Skip to content

Commit

Permalink
fix(mobile): skip initial fs watcher notification
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Dec 9, 2022
1 parent 158b6e0 commit 992914d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
13 changes: 10 additions & 3 deletions android/app/src/main/java/com/logseq/app/FsWatcher.java
Expand Up @@ -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();
Expand All @@ -214,7 +216,7 @@ public void run() {

}

private void tick() {
private void tick(boolean shouldNotify) {
Map<String, SimpleFileMetadata> newMetaDb = new HashMap();

Stack<String> paths = new Stack();
Expand All @@ -241,7 +243,12 @@ private void tick() {
}
}
}
this.updateMetaDb(newMetaDb);

if (shouldNotify) {
this.updateMetaDb(newMetaDb);
} else {
this.metaDb = newMetaDb;
}
}

private void updateMetaDb(Map<String, SimpleFileMetadata> newMetaDb) {
Expand Down
19 changes: 14 additions & 5 deletions ios/App/App/FsWatcher.swift
Expand Up @@ -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])

Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/main/frontend/fs/watcher_handler.cljs
Expand Up @@ -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))))))))))

0 comments on commit 992914d

Please sign in to comment.