From 2661cb24748d410fc993191fbecc78647cddf5b0 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Mon, 18 Mar 2024 21:57:18 +0800 Subject: [PATCH] refactor(reactivity): remove middleware (#156) --- .../reactivity/__tests__/baseWatch.spec.ts | 83 ------------------- packages/reactivity/src/baseWatch.ts | 62 ++++++-------- packages/reactivity/src/index.ts | 1 - .../__tests__/renderEffect.spec.ts | 2 +- packages/runtime-vapor/src/index.ts | 5 +- packages/runtime-vapor/src/scheduler.ts | 10 --- 6 files changed, 29 insertions(+), 134 deletions(-) diff --git a/packages/reactivity/__tests__/baseWatch.spec.ts b/packages/reactivity/__tests__/baseWatch.spec.ts index 9a46c3a29..16d990982 100644 --- a/packages/reactivity/__tests__/baseWatch.spec.ts +++ b/packages/reactivity/__tests__/baseWatch.spec.ts @@ -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', - ]) - }) }) diff --git a/packages/reactivity/src/baseWatch.ts b/packages/reactivity/src/baseWatch.ts index a59c86eb9..9fbe56c06 100644 --- a/packages/reactivity/src/baseWatch.ts +++ b/packages/reactivity/src/baseWatch.ts @@ -47,7 +47,6 @@ export interface BaseWatchOptions 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,38 +246,31 @@ 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() - } - const currentWatcher = activeWatcher - activeWatcher = effect - try { - callWithAsyncErrorHandling( - cb!, - onError, - BaseWatchErrorCodes.WATCH_CALLBACK, - [ - newValue, - // pass undefined as the old value when it's changed for the first time - oldValue === INITIAL_WATCHER_VALUE - ? undefined - : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE - ? [] - : oldValue, - onWatcherCleanup, - ], - ) - oldValue = newValue - } finally { - activeWatcher = currentWatcher - } + // cleanup before running cb again + if (cleanup) { + cleanup() } - if (middleware) { - middleware(next) - } else { - next() + const currentWatcher = activeWatcher + activeWatcher = effect + try { + callWithAsyncErrorHandling( + cb!, + onError, + BaseWatchErrorCodes.WATCH_CALLBACK, + [ + newValue, + // pass undefined as the old value when it's changed for the first time + oldValue === INITIAL_WATCHER_VALUE + ? undefined + : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE + ? [] + : oldValue, + onWatcherCleanup, + ], + ) + oldValue = newValue + } finally { + activeWatcher = currentWatcher } } } else { diff --git a/packages/reactivity/src/index.ts b/packages/reactivity/src/index.ts index c16d347d9..1343865a5 100644 --- a/packages/reactivity/src/index.ts +++ b/packages/reactivity/src/index.ts @@ -85,7 +85,6 @@ export { onWatcherCleanup, BaseWatchErrorCodes, type BaseWatchOptions, - type BaseWatchMiddleware, type WatchScheduler, } from './baseWatch' export { type SchedulerJob, SchedulerJobFlags } from './scheduler' diff --git a/packages/runtime-vapor/__tests__/renderEffect.spec.ts b/packages/runtime-vapor/__tests__/renderEffect.spec.ts index cdf974e53..aa1c91a18 100644 --- a/packages/runtime-vapor/__tests__/renderEffect.spec.ts +++ b/packages/runtime-vapor/__tests__/renderEffect.spec.ts @@ -1,7 +1,7 @@ -import { onEffectCleanup } from '@vue/reactivity' import { nextTick, onBeforeUpdate, + onEffectCleanup, onUpdated, ref, renderEffect, diff --git a/packages/runtime-vapor/src/index.ts b/packages/runtime-vapor/src/index.ts index 03d79a3c7..dd3d7fa7a 100644 --- a/packages/runtime-vapor/src/index.ts +++ b/packages/runtime-vapor/src/index.ts @@ -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' diff --git a/packages/runtime-vapor/src/scheduler.ts b/packages/runtime-vapor/src/scheduler.ts index 8f7bbb4ce..f17f88ec7 100644 --- a/packages/runtime-vapor/src/scheduler.ts +++ b/packages/runtime-vapor/src/scheduler.ts @@ -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) {