1
1
import type { MaybeRefOrGetter , ShallowReactive } from '@reactive-vscode/reactivity'
2
- import { onScopeDispose , shallowReactive , toValue , watchEffect } from '@reactive-vscode/reactivity'
2
+ import { computed , onScopeDispose , shallowReactive , toValue , watch , watchEffect } from '@reactive-vscode/reactivity'
3
3
import type { Event , FileSystemWatcher , GlobPattern , Uri } from 'vscode'
4
4
import { workspace } from 'vscode'
5
5
import type { MaybeNullableRefOrGetter } from '../utils'
@@ -26,13 +26,17 @@ export function useFsWatcher(
26
26
const changeEmitter = useEventEmitter < Uri > ( )
27
27
const deleteEmitter = useEventEmitter < Uri > ( )
28
28
29
- watchEffect ( ( ) => {
29
+ const normalizedPatterns = computed ( ( ) => {
30
30
const globPatternValue = toValue ( globPattern )
31
- const newPatterns = Array . isArray ( globPatternValue )
31
+ return Array . isArray ( globPatternValue )
32
32
? globPatternValue
33
33
: globPatternValue instanceof Set
34
34
? Array . from ( globPatternValue )
35
35
: [ globPatternValue ]
36
+ } )
37
+
38
+ function updateWatchers ( ) {
39
+ const newPatterns = normalizedPatterns . value
36
40
for ( const [ pattern , watcher ] of watchers ) {
37
41
if ( ! newPatterns . includes ( pattern ) ) {
38
42
watcher . dispose ( )
@@ -52,14 +56,21 @@ export function useFsWatcher(
52
56
w . onDidDelete ( deleteEmitter . fire )
53
57
}
54
58
}
55
- } )
59
+ }
56
60
57
- onScopeDispose ( ( ) => {
61
+ function clearWatchers ( ) {
58
62
for ( const watcher of watchers . values ( ) ) {
59
63
watcher . dispose ( )
60
64
}
61
65
watchers . clear ( )
66
+ }
67
+
68
+ watch ( normalizedPatterns , updateWatchers )
69
+ watch ( [ ignoreCreateEvents , ignoreChangeEvents , ignoreDeleteEvents ] , ( ) => {
70
+ clearWatchers ( )
71
+ updateWatchers ( )
62
72
} )
73
+ onScopeDispose ( clearWatchers )
63
74
64
75
return {
65
76
watchers,
0 commit comments