|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|