mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user