Skip to content

Commit 3694745

Browse files
committed
refactor: rename to onWatcherCleanup, getCurrentWatcher, remove middleware
1 parent b3f45d2 commit 3694745

File tree

3 files changed

+48
-140
lines changed

3 files changed

+48
-140
lines changed

packages/reactivity/__tests__/baseWatch.spec.ts

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
EffectScope,
55
type Ref,
66
baseWatch,
7+
onWatcherCleanup,
78
ref,
89
} from '../src'
9-
import { onEffectCleanup } from '../src/effect'
1010

1111
const queue: SchedulerJob[] = []
1212

@@ -67,7 +67,7 @@ describe('baseWatch', () => {
6767
const effect = baseWatch(
6868
source,
6969
() => {
70-
onEffectCleanup(() => {
70+
onWatcherCleanup(() => {
7171
throw 'oops in cleanup'
7272
})
7373
throw 'oops in watch'
@@ -108,8 +108,8 @@ describe('baseWatch', () => {
108108
source.value
109109

110110
onCleanup(() => (dummy += 2))
111-
onEffectCleanup(() => (dummy += 3))
112-
onEffectCleanup(() => (dummy += 5))
111+
onWatcherCleanup(() => (dummy += 3))
112+
onWatcherCleanup(() => (dummy += 5))
113113
})
114114
})
115115
expect(dummy).toBe(0)
@@ -141,7 +141,7 @@ describe('baseWatch', () => {
141141
baseWatch(
142142
() => {
143143
const current = (copyist.value = source.value)
144-
onEffectCleanup(() => calls.push(`sync ${current}`))
144+
onWatcherCleanup(() => calls.push(`sync ${current}`))
145145
},
146146
null,
147147
{},
@@ -150,7 +150,7 @@ describe('baseWatch', () => {
150150
baseWatch(
151151
() => {
152152
const current = copyist.value
153-
onEffectCleanup(() => calls.push(`post ${current}`))
153+
onWatcherCleanup(() => calls.push(`post ${current}`))
154154
},
155155
null,
156156
{ scheduler },
@@ -175,82 +175,4 @@ describe('baseWatch', () => {
175175
scope.stop()
176176
expect(calls).toEqual(['sync 2', 'post 2'])
177177
})
178-
test('baseWatch with middleware', async () => {
179-
let effectCalls: string[] = []
180-
let watchCalls: string[] = []
181-
const source = ref(0)
182-
183-
// effect
184-
baseWatch(
185-
() => {
186-
source.value
187-
effectCalls.push('effect')
188-
onEffectCleanup(() => effectCalls.push('effect cleanup'))
189-
},
190-
null,
191-
{
192-
scheduler,
193-
middleware: next => {
194-
effectCalls.push('before effect running')
195-
next()
196-
effectCalls.push('effect ran')
197-
},
198-
},
199-
)
200-
// watch
201-
baseWatch(
202-
() => source.value,
203-
() => {
204-
watchCalls.push('watch')
205-
onEffectCleanup(() => watchCalls.push('watch cleanup'))
206-
},
207-
{
208-
scheduler,
209-
middleware: next => {
210-
watchCalls.push('before watch running')
211-
next()
212-
watchCalls.push('watch ran')
213-
},
214-
},
215-
)
216-
217-
expect(effectCalls).toEqual([])
218-
expect(watchCalls).toEqual([])
219-
await nextTick()
220-
expect(effectCalls).toEqual([
221-
'before effect running',
222-
'effect',
223-
'effect ran',
224-
])
225-
expect(watchCalls).toEqual([])
226-
effectCalls.length = 0
227-
watchCalls.length = 0
228-
229-
source.value++
230-
await nextTick()
231-
expect(effectCalls).toEqual([
232-
'before effect running',
233-
'effect cleanup',
234-
'effect',
235-
'effect ran',
236-
])
237-
expect(watchCalls).toEqual(['before watch running', 'watch', 'watch ran'])
238-
effectCalls.length = 0
239-
watchCalls.length = 0
240-
241-
source.value++
242-
await nextTick()
243-
expect(effectCalls).toEqual([
244-
'before effect running',
245-
'effect cleanup',
246-
'effect',
247-
'effect ran',
248-
])
249-
expect(watchCalls).toEqual([
250-
'before watch running',
251-
'watch cleanup',
252-
'watch',
253-
'watch ran',
254-
])
255-
})
256178
})

packages/reactivity/src/baseWatch.ts

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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
9694
export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void
9795
export type HandleWarn = (msg: string, ...args: any[]) => void
9896

@@ -102,13 +100,13 @@ const DEFAULT_HANDLE_ERROR: HandleError = (err: unknown) => {
102100
}
103101

104102
const 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 {

packages/reactivity/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ export { reactiveReadArray, shallowReadArray } from './arrayInstrumentations'
7979
export { TrackOpTypes, TriggerOpTypes, ReactiveFlags } from './constants'
8080
export {
8181
baseWatch,
82-
getCurrentEffect,
82+
getCurrentWatcher as getCurrentEffect,
8383
traverse,
84+
onWatcherCleanup,
8485
BaseWatchErrorCodes,
8586
type BaseWatchOptions,
86-
type BaseWatchMiddleware,
8787
type Scheduler,
8888
} from './baseWatch'

0 commit comments

Comments
 (0)