From 7907dcc2209c71a29a5943b3c5c29c6705e30abd Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 9 Sep 2024 15:44:35 -0700 Subject: [PATCH] Don't notify twice --- plugin-runtime/src/index.worker.ts | 27 ++++++++++++++++++++++++--- src-tauri/src/lib.rs | 3 ++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/plugin-runtime/src/index.worker.ts b/plugin-runtime/src/index.worker.ts index e44b9a27..6f069c17 100644 --- a/plugin-runtime/src/index.worker.ts +++ b/plugin-runtime/src/index.worker.ts @@ -14,7 +14,7 @@ import { HttpRequestActionPlugin } from '@yaakapp/api/lib/plugins/HttpRequestAct import { TemplateFunctionPlugin } from '@yaakapp/api/lib/plugins/TemplateFunctionPlugin'; import interceptStdout from 'intercept-stdout'; import * as console from 'node:console'; -import { readFileSync, watch } from 'node:fs'; +import { Stats, readFileSync, statSync, watch } from 'node:fs'; import path from 'node:path'; import * as util from 'node:util'; import { parentPort, workerData } from 'node:worker_threads'; @@ -100,8 +100,9 @@ async function initialize() { await reloadModule(); return sendPayload({ type: 'reload_response' }, null); }; - watch(path.join(pathMod), cb); - watch(path.join(pathPkg), cb); + + watchFile(pathMod, cb); + watchFile(pathPkg, cb); const ctx: Context = { clipboard: { @@ -298,3 +299,23 @@ function prefixStdout(s: string) { return newText.trimEnd(); }); } + +const watchedFiles: Record = {}; + +/** + * Watch a file and trigger callback on change. + * + * We also track the stat for each file because fs.watch will + * trigger a "change" event when the access date changes + */ +function watchFile(filepath: string, cb: (filepath: string) => void) { + watch(filepath, (_event, _name) => { + const stat = statSync(filepath); + if (stat.mtimeMs !== watchedFiles[filepath]?.mtimeMs) { + cb(filepath); + } else { + console.log('SKIPPING SAME FILE STAT', filepath, stat); + } + watchedFiles[filepath] = stat; + }); +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 69f1813a..2a6c23d4 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2104,9 +2104,10 @@ async fn handle_plugin_event( .await .unwrap(); } + let plugin_name = plugin_handle.info().await.unwrap().name; let toast_event = plugin_handle.build_event_to_send( &InternalEventPayload::ShowToastRequest(ShowToastRequest { - message: "Plugin Reloaded".to_string(), + message: format!("Reloaded plugin {}", plugin_name), variant: ToastVariant::Info, }), None,