]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf: use class for SetupContext
authorEvan You <evan@vuejs.org>
Sun, 1 Dec 2024 09:00:38 +0000 (17:00 +0800)
committerEvan You <evan@vuejs.org>
Sun, 1 Dec 2024 09:37:49 +0000 (17:37 +0800)
packages/runtime-vapor/src/component.ts

index 1cdf4f33429ba7eca9ab9dd010dbc74d49611fcb..89f8ccc881aadec891bf3c6fa39d33b127d9df9f 100644 (file)
@@ -34,42 +34,25 @@ export type FunctionalComponent = SetupFn &
     displayName?: string
   }
 
-export type SetupContext<E = EmitsOptions> = E extends any
-  ? {
-      attrs: Data
-      emit: EmitFn<E>
-      expose: (exposed?: Record<string, any>) => void
-      slots: Readonly<StaticSlots>
+export class SetupContext<E = EmitsOptions> {
+  attrs: Data
+  emit: EmitFn<E>
+  slots: Readonly<StaticSlots>
+  expose: (exposed?: Record<string, any>) => void
+
+  constructor(instance: ComponentInternalInstance) {
+    this.attrs = instance.attrs
+    this.emit = instance.emit as EmitFn<E>
+    this.slots = instance.slots
+    this.expose = (exposed = {}) => {
+      instance.exposed = exposed
     }
-  : never
+  }
+}
 
 export function createSetupContext(
   instance: ComponentInternalInstance,
 ): SetupContext {
-  const expose: SetupContext['expose'] = exposed => {
-    if (__DEV__) {
-      if (instance.exposed) {
-        warn(`expose() should be called only once per setup().`)
-      }
-      if (exposed != null) {
-        let exposedType: string = typeof exposed
-        if (exposedType === 'object') {
-          if (isArray(exposed)) {
-            exposedType = 'array'
-          } else if (isRef(exposed)) {
-            exposedType = 'ref'
-          }
-        }
-        if (exposedType !== 'object') {
-          warn(
-            `expose() should be passed a plain object, received ${exposedType}.`,
-          )
-        }
-      }
-    }
-    instance.exposed = exposed || {}
-  }
-
   if (__DEV__) {
     // We use getters in dev in case libs like test-utils overwrite instance
     // properties (overwrites should not be done in prod)
@@ -83,15 +66,30 @@ export function createSetupContext(
       get emit() {
         return (event: string, ...args: any[]) => instance.emit(event, ...args)
       },
-      expose,
-    })
+      expose: (exposed?: Record<string, any>) => {
+        if (instance.exposed) {
+          warn(`expose() should be called only once per setup().`)
+        }
+        if (exposed != null) {
+          let exposedType: string = typeof exposed
+          if (exposedType === 'object') {
+            if (isArray(exposed)) {
+              exposedType = 'array'
+            } else if (isRef(exposed)) {
+              exposedType = 'ref'
+            }
+          }
+          if (exposedType !== 'object') {
+            warn(
+              `expose() should be passed a plain object, received ${exposedType}.`,
+            )
+          }
+        }
+        instance.exposed = exposed || {}
+      },
+    }) as SetupContext
   } else {
-    return {
-      attrs: instance.attrs,
-      emit: instance.emit,
-      slots: instance.slots,
-      expose,
-    }
+    return new SetupContext(instance)
   }
 }