Skip to content

Commit

Permalink
refactor: rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 9, 2021
1 parent 788db36 commit 31d85d4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
10 changes: 8 additions & 2 deletions 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 = {}
Expand All @@ -23,7 +29,7 @@ function resolveInject(
}

export function provide<T>(key: InjectionKey<T> | string, value: T): void {
const vm: any = currentVMInFn('provide')
const vm: any = getCurrentInstanceForFn('provide')?.proxy
if (!vm) return

if (!vm._provided) {
Expand Down
27 changes: 14 additions & 13 deletions 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<string, unknown>
const options = instance.proxy.$options as Record<string, unknown>
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')
Expand Down
38 changes: 19 additions & 19 deletions src/runtimeContext.ts
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
Expand All @@ -224,47 +224,47 @@ function toVue3ComponentInstance(
instanceProps.forEach((prop) => {
proxy(instance, prop, {
get() {
return (vue2Instance as any)[`$${prop}`]
return (vm as any)[`$${prop}`]
},
})
})

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
Expand Down
6 changes: 3 additions & 3 deletions src/utils/helper.ts
Expand Up @@ -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(
Expand All @@ -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<V extends Vue = Vue>(
Expand Down

0 comments on commit 31d85d4

Please sign in to comment.