@@ -79,7 +79,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
7979 deep ?: boolean
8080 once ?: boolean
8181 scheduler ?: Scheduler
82- middleware ?: BaseWatchMiddleware
8382 onError ?: HandleError
8483 onWarn ?: HandleWarn
8584}
@@ -92,7 +91,6 @@ export type Scheduler = (
9291 effect : ReactiveEffect ,
9392 isInit : boolean ,
9493) => void
95- export type BaseWatchMiddleware = ( next : ( ) => unknown ) => any
9694export type HandleError = ( err : unknown , type : BaseWatchErrorCodes ) => void
9795export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
9896
@@ -102,13 +100,13 @@ const DEFAULT_HANDLE_ERROR: HandleError = (err: unknown) => {
102100}
103101
104102const cleanupMap : WeakMap < ReactiveEffect , ( ( ) => void ) [ ] > = new WeakMap ( )
105- let activeEffect : ReactiveEffect | undefined = undefined
103+ let activeWatcher : ReactiveEffect | undefined = undefined
106104
107105/**
108106 * Returns the current active effect if there is one.
109107 */
110- export function getCurrentEffect ( ) {
111- return activeEffect
108+ export function getCurrentWatcher ( ) {
109+ return activeWatcher
112110}
113111
114112/**
@@ -118,15 +116,15 @@ export function getCurrentEffect() {
118116 *
119117 * @param cleanupFn - The callback function to attach to the effect's cleanup.
120118 */
121- export function onEffectCleanup ( cleanupFn : ( ) => void ) {
122- if ( activeEffect ) {
119+ export function onWatcherCleanup ( cleanupFn : ( ) => void , failSilently = false ) {
120+ if ( activeWatcher ) {
123121 const cleanups =
124- cleanupMap . get ( activeEffect ) ||
125- cleanupMap . set ( activeEffect , [ ] ) . get ( activeEffect ) !
122+ cleanupMap . get ( activeWatcher ) ||
123+ cleanupMap . set ( activeWatcher , [ ] ) . get ( activeWatcher ) !
126124 cleanups . push ( cleanupFn )
127- } else if ( __DEV__ ) {
125+ } else if ( __DEV__ && ! failSilently ) {
128126 warn (
129- `onEffectCleanup () was called when there was no active effect ` +
127+ `onWatcherCleanup () was called when there was no active watcher ` +
130128 ` to associate with.` ,
131129 )
132130 }
@@ -142,7 +140,6 @@ export function baseWatch(
142140 scheduler = DEFAULT_SCHEDULER ,
143141 onWarn = __DEV__ ? warn : NOOP ,
144142 onError = DEFAULT_HANDLE_ERROR ,
145- middleware,
146143 onTrack,
147144 onTrigger,
148145 } : BaseWatchOptions = EMPTY_OBJ ,
@@ -209,23 +206,19 @@ export function baseWatch(
209206 resetTracking ( )
210207 }
211208 }
212- const currentEffect = activeEffect
213- activeEffect = effect
209+ const currentEffect = activeWatcher
210+ activeWatcher = effect
214211 try {
215212 return callWithAsyncErrorHandling (
216213 source ,
217214 onError ,
218215 BaseWatchErrorCodes . WATCH_CALLBACK ,
219- [ onEffectCleanup ] ,
216+ [ onWatcherCleanup ] ,
220217 )
221218 } finally {
222- activeEffect = currentEffect
219+ activeWatcher = currentEffect
223220 }
224221 }
225- if ( middleware ) {
226- const baseGetter = getter
227- getter = ( ) => middleware ( baseGetter )
228- }
229222 }
230223 } else {
231224 getter = NOOP
@@ -239,19 +232,19 @@ export function baseWatch(
239232
240233 if ( once ) {
241234 if ( ! cb ) {
242- // onEffectCleanup need use effect as a key
235+ // onWatcherCleanup need use effect as a key
243236 getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
244237 getter ( )
245238 return
246239 }
247240 if ( immediate ) {
248- // onEffectCleanup need use effect as a key
241+ // onWatcherCleanup need use effect as a key
249242 getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
250243 callWithAsyncErrorHandling (
251244 cb ,
252245 onError ,
253246 BaseWatchErrorCodes . WATCH_CALLBACK ,
254- [ getter ( ) , isMultiSource ? [ ] : undefined , onEffectCleanup ] ,
247+ [ getter ( ) , isMultiSource ? [ ] : undefined , onWatcherCleanup ] ,
255248 )
256249 return
257250 }
@@ -282,38 +275,31 @@ export function baseWatch(
282275 ? ( newValue as any [ ] ) . some ( ( v , i ) => hasChanged ( v , oldValue [ i ] ) )
283276 : hasChanged ( newValue , oldValue ) )
284277 ) {
285- const next = ( ) => {
286- // cleanup before running cb again
287- if ( cleanup ) {
288- cleanup ( )
289- }
290- const currentEffect = activeEffect
291- activeEffect = effect
292- try {
293- callWithAsyncErrorHandling (
294- cb ! ,
295- onError ,
296- BaseWatchErrorCodes . WATCH_CALLBACK ,
297- [
298- newValue ,
299- // pass undefined as the old value when it's changed for the first time
300- oldValue === INITIAL_WATCHER_VALUE
301- ? undefined
302- : isMultiSource && oldValue [ 0 ] === INITIAL_WATCHER_VALUE
303- ? [ ]
304- : oldValue ,
305- onEffectCleanup ,
306- ] ,
307- )
308- oldValue = newValue
309- } finally {
310- activeEffect = currentEffect
311- }
278+ // cleanup before running cb again
279+ if ( cleanup ) {
280+ cleanup ( )
312281 }
313- if ( middleware ) {
314- middleware ( next )
315- } else {
316- next ( )
282+ const currentWatcher = activeWatcher
283+ activeWatcher = effect
284+ try {
285+ callWithAsyncErrorHandling (
286+ cb ! ,
287+ onError ,
288+ BaseWatchErrorCodes . WATCH_CALLBACK ,
289+ [
290+ newValue ,
291+ // pass undefined as the old value when it's changed for the first time
292+ oldValue === INITIAL_WATCHER_VALUE
293+ ? undefined
294+ : isMultiSource && oldValue [ 0 ] === INITIAL_WATCHER_VALUE
295+ ? [ ]
296+ : oldValue ,
297+ onWatcherCleanup ,
298+ ] ,
299+ )
300+ oldValue = newValue
301+ } finally {
302+ activeWatcher = currentWatcher
317303 }
318304 }
319305 } else {
0 commit comments