pull/71/head
FabianLars 2 years ago
parent 24b09c385e
commit ea5fb0013c
No known key found for this signature in database
GPG Key ID: 3B12BC1DEBF61125

@ -1,103 +1,119 @@
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 type RawEvent = { export type RawEvent = {
type: RawEventKind; type: RawEventKind;
paths: string[]; paths: string[];
attrs: unknown; attrs: unknown;
}; };
type RawEventKind = type RawEventKind =
| 'any ' | "any "
| { | {
access?: unknown; access?: unknown;
} }
| { | {
create?: unknown; create?: unknown;
} }
| { | {
modify?: unknown; modify?: unknown;
} }
| { | {
remove?: unknown; 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> { 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(
const opts = { paths: string | string[],
recursive: false, options: DebouncedWatchOptions,
delayMs: 2000, cb: (event: DebouncedEvent) => void
...options, ): Promise<UnlistenFn> {
}; const opts = {
let watchPaths; recursive: false,
if (typeof paths === 'string') { delayMs: 2000,
watchPaths = [paths]; ...options,
} else { };
watchPaths = paths; let watchPaths;
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);
} }
);
const id = window.crypto.getRandomValues(new Uint32Array(1))[0]; return () => {
void unwatch(id);
await invoke('plugin:fs-watch|watch', { unlisten();
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(paths: string | string[], options: WatchOptions, cb: (event: RawEvent) => void): Promise<UnlistenFn> { export async function watchImmediate(
const opts = { paths: string | string[],
recursive: false, options: WatchOptions,
...options, cb: (event: RawEvent) => void
delayMs: null, ): Promise<UnlistenFn> {
}; const opts = {
let watchPaths; recursive: false,
if (typeof paths === 'string') { ...options,
watchPaths = [paths]; delayMs: null,
} else { };
watchPaths = paths; let watchPaths;
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);
} }
);
const id = window.crypto.getRandomValues(new Uint32Array(1))[0]; return () => {
void unwatch(id);
await invoke('plugin:fs-watch|watch', { unlisten();
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…
Cancel
Save