Skip to content

Commit fbf0473

Browse files
committed
Fix plugin dev rebuild signaling and reload toast timing
1 parent 876b7ef commit fbf0473

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

crates-cli/yaak-cli/src/commands/plugin.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::utils::http;
44
use keyring::Entry;
55
use rand::Rng;
66
use rolldown::{
7-
Bundler, BundlerOptions, ExperimentalOptions, InputItem, LogLevel, OutputFormat, Platform,
8-
WatchOption, Watcher,
7+
BundleEvent, Bundler, BundlerOptions, ExperimentalOptions, InputItem, LogLevel, OutputFormat,
8+
Platform, WatchOption, Watcher, WatcherEvent,
99
};
1010
use serde::Deserialize;
1111
use std::collections::HashSet;
@@ -114,12 +114,53 @@ async fn dev(args: PluginPathArg) -> CommandResult {
114114
ensure_plugin_build_inputs(&plugin_dir)?;
115115

116116
ui::info(&format!("Watching plugin {}...", plugin_dir.display()));
117-
ui::info("Press Ctrl-C to stop");
118117

119118
let bundler = Bundler::new(bundler_options(&plugin_dir, true))
120119
.map_err(|err| format!("Failed to initialize Rolldown watcher: {err}"))?;
121120
let watcher = Watcher::new(vec![Arc::new(Mutex::new(bundler))], None)
122121
.map_err(|err| format!("Failed to start Rolldown watcher: {err}"))?;
122+
let emitter = watcher.emitter();
123+
let watch_root = plugin_dir.clone();
124+
let _event_logger = tokio::spawn(async move {
125+
loop {
126+
let event = {
127+
let rx = emitter.rx.lock().await;
128+
rx.recv()
129+
};
130+
131+
let Ok(event) = event else {
132+
break;
133+
};
134+
135+
match event {
136+
WatcherEvent::Change(change) => {
137+
let changed_path = Path::new(change.path.as_str());
138+
let display_path = changed_path
139+
.strip_prefix(&watch_root)
140+
.map(|p| p.display().to_string())
141+
.unwrap_or_else(|_| {
142+
changed_path
143+
.file_name()
144+
.map(|name| name.to_string_lossy().into_owned())
145+
.unwrap_or_else(|| "unknown".to_string())
146+
});
147+
ui::info(&format!("Rebuilding plugin {display_path}"));
148+
}
149+
WatcherEvent::Event(BundleEvent::BundleEnd(_)) => {}
150+
WatcherEvent::Event(BundleEvent::Error(event)) => {
151+
if event.error.diagnostics.is_empty() {
152+
ui::error("Plugin build failed");
153+
} else {
154+
for diagnostic in event.error.diagnostics {
155+
ui::error(&diagnostic.to_string());
156+
}
157+
}
158+
}
159+
WatcherEvent::Close => break,
160+
_ => {}
161+
}
162+
}
163+
});
123164

124165
watcher.start().await;
125166
Ok(())

crates-tauri/yaak-app/src/plugin_events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async fn handle_host_plugin_request<R: Runtime>(
118118
&InternalEventPayload::ShowToastRequest(ShowToastRequest {
119119
message: format!("Reloaded plugin {}@{}", info.name, info.version),
120120
icon: Some(Icon::Info),
121-
timeout: Some(3000),
121+
timeout: Some(5000),
122122
..Default::default()
123123
}),
124124
None,

packages/plugin-runtime/src/PluginInstance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export class PluginInstance {
7676
this.#mod = {};
7777

7878
const fileChangeCallback = async () => {
79-
await this.#mod?.dispose?.();
80-
this.#importModule();
8179
const ctx = this.#newCtx(workerData.context);
8280
try {
81+
await this.#mod?.dispose?.();
82+
this.#importModule();
8383
await this.#mod?.init?.(ctx);
8484
this.#sendPayload(
8585
workerData.context,
@@ -90,7 +90,7 @@ export class PluginInstance {
9090
null,
9191
);
9292
} catch (err: unknown) {
93-
ctx.toast.show({
93+
await ctx.toast.show({
9494
message: `Failed to initialize plugin ${this.#workerData.bootRequest.dir.split('/').pop()}: ${err}`,
9595
color: 'notice',
9696
icon: 'alert_triangle',
@@ -1003,6 +1003,7 @@ function watchFile(filepath: string, cb: () => void) {
10031003
const stat = statSync(filepath, { throwIfNoEntry: false });
10041004
if (stat == null || stat.mtimeMs !== watchedFiles[filepath]?.mtimeMs) {
10051005
watchedFiles[filepath] = stat ?? null;
1006+
console.log('[plugin-runtime] watchFile triggered', filepath);
10061007
cb();
10071008
}
10081009
});

0 commit comments

Comments
 (0)