refactor(reactivity): remove middleware (#156)

This commit is contained in:
Rizumu Ayaka 2024-03-18 21:57:18 +08:00 committed by GitHub
parent 268a2c4ae6
commit 2661cb2474
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 134 deletions

View File

@ -180,87 +180,4 @@ describe('baseWatch', () => {
scope.stop() scope.stop()
expect(calls).toEqual(['sync 2', 'post 2']) expect(calls).toEqual(['sync 2', 'post 2'])
}) })
test('baseWatch with middleware', async () => {
let effectCalls: string[] = []
let watchCalls: string[] = []
const source = ref(0)
// effect
baseWatch(
() => {
source.value
effectCalls.push('effect')
onWatcherCleanup(() => effectCalls.push('effect cleanup'))
},
null,
{
scheduler,
middleware: next => {
effectCalls.push('before effect running')
next()
effectCalls.push('effect ran')
},
},
)
// watch
baseWatch(
() => source.value,
() => {
watchCalls.push('watch')
onWatcherCleanup(() => watchCalls.push('watch cleanup'))
},
{
scheduler,
middleware: next => {
watchCalls.push('before watch running')
next()
watchCalls.push('watch ran')
},
},
)
expect(effectCalls).toEqual([
'before effect running',
'effect',
'effect ran',
])
expect(watchCalls).toEqual([])
await nextTick()
expect(effectCalls).toEqual([
'before effect running',
'effect',
'effect ran',
])
expect(watchCalls).toEqual([])
effectCalls.length = 0
watchCalls.length = 0
source.value++
await nextTick()
expect(effectCalls).toEqual([
'before effect running',
'effect cleanup',
'effect',
'effect ran',
])
expect(watchCalls).toEqual(['before watch running', 'watch', 'watch ran'])
effectCalls.length = 0
watchCalls.length = 0
source.value++
await nextTick()
expect(effectCalls).toEqual([
'before effect running',
'effect cleanup',
'effect',
'effect ran',
])
expect(watchCalls).toEqual([
'before watch running',
'watch cleanup',
'watch',
'watch ran',
])
})
}) })

View File

@ -47,7 +47,6 @@ export interface BaseWatchOptions<Immediate = boolean> extends DebuggerOptions {
deep?: boolean deep?: boolean
once?: boolean once?: boolean
scheduler?: WatchScheduler scheduler?: WatchScheduler
middleware?: BaseWatchMiddleware
onError?: HandleError onError?: HandleError
onWarn?: HandleWarn onWarn?: HandleWarn
} }
@ -61,7 +60,6 @@ export type WatchScheduler = (
immediateFirstRun: boolean, immediateFirstRun: boolean,
hasCb: boolean, hasCb: boolean,
) => void ) => void
export type BaseWatchMiddleware = (next: () => unknown) => any
export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void export type HandleError = (err: unknown, type: BaseWatchErrorCodes) => void
export type HandleWarn = (msg: string, ...args: any[]) => void export type HandleWarn = (msg: string, ...args: any[]) => void
@ -122,7 +120,6 @@ export function baseWatch(
scheduler = DEFAULT_SCHEDULER, scheduler = DEFAULT_SCHEDULER,
onWarn = __DEV__ ? warn : NOOP, onWarn = __DEV__ ? warn : NOOP,
onError = DEFAULT_HANDLE_ERROR, onError = DEFAULT_HANDLE_ERROR,
middleware,
onTrack, onTrack,
onTrigger, onTrigger,
}: BaseWatchOptions = EMPTY_OBJ, }: BaseWatchOptions = EMPTY_OBJ,
@ -202,10 +199,6 @@ export function baseWatch(
activeWatcher = currentEffect activeWatcher = currentEffect
} }
} }
if (middleware) {
const baseGetter = getter
getter = () => middleware(baseGetter)
}
} }
} else { } else {
getter = NOOP getter = NOOP
@ -253,7 +246,6 @@ export function baseWatch(
? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i])) ? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i]))
: hasChanged(newValue, oldValue)) : hasChanged(newValue, oldValue))
) { ) {
const next = () => {
// cleanup before running cb again // cleanup before running cb again
if (cleanup) { if (cleanup) {
cleanup() cleanup()
@ -281,12 +273,6 @@ export function baseWatch(
activeWatcher = currentWatcher activeWatcher = currentWatcher
} }
} }
if (middleware) {
middleware(next)
} else {
next()
}
}
} else { } else {
// watchEffect // watchEffect
effect.run() effect.run()

View File

@ -85,7 +85,6 @@ export {
onWatcherCleanup, onWatcherCleanup,
BaseWatchErrorCodes, BaseWatchErrorCodes,
type BaseWatchOptions, type BaseWatchOptions,
type BaseWatchMiddleware,
type WatchScheduler, type WatchScheduler,
} from './baseWatch' } from './baseWatch'
export { type SchedulerJob, SchedulerJobFlags } from './scheduler' export { type SchedulerJob, SchedulerJobFlags } from './scheduler'

View File

@ -1,7 +1,7 @@
import { onEffectCleanup } from '@vue/reactivity'
import { import {
nextTick, nextTick,
onBeforeUpdate, onBeforeUpdate,
onEffectCleanup,
onUpdated, onUpdated,
ref, ref,
renderEffect, renderEffect,

View File

@ -29,12 +29,15 @@ export {
// effect // effect
stop, stop,
ReactiveEffect, ReactiveEffect,
onWatcherCleanup, onEffectCleanup,
// effect scope // effect scope
effectScope, effectScope,
EffectScope, EffectScope,
getCurrentScope, getCurrentScope,
onScopeDispose, onScopeDispose,
// baseWatch
onWatcherCleanup,
getCurrentWatcher,
} from '@vue/reactivity' } from '@vue/reactivity'
export { nextTick } from './scheduler' export { nextTick } from './scheduler'

View File

@ -213,16 +213,6 @@ export const createVaporPreScheduler: SchedulerFactory =
} }
} }
export const createVaporRenderingScheduler: SchedulerFactory =
instance => (job, effect, immediateFirstRun, hasCb) => {
if (!immediateFirstRun) {
if (instance) job.id = instance.uid
queueJob(job)
} else if (!hasCb) {
effect.run()
}
}
export const createVaporPostScheduler: SchedulerFactory = export const createVaporPostScheduler: SchedulerFactory =
instance => (job, effect, immediateFirstRun, hasCb) => { instance => (job, effect, immediateFirstRun, hasCb) => {
if (!immediateFirstRun) { if (!immediateFirstRun) {