refactor(fs)!: return Watcher from watch and watchImmediate (#3610)

* refactor(fs)!: return Watcher from watch and watchImmediate

Both resolve to a `Watcher` resource instead of an `UnwatchFn` callback;
call `await watcher.close()` to stop watching. `UnwatchFn` is removed and
`Watcher` is now exported.

* fix change file

* fmt
This commit is contained in:
Lucas Fernandes Nogueira
2026-09-22 06:06:50 -03:00
committed by GitHub
parent 8083107a0a
commit fdfb35e531
4 changed files with 42 additions and 27 deletions
+10 -10
View File
@@ -17,7 +17,7 @@
let watchRecursive = $state(false)
/** @type {fs.BaseDirectory | undefined} */
let baseDir = $state()
let unwatchFn
let watcher
let unwatchPath = ''
let isMobile = $state(false)
@@ -161,15 +161,15 @@
}
if (options.delayMs === 0) {
fs.watchImmediate(watchPath, onMessage, options)
.then((fn) => {
unwatchFn = fn
.then((w) => {
watcher = w
unwatchPath = watchPath
})
.catch(onMessage)
} else {
fs.watch(watchPath, onMessage, options)
.then((fn) => {
unwatchFn = fn
.then((w) => {
watcher = w
unwatchPath = watchPath
})
.catch(onMessage)
@@ -178,11 +178,11 @@
}
function unwatch() {
if (unwatchFn) {
if (watcher) {
onMessage(`Stopped watching ${unwatchPath} for changes`)
unwatchFn()
watcher.close()
}
unwatchFn = undefined
watcher = undefined
unwatchPath = undefined
}
@@ -190,8 +190,8 @@
if (file) {
file.close()
}
if (unwatchFn) {
unwatchFn()
if (watcher) {
watcher.close()
}
})
</script>