From 31d85d419ab95fef5551087490adf2346df3f6df Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 9 Aug 2021 20:59:27 +0800 Subject: [PATCH] refactor: rename variables --- src/apis/inject.ts | 10 ++++++++-- src/apis/lifecycle.ts | 27 ++++++++++++++------------- src/runtimeContext.ts | 38 +++++++++++++++++++------------------- src/utils/helper.ts | 6 +++--- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/apis/inject.ts b/src/apis/inject.ts index 75b53013..a8feaf2d 100644 --- a/src/apis/inject.ts +++ b/src/apis/inject.ts @@ -1,5 +1,11 @@ import { ComponentInstance } from '../component' -import { hasOwn, warn, currentVMInFn, isFunction, proxy } from '../utils' +import { + hasOwn, + warn, + getCurrentInstanceForFn, + isFunction, + proxy, +} from '../utils' import { getCurrentInstance } from '../runtimeContext' const NOT_FOUND = {} @@ -23,7 +29,7 @@ function resolveInject( } export function provide(key: InjectionKey | string, value: T): void { - const vm: any = currentVMInFn('provide') + const vm: any = getCurrentInstanceForFn('provide')?.proxy if (!vm) return if (!vm._provided) { diff --git a/src/apis/lifecycle.ts b/src/apis/lifecycle.ts index 94bab294..3bb49e3a 100644 --- a/src/apis/lifecycle.ts +++ b/src/apis/lifecycle.ts @@ -1,50 +1,51 @@ import { VueConstructor } from 'vue' -import { ComponentInstance } from '../component' import { getVueConstructor, setCurrentInstance, getCurrentInstance, ComponentInternalInstance, - setCurrentVue2Instance, } from '../runtimeContext' -import { currentVMInFn } from '../utils/helper' +import { getCurrentInstanceForFn } from '../utils/helper' const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}` function createLifeCycle(lifeCyclehook: string) { return (callback: Function, target?: ComponentInternalInstance | null) => { - const vm = currentVMInFn(genName(lifeCyclehook), target) + const instance = getCurrentInstanceForFn(genName(lifeCyclehook), target) return ( - vm && injectHookOption(getVueConstructor(), vm, lifeCyclehook, callback) + instance && + injectHookOption(getVueConstructor(), instance, lifeCyclehook, callback) ) } } function injectHookOption( Vue: VueConstructor, - vm: ComponentInstance, + instance: ComponentInternalInstance, hook: string, val: Function ) { - const options = vm.$options as Record + const options = instance.proxy.$options as Record const mergeFn = Vue.config.optionMergeStrategies[hook] - const wrappedHook = wrapHookCall(vm, val) + const wrappedHook = wrapHookCall(instance, val) options[hook] = mergeFn(options[hook], wrappedHook) return wrappedHook } -function wrapHookCall(vm: ComponentInstance, fn: Function): Function { +function wrapHookCall( + instance: ComponentInternalInstance, + fn: Function +): Function { return (...args: any) => { - let preVm = getCurrentInstance() - setCurrentVue2Instance(vm) + let prev = getCurrentInstance() + setCurrentInstance(instance) try { return fn(...args) } finally { - setCurrentInstance(preVm) + setCurrentInstance(prev) } } } -// export const onCreated = createLifeCycle('created'); export const onBeforeMount = createLifeCycle('beforeMount') export const onMounted = createLifeCycle('mounted') export const onBeforeUpdate = createLifeCycle('beforeUpdate') diff --git a/src/runtimeContext.ts b/src/runtimeContext.ts index 104f2efa..02afdedb 100644 --- a/src/runtimeContext.ts +++ b/src/runtimeContext.ts @@ -99,11 +99,11 @@ export function setCurrentVue2Instance(vm: ComponentInstance | null) { setCurrentInstance(vm ? toVue3ComponentInstance(vm) : vm) } -export function setCurrentInstance(vm: ComponentInternalInstance | null) { +export function setCurrentInstance(instance: ComponentInternalInstance | null) { if (!currentInstanceTracking) return const prev = currentInstance prev?.scope.off() - currentInstance = vm + currentInstance = instance currentInstance?.scope.on() } @@ -191,19 +191,19 @@ const instanceMapCache = new WeakMap< >() function toVue3ComponentInstance( - vue2Instance: ComponentInstance + vm: ComponentInstance ): ComponentInternalInstance { - if (instanceMapCache.has(vue2Instance)) { - return instanceMapCache.get(vue2Instance)! + if (instanceMapCache.has(vm)) { + return instanceMapCache.get(vm)! } const instance: ComponentInternalInstance = { - proxy: vue2Instance, - update: vue2Instance.$forceUpdate, - uid: vue2Instance._uid, + proxy: vm, + update: vm.$forceUpdate, + uid: vm._uid, // $emit is defined on prototype and it expected to be bound - emit: vue2Instance.$emit.bind(vue2Instance), + emit: vm.$emit.bind(vm), parent: null, root: null!, // to be immediately set @@ -224,7 +224,7 @@ function toVue3ComponentInstance( instanceProps.forEach((prop) => { proxy(instance, prop, { get() { - return (vue2Instance as any)[`$${prop}`] + return (vm as any)[`$${prop}`] }, }) }) @@ -232,39 +232,39 @@ function toVue3ComponentInstance( proxy(instance, 'isMounted', { get() { // @ts-expect-error private api - return vue2Instance._isMounted + return vm._isMounted }, }) proxy(instance, 'isUnmounted', { get() { // @ts-expect-error private api - return vue2Instance._isDestroyed + return vm._isDestroyed }, }) proxy(instance, 'isDeactivated', { get() { // @ts-expect-error private api - return vue2Instance._inactive + return vm._inactive }, }) proxy(instance, 'emitted', { get() { // @ts-expect-error private api - return vue2Instance._events + return vm._events }, }) - instanceMapCache.set(vue2Instance, instance) + instanceMapCache.set(vm, instance) - if (vue2Instance.$parent) { - instance.parent = toVue3ComponentInstance(vue2Instance.$parent) + if (vm.$parent) { + instance.parent = toVue3ComponentInstance(vm.$parent) } - if (vue2Instance.$root) { - instance.root = toVue3ComponentInstance(vue2Instance.$root) + if (vm.$root) { + instance.root = toVue3ComponentInstance(vm.$root) } return instance diff --git a/src/utils/helper.ts b/src/utils/helper.ts index c83dc7ce..e60c4758 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -7,10 +7,10 @@ import { } from '../runtimeContext' import { warn } from './utils' -export function currentVMInFn( +export function getCurrentInstanceForFn( hook: string, target?: ComponentInternalInstance | null -): ComponentInstance | undefined { +): ComponentInternalInstance | null { target = target || getCurrentInstance() if (__DEV__ && !target) { warn( @@ -19,7 +19,7 @@ export function currentVMInFn( `Lifecycle injection APIs can only be used during execution of setup().` ) } - return target?.proxy + return target } export function defineComponentInstance(