Combine `watch` and `watchImmediate` internally

pull/2613/head
Tony 3 months ago
parent cd8691831b
commit 8a2be0c7a0
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

File diff suppressed because one or more lines are too long

@ -1261,23 +1261,11 @@ class Watcher extends Resource {
}
}
// TODO: Return `Watcher` instead in v3
/**
* Watch changes (after a delay) on files or directories.
*
* @since 2.0.0
*/
async function watch(
async function watchInternal(
paths: string | string[] | URL | URL[],
cb: (event: WatchEvent) => void,
options?: DebouncedWatchOptions
options: DebouncedWatchOptions
): Promise<UnwatchFn> {
const opts = {
recursive: false,
delayMs: 2000,
...options
}
const watchPaths = Array.isArray(paths) ? paths : [paths]
for (const path of watchPaths) {
@ -1291,7 +1279,7 @@ async function watch(
const rid: number = await invoke('plugin:fs|watch', {
paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
options: opts,
options,
onEvent
})
@ -1300,6 +1288,23 @@ async function watch(
return watcher.unwatch
}
// TODO: Return `Watcher` instead in v3
/**
* Watch changes (after a delay) on files or directories.
*
* @since 2.0.0
*/
async function watch(
paths: string | string[] | URL | URL[],
cb: (event: WatchEvent) => void,
options?: DebouncedWatchOptions
): Promise<UnwatchFn> {
return await watchInternal(paths, cb, {
delayMs: 2000,
...options
})
}
// TODO: Return `Watcher` instead in v3
/**
* Watch changes on files or directories.
@ -1311,32 +1316,9 @@ async function watchImmediate(
cb: (event: WatchEvent) => void,
options?: WatchOptions
): Promise<UnwatchFn> {
const opts = {
recursive: false,
...options,
delayMs: null
}
const watchPaths = Array.isArray(paths) ? paths : [paths]
for (const path of watchPaths) {
if (path instanceof URL && path.protocol !== 'file:') {
throw new TypeError('Must be a file URL.')
}
}
const onEvent = new Channel<WatchEvent>()
onEvent.onmessage = cb
const rid: number = await invoke('plugin:fs|watch', {
paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
options: opts,
onEvent
return await watchInternal(paths, cb, {
...options
})
const watcher = new Watcher(rid)
return watcher.unwatch
}
/**

@ -31,6 +31,7 @@ impl Resource for WatcherKind {}
#[serde(rename_all = "camelCase")]
pub struct WatchOptions {
base_dir: Option<BaseDirectory>,
#[serde(default)]
recursive: bool,
delay_ms: Option<u64>,
}

Loading…
Cancel
Save