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

View File

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

View File

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

View File

@ -29,12 +29,15 @@ export {
// effect
stop,
ReactiveEffect,
onWatcherCleanup,
onEffectCleanup,
// effect scope
effectScope,
EffectScope,
getCurrentScope,
onScopeDispose,
// baseWatch
onWatcherCleanup,
getCurrentWatcher,
} from '@vue/reactivity'
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 =
instance => (job, effect, immediateFirstRun, hasCb) => {
if (!immediateFirstRun) {