mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-08-15 19:00:23 +02:00
feat: use tauri next branch, fix tests, MSRV 1.65 (#354)
This commit is contained in:
@@ -4,7 +4,7 @@ Watch files and directories for changes using [notify](https://github.com/notify
|
||||
|
||||
## Install
|
||||
|
||||
_This plugin requires a Rust version of at least **1.64**_
|
||||
_This plugin requires a Rust version of at least **1.65**_
|
||||
|
||||
There are three general methods of installation that we can recommend.
|
||||
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { UnlistenFn } from "@tauri-apps/api/event";
|
||||
import { appWindow, WebviewWindow } from "tauri-plugin-window-api";
|
||||
|
||||
const w: WebviewWindow = appWindow;
|
||||
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
|
||||
|
||||
export interface WatchOptions {
|
||||
recursive?: boolean;
|
||||
@@ -42,11 +38,39 @@ async function unwatch(id: number): Promise<void> {
|
||||
await invoke("plugin:fs-watch|unwatch", { id });
|
||||
}
|
||||
|
||||
// TODO: use channel from @tauri-apps/api on v2
|
||||
class Channel<T = unknown> {
|
||||
id: number;
|
||||
// @ts-expect-error field used by the IPC serializer
|
||||
private readonly __TAURI_CHANNEL_MARKER__ = true;
|
||||
#onmessage: (response: T) => void = () => {
|
||||
// no-op
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.id = transformCallback((response: T) => {
|
||||
this.#onmessage(response);
|
||||
});
|
||||
}
|
||||
|
||||
set onmessage(handler: (response: T) => void) {
|
||||
this.#onmessage = handler;
|
||||
}
|
||||
|
||||
get onmessage(): (response: T) => void {
|
||||
return this.#onmessage;
|
||||
}
|
||||
|
||||
toJSON(): string {
|
||||
return `__CHANNEL__:${this.id}`;
|
||||
}
|
||||
}
|
||||
|
||||
export async function watch(
|
||||
paths: string | string[],
|
||||
cb: (event: DebouncedEvent) => void,
|
||||
options: DebouncedWatchOptions = {}
|
||||
): Promise<UnlistenFn> {
|
||||
): Promise<() => void> {
|
||||
const opts = {
|
||||
recursive: false,
|
||||
delayMs: 2000,
|
||||
@@ -61,22 +85,18 @@ export async function watch(
|
||||
|
||||
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
||||
|
||||
const onEvent = new Channel<DebouncedEvent>();
|
||||
onEvent.onmessage = cb;
|
||||
|
||||
await invoke("plugin:fs-watch|watch", {
|
||||
id,
|
||||
paths: watchPaths,
|
||||
options: opts,
|
||||
onEvent,
|
||||
});
|
||||
|
||||
const unlisten = await w.listen<DebouncedEvent>(
|
||||
`watcher://debounced-event/${id}`,
|
||||
(event) => {
|
||||
cb(event.payload);
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
void unwatch(id);
|
||||
unlisten();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -84,7 +104,7 @@ export async function watchImmediate(
|
||||
paths: string | string[],
|
||||
cb: (event: RawEvent) => void,
|
||||
options: WatchOptions = {}
|
||||
): Promise<UnlistenFn> {
|
||||
): Promise<() => void> {
|
||||
const opts = {
|
||||
recursive: false,
|
||||
...options,
|
||||
@@ -99,21 +119,17 @@ export async function watchImmediate(
|
||||
|
||||
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
||||
|
||||
const onEvent = new Channel<RawEvent>();
|
||||
onEvent.onmessage = cb;
|
||||
|
||||
await invoke("plugin:fs-watch|watch", {
|
||||
id,
|
||||
paths: watchPaths,
|
||||
options: opts,
|
||||
onEvent,
|
||||
});
|
||||
|
||||
const unlisten = await w.listen<RawEvent>(
|
||||
`watcher://raw-event/${id}`,
|
||||
(event) => {
|
||||
cb(event.payload);
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
void unwatch(id);
|
||||
unlisten();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
"tslib": "^2.5.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tauri-apps/api": "^1.2.0",
|
||||
"tauri-plugin-window-api": "0.0.0"
|
||||
"@tauri-apps/api": "^1.2.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use notify_debouncer_mini::{new_debouncer, DebounceEventResult, Debouncer};
|
||||
use serde::{ser::Serializer, Deserialize, Serialize};
|
||||
use tauri::{
|
||||
api::ipc::Channel,
|
||||
command,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
Manager, Runtime, State, Window,
|
||||
Manager, Runtime, State,
|
||||
};
|
||||
|
||||
use std::{
|
||||
@@ -44,25 +45,23 @@ enum WatcherKind {
|
||||
Watcher(RecommendedWatcher),
|
||||
}
|
||||
|
||||
fn watch_raw<R: Runtime>(window: Window<R>, rx: Receiver<notify::Result<Event>>, id: Id) {
|
||||
fn watch_raw<R: Runtime>(on_event: Channel<R>, rx: Receiver<notify::Result<Event>>) {
|
||||
spawn(move || {
|
||||
let event_name = format!("watcher://raw-event/{id}");
|
||||
while let Ok(event) = rx.recv() {
|
||||
if let Ok(event) = event {
|
||||
// TODO: Should errors be emitted too?
|
||||
let _ = window.emit(&event_name, event);
|
||||
let _ = on_event.send(&event);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn watch_debounced<R: Runtime>(window: Window<R>, rx: Receiver<DebounceEventResult>, id: Id) {
|
||||
fn watch_debounced<R: Runtime>(on_event: Channel<R>, rx: Receiver<DebounceEventResult>) {
|
||||
spawn(move || {
|
||||
let event_name = format!("watcher://debounced-event/{id}");
|
||||
while let Ok(event) = rx.recv() {
|
||||
if let Ok(event) = event {
|
||||
// TODO: Should errors be emitted too?
|
||||
let _ = window.emit(&event_name, event);
|
||||
let _ = on_event.send(&event);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -77,11 +76,11 @@ struct WatchOptions {
|
||||
|
||||
#[command]
|
||||
async fn watch<R: Runtime>(
|
||||
window: Window<R>,
|
||||
watchers: State<'_, WatcherCollection>,
|
||||
id: Id,
|
||||
paths: Vec<PathBuf>,
|
||||
options: WatchOptions,
|
||||
on_event: Channel<R>,
|
||||
) -> Result<()> {
|
||||
let mode = if options.recursive {
|
||||
RecursiveMode::Recursive
|
||||
@@ -96,7 +95,7 @@ async fn watch<R: Runtime>(
|
||||
for path in &paths {
|
||||
watcher.watch(path, mode)?;
|
||||
}
|
||||
watch_debounced(window, rx, id);
|
||||
watch_debounced(on_event, rx);
|
||||
WatcherKind::Debouncer(debouncer)
|
||||
} else {
|
||||
let (tx, rx) = channel();
|
||||
@@ -104,7 +103,7 @@ async fn watch<R: Runtime>(
|
||||
for path in &paths {
|
||||
watcher.watch(path, mode)?;
|
||||
}
|
||||
watch_raw(window, rx, id);
|
||||
watch_raw(on_event, rx);
|
||||
WatcherKind::Watcher(watcher)
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user