1
1
import {
2
- isRef ,
3
- type Ref ,
2
+ type BaseWatchErrorCodes ,
3
+ type BaseWatchOptions ,
4
4
type ComputedRef ,
5
- ReactiveFlags ,
6
5
type DebuggerOptions ,
7
- getCurrentScope ,
8
- BaseWatchErrorCodes ,
6
+ ReactiveFlags ,
7
+ type Ref ,
9
8
baseWatch ,
10
- type BaseWatchOptions
9
+ getCurrentScope ,
10
+ isRef ,
11
11
} from '@vue/reactivity'
12
12
import {
13
13
type SchedulerJob ,
14
14
usePreScheduler ,
15
- useSyncScheduler
15
+ useSyncScheduler ,
16
16
} from './scheduler'
17
17
import {
18
18
EMPTY_OBJ ,
19
- isObject ,
19
+ NOOP ,
20
+ extend ,
20
21
isArray ,
21
22
isFunction ,
22
- isString ,
23
- NOOP ,
24
- remove ,
25
23
isMap ,
26
- isSet ,
24
+ isObject ,
27
25
isPlainObject ,
28
- extend
26
+ isSet ,
27
+ isString ,
28
+ remove ,
29
29
} from '@vue/shared'
30
30
import {
31
- currentInstance ,
32
31
type ComponentInternalInstance ,
32
+ currentInstance ,
33
33
isInSSRComponentSetup ,
34
34
setCurrentInstance ,
35
- unsetCurrentInstance
35
+ unsetCurrentInstance ,
36
36
} from './component'
37
37
import { handleError as handleErrorWithInstance } from './errorHandling'
38
38
import { usePostRenderScheduler } from './renderer'
39
39
import { warn } from './warning'
40
- import { type ObjectWatchOptionItem } from './componentOptions'
40
+ import type { ObjectWatchOptionItem } from './componentOptions'
41
41
import { useSSRContext } from '@vue/runtime-core'
42
42
43
43
export type WatchEffect = ( onCleanup : OnCleanup ) => void
@@ -47,7 +47,7 @@ export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
47
47
export type WatchCallback < V = any , OV = any > = (
48
48
value : V ,
49
49
oldValue : OV ,
50
- onCleanup : OnCleanup
50
+ onCleanup : OnCleanup ,
51
51
) => any
52
52
53
53
type MapSources < T , Immediate > = {
@@ -79,30 +79,30 @@ export type WatchStopHandle = () => void
79
79
// Simple effect.
80
80
export function watchEffect (
81
81
effect : WatchEffect ,
82
- options ?: WatchOptionsBase
82
+ options ?: WatchOptionsBase ,
83
83
) : WatchStopHandle {
84
84
return doWatch ( effect , null , options )
85
85
}
86
86
87
87
export function watchPostEffect (
88
88
effect : WatchEffect ,
89
- options ?: DebuggerOptions
89
+ options ?: DebuggerOptions ,
90
90
) {
91
91
return doWatch (
92
92
effect ,
93
93
null ,
94
- __DEV__ ? extend ( { } , options as any , { flush : 'post' } ) : { flush : 'post' }
94
+ __DEV__ ? extend ( { } , options as any , { flush : 'post' } ) : { flush : 'post' } ,
95
95
)
96
96
}
97
97
98
98
export function watchSyncEffect (
99
99
effect : WatchEffect ,
100
- options ?: DebuggerOptions
100
+ options ?: DebuggerOptions ,
101
101
) {
102
102
return doWatch (
103
103
effect ,
104
104
null ,
105
- __DEV__ ? extend ( { } , options as any , { flush : 'sync' } ) : { flush : 'sync' }
105
+ __DEV__ ? extend ( { } , options as any , { flush : 'sync' } ) : { flush : 'sync' } ,
106
106
)
107
107
}
108
108
@@ -111,60 +111,60 @@ type MultiWatchSources = (WatchSource<unknown> | object)[]
111
111
// overload: array of multiple sources + cb
112
112
export function watch <
113
113
T extends MultiWatchSources ,
114
- Immediate extends Readonly < boolean > = false
114
+ Immediate extends Readonly < boolean > = false ,
115
115
> (
116
116
sources : [ ...T ] ,
117
117
cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
118
- options ?: WatchOptions < Immediate >
118
+ options ?: WatchOptions < Immediate > ,
119
119
) : WatchStopHandle
120
120
121
121
// overload: multiple sources w/ `as const`
122
122
// watch([foo, bar] as const, () => {})
123
123
// somehow [...T] breaks when the type is readonly
124
124
export function watch <
125
125
T extends Readonly < MultiWatchSources > ,
126
- Immediate extends Readonly < boolean > = false
126
+ Immediate extends Readonly < boolean > = false ,
127
127
> (
128
128
source : T ,
129
129
cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
130
- options ?: WatchOptions < Immediate >
130
+ options ?: WatchOptions < Immediate > ,
131
131
) : WatchStopHandle
132
132
133
133
// overload: single source + cb
134
134
export function watch < T , Immediate extends Readonly < boolean > = false > (
135
135
source : WatchSource < T > ,
136
136
cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
137
- options ?: WatchOptions < Immediate >
137
+ options ?: WatchOptions < Immediate > ,
138
138
) : WatchStopHandle
139
139
140
140
// overload: watching reactive object w/ cb
141
141
export function watch <
142
142
T extends object ,
143
- Immediate extends Readonly < boolean > = false
143
+ Immediate extends Readonly < boolean > = false ,
144
144
> (
145
145
source : T ,
146
146
cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
147
- options ?: WatchOptions < Immediate >
147
+ options ?: WatchOptions < Immediate > ,
148
148
) : WatchStopHandle
149
149
150
150
// implementation
151
151
export function watch < T = any , Immediate extends Readonly < boolean > = false > (
152
152
source : T | WatchSource < T > ,
153
153
cb : any ,
154
- options ?: WatchOptions < Immediate >
154
+ options ?: WatchOptions < Immediate > ,
155
155
) : WatchStopHandle {
156
156
if ( __DEV__ && ! isFunction ( cb ) ) {
157
157
warn (
158
158
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
159
159
`Use \`watchEffect(fn, options?)\` instead. \`watch\` now only ` +
160
- `supports \`watch(source, cb, options?) signature.`
160
+ `supports \`watch(source, cb, options?) signature.` ,
161
161
)
162
162
}
163
163
return doWatch ( source as any , cb , options )
164
164
}
165
165
166
166
function getSchedulerByFlushMode (
167
- flush : WatchOptionsBase [ 'flush' ]
167
+ flush : WatchOptionsBase [ 'flush' ] ,
168
168
) : SchedulerJob {
169
169
if ( flush === 'post' ) {
170
170
return usePostRenderScheduler
@@ -179,26 +179,26 @@ function getSchedulerByFlushMode(
179
179
function doWatch (
180
180
source : WatchSource | WatchSource [ ] | WatchEffect | object ,
181
181
cb : WatchCallback | null ,
182
- options : WatchOptions = EMPTY_OBJ
182
+ options : WatchOptions = EMPTY_OBJ ,
183
183
) : WatchStopHandle {
184
184
const { immediate, deep, flush, once } = options
185
185
if ( __DEV__ && ! cb ) {
186
186
if ( immediate !== undefined ) {
187
187
warn (
188
188
`watch() "immediate" option is only respected when using the ` +
189
- `watch(source, callback, options?) signature.`
189
+ `watch(source, callback, options?) signature.` ,
190
190
)
191
191
}
192
192
if ( deep !== undefined ) {
193
193
warn (
194
194
`watch() "deep" option is only respected when using the ` +
195
- `watch(source, callback, options?) signature.`
195
+ `watch(source, callback, options?) signature.` ,
196
196
)
197
197
}
198
198
if ( once !== undefined ) {
199
199
warn (
200
200
`watch() "once" option is only respected when using the ` +
201
- `watch(source, callback, options?) signature.`
201
+ `watch(source, callback, options?) signature.` ,
202
202
)
203
203
}
204
204
}
@@ -246,7 +246,7 @@ export function instanceWatch(
246
246
this : ComponentInternalInstance ,
247
247
source : string | Function ,
248
248
value : WatchCallback | ObjectWatchOptionItem ,
249
- options ?: WatchOptions
249
+ options ?: WatchOptions ,
250
250
) : WatchStopHandle {
251
251
const publicThis = this . proxy as any
252
252
const getter = isString ( source )
0 commit comments