Skip to content

Commit 2661cb2

Browse files
authored
refactor(reactivity): remove middleware (#156)
1 parent 268a2c4 commit 2661cb2

File tree

6 files changed

+29
-134
lines changed

6 files changed

+29
-134
lines changed

packages/reactivity/__tests__/baseWatch.spec.ts

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -180,87 +180,4 @@ describe('baseWatch', () => {
180180
scope.stop()
181181
expect(calls).toEqual(['sync 2', 'post 2'])
182182
})
183-
184-
test('baseWatch with middleware', async () => {
185-
let effectCalls: string[] = []
186-
let watchCalls: string[] = []
187-
const source = ref(0)
188-
189-
// effect
190-
baseWatch(
191-
() => {
192-
source.value
193-
effectCalls.push('effect')
194-
onWatcherCleanup(() => effectCalls.push('effect cleanup'))
195-
},
196-
null,
197-
{
198-
scheduler,
199-
middleware: next => {
200-
effectCalls.push('before effect running')
201-
next()
202-
effectCalls.push('effect ran')
203-
},
204-
},
205-
)
206-
// watch
207-
baseWatch(
208-
() => source.value,
209-
() => {
210-
watchCalls.push('watch')
211-
onWatcherCleanup(() => watchCalls.push('watch cleanup'))
212-
},
213-
{
214-
scheduler,
215-
middleware: next => {
216-
watchCalls.push('before watch running')
217-
next()
218-
watchCalls.push('watch ran')
219-
},
220-
},
221-
)
222-
223-
expect(effectCalls).toEqual([
224-
'before effect running',
225-
'effect',
226-
'effect ran',
227-
])
228-
expect(watchCalls).toEqual([])
229-
await nextTick()
230-
expect(effectCalls).toEqual([
231-
'before effect running',
232-
'effect',
233-
'effect ran',
234-
])
235-
expect(watchCalls).toEqual([])
236-
effectCalls.length = 0
237-
watchCalls.length = 0
238-
239-
source.value++
240-
await nextTick()
241-
expect(effectCalls).toEqual([
242-
'before effect running',
243-
'effect cleanup',
244-
'effect',
245-
'effect ran',
246-
])
247-
expect(watchCalls).toEqual(['before watch running', 'watch', 'watch ran'])
248-
effectCalls.length = 0
249-
watchCalls.length = 0
250-
251-
source.value++
252-
await nextTick()
253-
expect(effectCalls).toEqual([
254-
'before effect running',
255-
'effect cleanup',
256-
'effect',
257-
'effect ran',
258-
])
259-
expect(watchCalls).toEqual([
260-
'before watch running',
261-
'watch cleanup',
262-
'watch',
263-
'watch ran',
264-
])
265-
})
266183
})

packages/reactivity/src/baseWatch.ts

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
4747
deep?: boolean
4848
once?: boolean
4949
scheduler?: WatchScheduler
50-
middleware?: BaseWatchMiddleware
5150
onError?: HandleError
5251
onWarn?: HandleWarn
5352
}
@@ -61,7 +60,6 @@ export type WatchScheduler = (
6160
immediateFirstRun: boolean,
6261
hasCb: boolean,
6362
) => void
64-
export type BaseWatchMiddleware = (next: () => unknown) => any
6563
export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void
6664
export type HandleWarn = (msg: string, ...args: any[]) => void
6765

@@ -122,7 +120,6 @@ export function baseWatch(
122120
scheduler = DEFAULT_SCHEDULER,
123121
onWarn = __DEV__ ? warn : NOOP,
124122
onError = DEFAULT_HANDLE_ERROR,
125-
middleware,
126123
onTrack,
127124
onTrigger,
128125
}: BaseWatchOptions = EMPTY_OBJ,
@@ -202,10 +199,6 @@ export function baseWatch(
202199
activeWatcher = currentEffect
203200
}
204201
}
205-
if (middleware) {
206-
const baseGetter = getter
207-
getter = () => middleware(baseGetter)
208-
}
209202
}
210203
} else {
211204
getter = NOOP
@@ -253,38 +246,31 @@ export function baseWatch(
253246
? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i]))
254247
: hasChanged(newValue, oldValue))
255248
) {
256-
const next = () => {
257-
// cleanup before running cb again
258-
if (cleanup) {
259-
cleanup()
260-
}
261-
const currentWatcher = activeWatcher
262-
activeWatcher = effect
263-
try {
264-
callWithAsyncErrorHandling(
265-
cb!,
266-
onError,
267-
BaseWatchErrorCodes.WATCH_CALLBACK,
268-
[
269-
newValue,
270-
// pass undefined as the old value when it's changed for the first time
271-
oldValue === INITIAL_WATCHER_VALUE
272-
? undefined
273-
: isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE
274-
? []
275-
: oldValue,
276-
onWatcherCleanup,
277-
],
278-
)
279-
oldValue = newValue
280-
} finally {
281-
activeWatcher = currentWatcher
282-
}
249+
// cleanup before running cb again
250+
if (cleanup) {
251+
cleanup()
283252
}
284-
if (middleware) {
285-
middleware(next)
286-
} else {
287-
next()
253+
const currentWatcher = activeWatcher
254+
activeWatcher = effect
255+
try {
256+
callWithAsyncErrorHandling(
257+
cb!,
258+
onError,
259+
BaseWatchErrorCodes.WATCH_CALLBACK,
260+
[
261+
newValue,
262+
// pass undefined as the old value when it's changed for the first time
263+
oldValue === INITIAL_WATCHER_VALUE
264+
? undefined
265+
: isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE
266+
? []
267+
: oldValue,
268+
onWatcherCleanup,
269+
],
270+
)
271+
oldValue = newValue
272+
} finally {
273+
activeWatcher = currentWatcher
288274
}
289275
}
290276
} else {

packages/reactivity/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ export {
8585
onWatcherCleanup,
8686
BaseWatchErrorCodes,
8787
type BaseWatchOptions,
88-
type BaseWatchMiddleware,
8988
type WatchScheduler,
9089
} from './baseWatch'
9190
export { type SchedulerJob, SchedulerJobFlags } from './scheduler'

packages/runtime-vapor/__tests__/renderEffect.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { onEffectCleanup } from '@vue/reactivity'
21
import {
32
nextTick,
43
onBeforeUpdate,
4+
onEffectCleanup,
55
onUpdated,
66
ref,
77
renderEffect,

packages/runtime-vapor/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ export {
2929
// effect
3030
stop,
3131
ReactiveEffect,
32-
onWatcherCleanup,
32+
onEffectCleanup,
3333
// effect scope
3434
effectScope,
3535
EffectScope,
3636
getCurrentScope,
3737
onScopeDispose,
38+
// baseWatch
39+
onWatcherCleanup,
40+
getCurrentWatcher,
3841
} from '@vue/reactivity'
3942

4043
export { nextTick } from './scheduler'

packages/runtime-vapor/src/scheduler.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,6 @@ export const createVaporPreScheduler: SchedulerFactory =
213213
}
214214
}
215215

216-
export const createVaporRenderingScheduler: SchedulerFactory =
217-
instance => (job, effect, immediateFirstRun, hasCb) => {
218-
if (!immediateFirstRun) {
219-
if (instance) job.id = instance.uid
220-
queueJob(job)
221-
} else if (!hasCb) {
222-
effect.run()
223-
}
224-
}
225-
226216
export const createVaporPostScheduler: SchedulerFactory =
227217
instance => (job, effect, immediateFirstRun, hasCb) => {
228218
if (!immediateFirstRun) {

0 commit comments

Comments
 (0)