@@ -24,12 +24,13 @@ import { isReactive, isShallow } from './reactive'
2424import { type Ref , isRef } from './ref'
2525import { getCurrentScope } from './effectScope'
2626
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.
2930export 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 ,
3334}
3435
3536// TODO move to a scheduler package
@@ -57,15 +58,12 @@ export interface SchedulerJob extends Function {
5758}
5859
5960type WatchEffect = ( onCleanup : OnCleanup ) => void
60-
6161type WatchSource < T = any > = Ref < T > | ComputedRef < T > | ( ( ) => T )
62-
6362type WatchCallback < V = any , OV = any > = (
6463 value : V ,
6564 oldValue : OV ,
6665 onCleanup : OnCleanup ,
6766) => any
68-
6967type OnCleanup = ( cleanupFn : ( ) => void ) => void
7068
7169export interface BaseWatchOptions < Immediate = boolean > extends DebuggerOptions {
@@ -80,22 +78,19 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
8078// initial value for watchers to trigger on undefined initial values
8179const INITIAL_WATCHER_VALUE = { }
8280
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
9186export type HandleError = ( err : unknown , type : BaseWatchErrorCodes ) => void
87+ export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
9288
89+ const DEFAULT_SCHEDULER : Scheduler = job => job ( )
9390const DEFAULT_HANDLE_ERROR : HandleError = ( err : unknown ) => {
9491 throw err
9592}
9693
97- export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
98-
9994const cleanupMap : WeakMap < ReactiveEffect , ( ( ) => void ) [ ] > = new WeakMap ( )
10095let activeEffect : ReactiveEffect | undefined = undefined
10196
@@ -306,12 +301,7 @@ export function baseWatch(
306301 // it is allowed to self-trigger (#1727)
307302 job . allowRecurse = ! ! cb
308303
309- let effectScheduler : EffectScheduler = ( ) =>
310- scheduler ( {
311- effect,
312- job,
313- isInit : false ,
314- } )
304+ let effectScheduler : EffectScheduler = ( ) => scheduler ( job , effect , false )
315305
316306 effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
317307
@@ -342,11 +332,7 @@ export function baseWatch(
342332 oldValue = effect . run ( )
343333 }
344334 } else {
345- scheduler ( {
346- effect,
347- job,
348- isInit : true ,
349- } )
335+ scheduler ( job , effect , true )
350336 }
351337
352338 return effect
0 commit comments