parent
b351c38e88
commit
24b09c385e
@ -1,110 +1,103 @@
|
|||||||
import { invoke } from "@tauri-apps/api/tauri";
|
import { invoke } from '@tauri-apps/api/tauri';
|
||||||
import { UnlistenFn } from "@tauri-apps/api/event";
|
import { UnlistenFn } from '@tauri-apps/api/event';
|
||||||
import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
|
import { appWindow, WebviewWindow } from '@tauri-apps/api/window';
|
||||||
|
|
||||||
const w: WebviewWindow = appWindow;
|
const w: WebviewWindow = appWindow;
|
||||||
|
|
||||||
export interface WatchOptions {
|
export interface WatchOptions {
|
||||||
recursive?: boolean;
|
recursive?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DebouncedWatchOptions extends WatchOptions {
|
export interface DebouncedWatchOptions extends WatchOptions {
|
||||||
delayMs?: number;
|
delayMs?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RawEvent {
|
export type RawEvent = {
|
||||||
path: string | null;
|
type: RawEventKind;
|
||||||
operation: number;
|
paths: string[];
|
||||||
cookie: number | null;
|
attrs: unknown;
|
||||||
}
|
};
|
||||||
|
|
||||||
export type DebouncedEvent =
|
type RawEventKind =
|
||||||
| { type: "NoticeWrite"; payload: string }
|
| 'any '
|
||||||
| { type: "NoticeRemove"; payload: string }
|
| {
|
||||||
| { type: "Create"; payload: string }
|
access?: unknown;
|
||||||
| { type: "Write"; payload: string }
|
}
|
||||||
| { type: "Chmod"; payload: string }
|
| {
|
||||||
| { type: "Remove"; payload: string }
|
create?: unknown;
|
||||||
| { type: "Rename"; payload: string }
|
}
|
||||||
| { type: "Rescan"; payload: null }
|
| {
|
||||||
| { type: "Error"; payload: { error: string; path: string | null } };
|
modify?: unknown;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
remove?: unknown;
|
||||||
|
}
|
||||||
|
| 'other';
|
||||||
|
|
||||||
|
export type DebouncedEvent = { kind: 'any'; path: string } | { kind: 'AnyContinous'; path: string };
|
||||||
|
|
||||||
async function unwatch(id: number): Promise<void> {
|
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(
|
export async function watch(paths: string | string[], options: DebouncedWatchOptions, cb: (event: DebouncedEvent) => void): Promise<UnlistenFn> {
|
||||||
paths: string | string[],
|
const opts = {
|
||||||
options: DebouncedWatchOptions,
|
recursive: false,
|
||||||
cb: (event: DebouncedEvent) => void
|
delayMs: 2000,
|
||||||
): Promise<UnlistenFn> {
|
...options,
|
||||||
const opts = {
|
};
|
||||||
recursive: false,
|
let watchPaths;
|
||||||
delayMs: 2000,
|
if (typeof paths === 'string') {
|
||||||
...options,
|
watchPaths = [paths];
|
||||||
};
|
} else {
|
||||||
let watchPaths;
|
watchPaths = paths;
|
||||||
if (typeof paths === "string") {
|
|
||||||
watchPaths = [paths];
|
|
||||||
} else {
|
|
||||||
watchPaths = paths;
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
||||||
|
|
||||||
await invoke("plugin:fs-watch|watch", {
|
|
||||||
id,
|
|
||||||
paths: watchPaths,
|
|
||||||
options: opts,
|
|
||||||
});
|
|
||||||
|
|
||||||
const unlisten = await w.listen<DebouncedEvent>(
|
|
||||||
`watcher://debounced-event/${id}`,
|
|
||||||
(event) => {
|
|
||||||
cb(event.payload);
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
return () => {
|
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
||||||
void unwatch(id);
|
|
||||||
unlisten();
|
await invoke('plugin:fs-watch|watch', {
|
||||||
};
|
id,
|
||||||
|
paths: watchPaths,
|
||||||
|
options: opts,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unlisten = await w.listen<DebouncedEvent>(`watcher://debounced-event/${id}`, (event) => {
|
||||||
|
cb(event.payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
void unwatch(id);
|
||||||
|
unlisten();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function watchImmediate(
|
export async function watchImmediate(paths: string | string[], options: WatchOptions, cb: (event: RawEvent) => void): Promise<UnlistenFn> {
|
||||||
paths: string | string[],
|
const opts = {
|
||||||
options: WatchOptions,
|
recursive: false,
|
||||||
cb: (event: RawEvent) => void
|
...options,
|
||||||
): Promise<UnlistenFn> {
|
delayMs: null,
|
||||||
const opts = {
|
};
|
||||||
recursive: false,
|
let watchPaths;
|
||||||
...options,
|
if (typeof paths === 'string') {
|
||||||
delayMs: null,
|
watchPaths = [paths];
|
||||||
};
|
} else {
|
||||||
let watchPaths;
|
watchPaths = paths;
|
||||||
if (typeof paths === "string") {
|
|
||||||
watchPaths = [paths];
|
|
||||||
} else {
|
|
||||||
watchPaths = paths;
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
||||||
|
|
||||||
await invoke("plugin:fs-watch|watch", {
|
|
||||||
id,
|
|
||||||
paths: watchPaths,
|
|
||||||
options: opts,
|
|
||||||
});
|
|
||||||
|
|
||||||
const unlisten = await w.listen<RawEvent>(
|
|
||||||
`watcher://raw-event/${id}`,
|
|
||||||
(event) => {
|
|
||||||
cb(event.payload);
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
return () => {
|
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
||||||
void unwatch(id);
|
|
||||||
unlisten();
|
await invoke('plugin:fs-watch|watch', {
|
||||||
};
|
id,
|
||||||
|
paths: watchPaths,
|
||||||
|
options: opts,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unlisten = await w.listen<RawEvent>(`watcher://raw-event/${id}`, (event) => {
|
||||||
|
cb(event.payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
void unwatch(id);
|
||||||
|
unlisten();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue