From 43d1ef1e336689e9e19ee6be154ebd8203f401ea Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Tue, 4 Oct 2022 12:11:40 +0200 Subject: [PATCH] better handling of file watchers --- src/Ation.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Ation.js b/src/Ation.js index 703e38f..90f9f9a 100644 --- a/src/Ation.js +++ b/src/Ation.js @@ -28,7 +28,7 @@ class Ation { app.whenReady().then(async () => { if (this.fileToOpen) - this.openFile(this.fileToOpen, true); + this.openFile(this.fileToOpen); protocol.registerFileProtocol("slideimg", (request, callback) => { const uri = request.url.replace(/^slideimg:\/\//, ""); const extension = path.extname(uri).replace(/^\./, ""); @@ -64,14 +64,19 @@ class Ation { const basePath = path.dirname(filePath); const data = parser(fileContents); - if (!change) + if (!change) this.currentFile = filePath; - - this.watcher = fsn.watch(filePath, () => { - this.openFile(this.currentFile, true); + if (this.watcher) + this.watcher.close(); + this.watcher = fsn.watch(filePath, (eventType, path) => { + if (path.match(/~$/)) // handling unix swap files here + setTimeout(() => this.openFile(this.currentFile, true), 1000); + else + this.openFile(this.currentFile, true); }); this.windowManager.mainWindow.send("Ation::openFile", [basePath, data]); - } catch { + } catch (error) { + console.log(error); return; } }