@@ -79,7 +79,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
79
79
deep ?: boolean
80
80
once ?: boolean
81
81
scheduler ?: Scheduler
82
- middleware ?: BaseWatchMiddleware
83
82
onError ?: HandleError
84
83
onWarn ?: HandleWarn
85
84
}
@@ -92,7 +91,6 @@ export type Scheduler = (
92
91
effect : ReactiveEffect ,
93
92
isInit : boolean ,
94
93
) => void
95
- export type BaseWatchMiddleware = ( next : ( ) => unknown ) => any
96
94
export type HandleError = ( err : unknown , type : BaseWatchErrorCodes ) => void
97
95
export type HandleWarn = ( msg : string , ...args : any [ ] ) => void
98
96
@@ -102,13 +100,13 @@ const DEFAULT_HANDLE_ERROR: HandleError = (err: unknown) => {
102
100
}
103
101
104
102
const cleanupMap : WeakMap < ReactiveEffect , ( ( ) => void ) [ ] > = new WeakMap ( )
105
- let activeEffect : ReactiveEffect | undefined = undefined
103
+ let activeWatcher : ReactiveEffect | undefined = undefined
106
104
107
105
/**
108
106
* Returns the current active effect if there is one.
109
107
*/
110
- export function getCurrentEffect ( ) {
111
- return activeEffect
108
+ export function getCurrentWatcher ( ) {
109
+ return activeWatcher
112
110
}
113
111
114
112
/**
@@ -118,15 +116,15 @@ export function getCurrentEffect() {
118
116
*
119
117
* @param cleanupFn - The callback function to attach to the effect's cleanup.
120
118
*/
121
- export function onEffectCleanup ( cleanupFn : ( ) => void ) {
122
- if ( activeEffect ) {
119
+ export function onWatcherCleanup ( cleanupFn : ( ) => void , failSilently = false ) {
120
+ if ( activeWatcher ) {
123
121
const cleanups =
124
- cleanupMap . get ( activeEffect ) ||
125
- cleanupMap . set ( activeEffect , [ ] ) . get ( activeEffect ) !
122
+ cleanupMap . get ( activeWatcher ) ||
123
+ cleanupMap . set ( activeWatcher , [ ] ) . get ( activeWatcher ) !
126
124
cleanups . push ( cleanupFn )
127
- } else if ( __DEV__ ) {
125
+ } else if ( __DEV__ && ! failSilently ) {
128
126
warn (
129
- `onEffectCleanup () was called when there was no active effect ` +
127
+ `onWatcherCleanup () was called when there was no active watcher ` +
130
128
` to associate with.` ,
131
129
)
132
130
}
@@ -142,7 +140,6 @@ export function baseWatch(
142
140
scheduler = DEFAULT_SCHEDULER ,
143
141
onWarn = __DEV__ ? warn : NOOP ,
144
142
onError = DEFAULT_HANDLE_ERROR ,
145
- middleware,
146
143
onTrack,
147
144
onTrigger,
148
145
} : BaseWatchOptions = EMPTY_OBJ ,
@@ -209,23 +206,19 @@ export function baseWatch(
209
206
resetTracking ( )
210
207
}
211
208
}
212
- const currentEffect = activeEffect
213
- activeEffect = effect
209
+ const currentEffect = activeWatcher
210
+ activeWatcher = effect
214
211
try {
215
212
return callWithAsyncErrorHandling (
216
213
source ,
217
214
onError ,
218
215
BaseWatchErrorCodes . WATCH_CALLBACK ,
219
- [ onEffectCleanup ] ,
216
+ [ onWatcherCleanup ] ,
220
217
)
221
218
} finally {
222
- activeEffect = currentEffect
219
+ activeWatcher = currentEffect
223
220
}
224
221
}
225
- if ( middleware ) {
226
- const baseGetter = getter
227
- getter = ( ) => middleware ( baseGetter )
228
- }
229
222
}
230
223
} else {
231
224
getter = NOOP
@@ -239,19 +232,19 @@ export function baseWatch(
239
232
240
233
if ( once ) {
241
234
if ( ! cb ) {
242
- // onEffectCleanup need use effect as a key
235
+ // onWatcherCleanup need use effect as a key
243
236
getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
244
237
getter ( )
245
238
return
246
239
}
247
240
if ( immediate ) {
248
- // onEffectCleanup need use effect as a key
241
+ // onWatcherCleanup need use effect as a key
249
242
getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
250
243
callWithAsyncErrorHandling (
251
244
cb ,
252
245
onError ,
253
246
BaseWatchErrorCodes . WATCH_CALLBACK ,
254
- [ getter ( ) , isMultiSource ? [ ] : undefined , onEffectCleanup ] ,
247
+ [ getter ( ) , isMultiSource ? [ ] : undefined , onWatcherCleanup ] ,
255
248
)
256
249
return
257
250
}
@@ -282,38 +275,31 @@ export function baseWatch(
282
275
? ( newValue as any [ ] ) . some ( ( v , i ) => hasChanged ( v , oldValue [ i ] ) )
283
276
: hasChanged ( newValue , oldValue ) )
284
277
) {
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 ( )
312
281
}
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
317
303
}
318
304
}
319
305
} else {
0 commit comments