refactor(fs-watch): make options arg optional, closes #3

pull/334/head
FabianLars 2 years ago
parent f0fee4f132
commit 97ae1515fb
No known key found for this signature in database
GPG Key ID: 3B12BC1DEBF61125

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

Loading…
Cancel
Save