|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import { invoke } from '@tauri-apps/api/tauri';
|
|
|
|
|
import { UnlistenFn } from '@tauri-apps/api/event';
|
|
|
|
|
import { appWindow, WebviewWindow } from '@tauri-apps/api/window';
|
|
|
|
|
import { invoke } from "@tauri-apps/api/tauri";
|
|
|
|
|
import { UnlistenFn } from "@tauri-apps/api/event";
|
|
|
|
|
import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
|
|
|
|
|
|
|
|
|
|
const w: WebviewWindow = appWindow;
|
|
|
|
|
|
|
|
|
@ -19,7 +19,7 @@ export type RawEvent = {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type RawEventKind =
|
|
|
|
|
| 'any '
|
|
|
|
|
| "any "
|
|
|
|
|
| {
|
|
|
|
|
access?: unknown;
|
|
|
|
|
}
|
|
|
|
@ -32,22 +32,28 @@ type RawEventKind =
|
|
|
|
|
| {
|
|
|
|
|
remove?: unknown;
|
|
|
|
|
}
|
|
|
|
|
| 'other';
|
|
|
|
|
| "other";
|
|
|
|
|
|
|
|
|
|
export type DebouncedEvent = { kind: 'any'; path: string } | { kind: 'AnyContinous'; path: string };
|
|
|
|
|
export type DebouncedEvent =
|
|
|
|
|
| { kind: "any"; path: string }
|
|
|
|
|
| { kind: "AnyContinous"; path: string };
|
|
|
|
|
|
|
|
|
|
async function unwatch(id: number): Promise<void> {
|
|
|
|
|
await invoke('plugin:fs-watch|unwatch', { id });
|
|
|
|
|
await invoke("plugin:fs-watch|unwatch", { id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function watch(paths: string | string[], options: DebouncedWatchOptions, cb: (event: DebouncedEvent) => void): Promise<UnlistenFn> {
|
|
|
|
|
export async function watch(
|
|
|
|
|
paths: string | string[],
|
|
|
|
|
options: DebouncedWatchOptions,
|
|
|
|
|
cb: (event: DebouncedEvent) => void
|
|
|
|
|
): Promise<UnlistenFn> {
|
|
|
|
|
const opts = {
|
|
|
|
|
recursive: false,
|
|
|
|
|
delayMs: 2000,
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
let watchPaths;
|
|
|
|
|
if (typeof paths === 'string') {
|
|
|
|
|
if (typeof paths === "string") {
|
|
|
|
|
watchPaths = [paths];
|
|
|
|
|
} else {
|
|
|
|
|
watchPaths = paths;
|
|
|
|
@ -55,15 +61,18 @@ export async function watch(paths: string | string[], options: DebouncedWatchOpt
|
|
|
|
|
|
|
|
|
|
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
|
|
|
|
|
|
|
|
await invoke('plugin:fs-watch|watch', {
|
|
|
|
|
await invoke("plugin:fs-watch|watch", {
|
|
|
|
|
id,
|
|
|
|
|
paths: watchPaths,
|
|
|
|
|
options: opts,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const unlisten = await w.listen<DebouncedEvent>(`watcher://debounced-event/${id}`, (event) => {
|
|
|
|
|
const unlisten = await w.listen<DebouncedEvent>(
|
|
|
|
|
`watcher://debounced-event/${id}`,
|
|
|
|
|
(event) => {
|
|
|
|
|
cb(event.payload);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
void unwatch(id);
|
|
|
|
@ -71,14 +80,18 @@ export async function watch(paths: string | string[], options: DebouncedWatchOpt
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function watchImmediate(paths: string | string[], options: WatchOptions, cb: (event: RawEvent) => void): Promise<UnlistenFn> {
|
|
|
|
|
export async function watchImmediate(
|
|
|
|
|
paths: string | string[],
|
|
|
|
|
options: WatchOptions,
|
|
|
|
|
cb: (event: RawEvent) => void
|
|
|
|
|
): Promise<UnlistenFn> {
|
|
|
|
|
const opts = {
|
|
|
|
|
recursive: false,
|
|
|
|
|
...options,
|
|
|
|
|
delayMs: null,
|
|
|
|
|
};
|
|
|
|
|
let watchPaths;
|
|
|
|
|
if (typeof paths === 'string') {
|
|
|
|
|
if (typeof paths === "string") {
|
|
|
|
|
watchPaths = [paths];
|
|
|
|
|
} else {
|
|
|
|
|
watchPaths = paths;
|
|
|
|
@ -86,15 +99,18 @@ export async function watchImmediate(paths: string | string[], options: WatchOpt
|
|
|
|
|
|
|
|
|
|
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
|
|
|
|
|
|
|
|
await invoke('plugin:fs-watch|watch', {
|
|
|
|
|
await invoke("plugin:fs-watch|watch", {
|
|
|
|
|
id,
|
|
|
|
|
paths: watchPaths,
|
|
|
|
|
options: opts,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const unlisten = await w.listen<RawEvent>(`watcher://raw-event/${id}`, (event) => {
|
|
|
|
|
const unlisten = await w.listen<RawEvent>(
|
|
|
|
|
`watcher://raw-event/${id}`,
|
|
|
|
|
(event) => {
|
|
|
|
|
cb(event.payload);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
void unwatch(id);
|
|
|
|
|