11import {
2- isRef ,
3- type Ref ,
2+ type BaseWatchErrorCodes ,
3+ type BaseWatchOptions ,
44 type ComputedRef ,
5- ReactiveFlags ,
65 type DebuggerOptions ,
7- getCurrentScope ,
8- BaseWatchErrorCodes ,
6+ ReactiveFlags ,
7+ type Ref ,
98 baseWatch ,
10- type BaseWatchOptions
9+ getCurrentScope ,
10+ isRef ,
1111} from '@vue/reactivity'
1212import {
1313 type SchedulerJob ,
1414 usePreScheduler ,
15- useSyncScheduler
15+ useSyncScheduler ,
1616} from './scheduler'
1717import {
1818 EMPTY_OBJ ,
19- isObject ,
19+ NOOP ,
20+ extend ,
2021 isArray ,
2122 isFunction ,
22- isString ,
23- NOOP ,
24- remove ,
2523 isMap ,
26- isSet ,
24+ isObject ,
2725 isPlainObject ,
28- extend
26+ isSet ,
27+ isString ,
28+ remove ,
2929} from '@vue/shared'
3030import {
31- currentInstance ,
3231 type ComponentInternalInstance ,
32+ currentInstance ,
3333 isInSSRComponentSetup ,
3434 setCurrentInstance ,
35- unsetCurrentInstance
35+ unsetCurrentInstance ,
3636} from './component'
3737import { handleError as handleErrorWithInstance } from './errorHandling'
3838import { usePostRenderScheduler } from './renderer'
3939import { warn } from './warning'
40- import { type ObjectWatchOptionItem } from './componentOptions'
40+ import type { ObjectWatchOptionItem } from './componentOptions'
4141import { useSSRContext } from '@vue/runtime-core'
4242
4343export type WatchEffect = ( onCleanup : OnCleanup ) => void
@@ -47,7 +47,7 @@ export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
4747export type WatchCallback < V = any , OV = any > = (
4848 value : V ,
4949 oldValue : OV ,
50- onCleanup : OnCleanup
50+ onCleanup : OnCleanup ,
5151) => any
5252
5353type MapSources < T , Immediate > = {
@@ -79,30 +79,30 @@ export type WatchStopHandle = () => void
7979// Simple effect.
8080export function watchEffect (
8181 effect : WatchEffect ,
82- options ?: WatchOptionsBase
82+ options ?: WatchOptionsBase ,
8383) : WatchStopHandle {
8484 return doWatch ( effect , null , options )
8585}
8686
8787export function watchPostEffect (
8888 effect : WatchEffect ,
89- options ?: DebuggerOptions
89+ options ?: DebuggerOptions ,
9090) {
9191 return doWatch (
9292 effect ,
9393 null ,
94- __DEV__ ? extend ( { } , options as any , { flush : 'post' } ) : { flush : 'post' }
94+ __DEV__ ? extend ( { } , options as any , { flush : 'post' } ) : { flush : 'post' } ,
9595 )
9696}
9797
9898export function watchSyncEffect (
9999 effect : WatchEffect ,
100- options ?: DebuggerOptions
100+ options ?: DebuggerOptions ,
101101) {
102102 return doWatch (
103103 effect ,
104104 null ,
105- __DEV__ ? extend ( { } , options as any , { flush : 'sync' } ) : { flush : 'sync' }
105+ __DEV__ ? extend ( { } , options as any , { flush : 'sync' } ) : { flush : 'sync' } ,
106106 )
107107}
108108
@@ -111,60 +111,60 @@ type MultiWatchSources = (WatchSource<unknown> | object)[]
111111// overload: array of multiple sources + cb
112112export function watch <
113113 T extends MultiWatchSources ,
114- Immediate extends Readonly < boolean > = false
114+ Immediate extends Readonly < boolean > = false ,
115115> (
116116 sources : [ ...T ] ,
117117 cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
118- options ?: WatchOptions < Immediate >
118+ options ?: WatchOptions < Immediate > ,
119119) : WatchStopHandle
120120
121121// overload: multiple sources w/ `as const`
122122// watch([foo, bar] as const, () => {})
123123// somehow [...T] breaks when the type is readonly
124124export function watch <
125125 T extends Readonly < MultiWatchSources > ,
126- Immediate extends Readonly < boolean > = false
126+ Immediate extends Readonly < boolean > = false ,
127127> (
128128 source : T ,
129129 cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
130- options ?: WatchOptions < Immediate >
130+ options ?: WatchOptions < Immediate > ,
131131) : WatchStopHandle
132132
133133// overload: single source + cb
134134export function watch < T , Immediate extends Readonly < boolean > = false > (
135135 source : WatchSource < T > ,
136136 cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
137- options ?: WatchOptions < Immediate >
137+ options ?: WatchOptions < Immediate > ,
138138) : WatchStopHandle
139139
140140// overload: watching reactive object w/ cb
141141export function watch <
142142 T extends object ,
143- Immediate extends Readonly < boolean > = false
143+ Immediate extends Readonly < boolean > = false ,
144144> (
145145 source : T ,
146146 cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
147- options ?: WatchOptions < Immediate >
147+ options ?: WatchOptions < Immediate > ,
148148) : WatchStopHandle
149149
150150// implementation
151151export function watch < T = any , Immediate extends Readonly < boolean > = false > (
152152 source : T | WatchSource < T > ,
153153 cb : any ,
154- options ?: WatchOptions < Immediate >
154+ options ?: WatchOptions < Immediate > ,
155155) : WatchStopHandle {
156156 if ( __DEV__ && ! isFunction ( cb ) ) {
157157 warn (
158158 `\`watch(fn, options?)\` signature has been moved to a separate API. ` +
159159 `Use \`watchEffect(fn, options?)\` instead. \`watch\` now only ` +
160- `supports \`watch(source, cb, options?) signature.`
160+ `supports \`watch(source, cb, options?) signature.` ,
161161 )
162162 }
163163 return doWatch ( source as any , cb , options )
164164}
165165
166166function getSchedulerByFlushMode (
167- flush : WatchOptionsBase [ 'flush' ]
167+ flush : WatchOptionsBase [ 'flush' ] ,
168168) : SchedulerJob {
169169 if ( flush === 'post' ) {
170170 return usePostRenderScheduler
@@ -179,26 +179,26 @@ function getSchedulerByFlushMode(
179179function doWatch (
180180 source : WatchSource | WatchSource [ ] | WatchEffect | object ,
181181 cb : WatchCallback | null ,
182- options : WatchOptions = EMPTY_OBJ
182+ options : WatchOptions = EMPTY_OBJ ,
183183) : WatchStopHandle {
184184 const { immediate, deep, flush, once } = options
185185 if ( __DEV__ && ! cb ) {
186186 if ( immediate !== undefined ) {
187187 warn (
188188 `watch() "immediate" option is only respected when using the ` +
189- `watch(source, callback, options?) signature.`
189+ `watch(source, callback, options?) signature.` ,
190190 )
191191 }
192192 if ( deep !== undefined ) {
193193 warn (
194194 `watch() "deep" option is only respected when using the ` +
195- `watch(source, callback, options?) signature.`
195+ `watch(source, callback, options?) signature.` ,
196196 )
197197 }
198198 if ( once !== undefined ) {
199199 warn (
200200 `watch() "once" option is only respected when using the ` +
201- `watch(source, callback, options?) signature.`
201+ `watch(source, callback, options?) signature.` ,
202202 )
203203 }
204204 }
@@ -246,7 +246,7 @@ export function instanceWatch(
246246 this : ComponentInternalInstance ,
247247 source : string | Function ,
248248 value : WatchCallback | ObjectWatchOptionItem ,
249- options ?: WatchOptions
249+ options ?: WatchOptions ,
250250) : WatchStopHandle {
251251 const publicThis = this . proxy as any
252252 const getter = isString ( source )
0 commit comments