Przeglądaj źródła

refactor: simplify code

三咲智子 Kevin Deng 2 lat temu
rodzic
commit
5c0b8bbeb3

+ 1 - 0
packages/reactivity/src/index.ts

@@ -78,4 +78,5 @@ export {
   type BaseWatchOptions,
   type BaseWatchMiddleware,
   type Scheduler,
+  type SchedulerJob,
 } from './baseWatch'

+ 2 - 2
packages/runtime-vapor/src/index.ts

@@ -39,15 +39,15 @@ export {
 } from '@vue/reactivity'
 export { withModifiers, withKeys } from '@vue/runtime-dom'
 
+export { nextTick } from './scheduler'
+export { getCurrentInstance, type ComponentInternalInstance } from './component'
 export * from './on'
 export * from './render'
 export * from './renderWatch'
 export * from './template'
-export * from './scheduler'
 export * from './apiWatch'
 export * from './directive'
 export * from './dom'
 export * from './directives/vShow'
 export * from './apiLifecycle'
-export { getCurrentInstance, type ComponentInternalInstance } from './component'
 export * from './if'

+ 1 - 12
packages/runtime-vapor/src/render.ts

@@ -16,11 +16,6 @@ export type ParentBlock = ParentNode | Node[]
 export type Fragment = { nodes: Block; anchor: Node }
 export type BlockFn = (props?: any) => Block
 
-let isRenderingActivity = false
-export function getIsRendering() {
-  return isRenderingActivity
-}
-
 export function render(
   comp: Component,
   props: Data,
@@ -55,13 +50,7 @@ export function mountComponent(
     let block: Block | null = null
     if (state && '__isScriptSetup' in state) {
       instance.setupState = proxyRefs(state)
-      const currentlyRenderingActivity = isRenderingActivity
-      isRenderingActivity = true
-      try {
-        block = component.render(instance.setupState)
-      } finally {
-        isRenderingActivity = currentlyRenderingActivity
-      }
+      block = component.render(instance.setupState)
     } else {
       block = state as Block
     }

+ 6 - 7
packages/runtime-vapor/src/renderWatch.ts

@@ -52,13 +52,12 @@ function doWatch(
   // if (__SSR__) {}
 
   const instance = getCurrentInstance()
-
-  extendOptions.onError = (err: unknown, type: BaseWatchErrorCodes) =>
-    handleErrorWithInstance(err, instance, type)
-  extendOptions.scheduler = createVaporRenderingScheduler(instance)
-
-  extendOptions.middleware = createMiddleware(instance)
-
+  extend(extendOptions, {
+    onError: (err: unknown, type: BaseWatchErrorCodes) =>
+      handleErrorWithInstance(err, instance, type),
+    scheduler: createVaporRenderingScheduler(instance),
+    middleware: createMiddleware(instance),
+  })
   let effect = baseWatch(source, cb, extendOptions)
 
   const unwatch = !effect

+ 3 - 33
packages/runtime-vapor/src/scheduler.ts

@@ -1,38 +1,8 @@
-import type { Scheduler } from '@vue/reactivity'
+import type { Scheduler, SchedulerJob } from '@vue/reactivity'
 import type { ComponentInternalInstance } from './component'
 import { isArray } from '@vue/shared'
 
-export interface SchedulerJob extends Function {
-  id?: number
-  pre?: boolean
-  active?: boolean
-  computed?: boolean
-  /**
-   * Indicates whether the effect is allowed to recursively trigger itself
-   * when managed by the scheduler.
-   *
-   * By default, a job cannot trigger itself because some built-in method calls,
-   * e.g. Array.prototype.push actually performs reads as well (#1740) which
-   * can lead to confusing infinite loops.
-   * The allowed cases are component update functions and watch callbacks.
-   * Component update functions may update child component props, which in turn
-   * trigger flush: "pre" watch callbacks that mutates state that the parent
-   * relies on (#1801). Watch callbacks doesn't track its dependencies so if it
-   * triggers itself again, it's likely intentional and it is the user's
-   * responsibility to perform recursive state mutation that eventually
-   * stabilizes (#1727).
-   */
-  allowRecurse?: boolean
-  /**
-   * Attached by renderer.ts when setting up a component's render effect
-   * Used to obtain component information when reporting max recursive updates.
-   * dev only.
-   */
-  ownerInstance?: ComponentInternalInstance
-}
-
 export type SchedulerJobs = SchedulerJob | SchedulerJob[]
-
 export type QueueEffect = (
   cb: SchedulerJobs,
   suspense: ComponentInternalInstance | null,
@@ -210,7 +180,7 @@ export type SchedulerFactory = (
 ) => Scheduler
 
 export const createVaporSyncScheduler: SchedulerFactory =
-  (instance) => (job, effect, isInit) => {
+  () => (job, effect, isInit) => {
     if (isInit) {
       effect.run()
     } else {
@@ -241,7 +211,7 @@ export const createVaporRenderingScheduler: SchedulerFactory =
   }
 
 export const createVaporPostScheduler: SchedulerFactory =
-  (instance) => (job, effect, isInit) => {
+  () => (job, effect, isInit) => {
     if (isInit) {
       queuePostRenderEffect(effect.run.bind(effect))
     } else {