Преглед изворни кода

refactor(reactivity): remove middleware (#156)

Rizumu Ayaka пре 2 година
родитељ
комит
2661cb2474

+ 0 - 83
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',
-    ])
-  })
 })

+ 24 - 38
packages/reactivity/src/baseWatch.ts

@@ -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,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 {

+ 0 - 1
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'

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

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

+ 4 - 1
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'

+ 0 - 10
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) {