@@ -24,12 +24,13 @@ import { isReactive, isShallow } from './reactive'
24
24
import { type Ref , isRef } from './ref'
25
25
import { getCurrentScope } from './effectScope'
26
26
27
- // contexts where user provided function may be executed, in addition to
28
- // lifecycle hooks.
27
+ // These errors were transferred from `packages/runtime-core/src/errorHandling.ts`
28
+ // along with baseWatch to maintain code compatibility. Hence,
29
+ // it is essential to keep these values unchanged.
29
30
export enum BaseWatchErrorCodes {
30
- WATCH_GETTER = 'BaseWatchErrorCodes_WATCH_GETTER' ,
31
- WATCH_CALLBACK = 'BaseWatchErrorCodes_WATCH_CALLBACK' ,
32
- WATCH_CLEANUP = 'BaseWatchErrorCodes_WATCH_CLEANUP' ,
31
+ WATCH_GETTER = 2 ,
32
+ WATCH_CALLBACK ,
33
+ WATCH_CLEANUP ,
33
34
}
34
35
35
36
// TODO move to a scheduler package
@@ -57,15 +58,12 @@ export interface SchedulerJob extends Function {
57
58
}
58
59
59
60
type WatchEffect = ( onCleanup : OnCleanup ) => void
60
-
61
61
type WatchSource < T = any > = Ref < T > | ComputedRef < T > | ( ( ) => T )
62
-
63
62
type WatchCallback < V = any , OV = any > = (
64
63
value : V ,
65
64
oldValue : OV ,
66
65
onCleanup : OnCleanup ,
67
66
) => any
68
-
69
67
type OnCleanup = ( cleanupFn : ( ) => void ) => void
70
68
71
69
export interface BaseWatchOptions < Immediate = boolean > extends DebuggerOptions {
@@ -80,22 +78,19 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
80
78
// initial value for watchers to trigger on undefined initial values
81
79
const INITIAL_WATCHER_VALUE = { }
82
80
83
- export type Scheduler = ( context : {
84
- effect : ReactiveEffect
85
- job : SchedulerJob
86
- isInit : boolean
87
- } ) => void
88
-
89
- const DEFAULT_SCHEDULER : Scheduler = ( { job } ) => job ( )
90
-
81
+ export type Scheduler = (
82
+ job : SchedulerJob ,
83
+ effect : ReactiveEffect ,
84
+ isInit : boolean ,
85
+ ) => void
91
86
export type HandleError = ( err : unknown , type : BaseWatchErrorCodes ) => void
87
+ export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
92
88
89
+ const DEFAULT_SCHEDULER : Scheduler = job => job ( )
93
90
const DEFAULT_HANDLE_ERROR : HandleError = ( err : unknown ) => {
94
91
throw err
95
92
}
96
93
97
- export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
98
-
99
94
const cleanupMap : WeakMap < ReactiveEffect , ( ( ) => void ) [ ] > = new WeakMap ( )
100
95
let activeEffect : ReactiveEffect | undefined = undefined
101
96
@@ -306,12 +301,7 @@ export function baseWatch(
306
301
// it is allowed to self-trigger (#1727)
307
302
job . allowRecurse = ! ! cb
308
303
309
- let effectScheduler : EffectScheduler = ( ) =>
310
- scheduler ( {
311
- effect,
312
- job,
313
- isInit : false ,
314
- } )
304
+ let effectScheduler : EffectScheduler = ( ) => scheduler ( job , effect , false )
315
305
316
306
effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
317
307
@@ -342,11 +332,7 @@ export function baseWatch(
342
332
oldValue = effect . run ( )
343
333
}
344
334
} else {
345
- scheduler ( {
346
- effect,
347
- job,
348
- isInit : true ,
349
- } )
335
+ scheduler ( job , effect , true )
350
336
}
351
337
352
338
return effect
0 commit comments