Skip to content

Commit 31d85d4

Browse files
committed
refactor: rename variables
1 parent 788db36 commit 31d85d4

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

src/apis/inject.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ComponentInstance } from '../component'
2-
import { hasOwn, warn, currentVMInFn, isFunction, proxy } from '../utils'
2+
import {
3+
hasOwn,
4+
warn,
5+
getCurrentInstanceForFn,
6+
isFunction,
7+
proxy,
8+
} from '../utils'
39
import { getCurrentInstance } from '../runtimeContext'
410

511
const NOT_FOUND = {}
@@ -23,7 +29,7 @@ function resolveInject(
2329
}
2430

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

2935
if (!vm._provided) {

src/apis/lifecycle.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
import { VueConstructor } from 'vue'
2-
import { ComponentInstance } from '../component'
32
import {
43
getVueConstructor,
54
setCurrentInstance,
65
getCurrentInstance,
76
ComponentInternalInstance,
8-
setCurrentVue2Instance,
97
} from '../runtimeContext'
10-
import { currentVMInFn } from '../utils/helper'
8+
import { getCurrentInstanceForFn } from '../utils/helper'
119

1210
const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`
1311
function createLifeCycle(lifeCyclehook: string) {
1412
return (callback: Function, target?: ComponentInternalInstance | null) => {
15-
const vm = currentVMInFn(genName(lifeCyclehook), target)
13+
const instance = getCurrentInstanceForFn(genName(lifeCyclehook), target)
1614
return (
17-
vm && injectHookOption(getVueConstructor(), vm, lifeCyclehook, callback)
15+
instance &&
16+
injectHookOption(getVueConstructor(), instance, lifeCyclehook, callback)
1817
)
1918
}
2019
}
2120

2221
function injectHookOption(
2322
Vue: VueConstructor,
24-
vm: ComponentInstance,
23+
instance: ComponentInternalInstance,
2524
hook: string,
2625
val: Function
2726
) {
28-
const options = vm.$options as Record<string, unknown>
27+
const options = instance.proxy.$options as Record<string, unknown>
2928
const mergeFn = Vue.config.optionMergeStrategies[hook]
30-
const wrappedHook = wrapHookCall(vm, val)
29+
const wrappedHook = wrapHookCall(instance, val)
3130
options[hook] = mergeFn(options[hook], wrappedHook)
3231
return wrappedHook
3332
}
3433

35-
function wrapHookCall(vm: ComponentInstance, fn: Function): Function {
34+
function wrapHookCall(
35+
instance: ComponentInternalInstance,
36+
fn: Function
37+
): Function {
3638
return (...args: any) => {
37-
let preVm = getCurrentInstance()
38-
setCurrentVue2Instance(vm)
39+
let prev = getCurrentInstance()
40+
setCurrentInstance(instance)
3941
try {
4042
return fn(...args)
4143
} finally {
42-
setCurrentInstance(preVm)
44+
setCurrentInstance(prev)
4345
}
4446
}
4547
}
4648

47-
// export const onCreated = createLifeCycle('created');
4849
export const onBeforeMount = createLifeCycle('beforeMount')
4950
export const onMounted = createLifeCycle('mounted')
5051
export const onBeforeUpdate = createLifeCycle('beforeUpdate')

src/runtimeContext.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ export function setCurrentVue2Instance(vm: ComponentInstance | null) {
9999
setCurrentInstance(vm ? toVue3ComponentInstance(vm) : vm)
100100
}
101101

102-
export function setCurrentInstance(vm: ComponentInternalInstance | null) {
102+
export function setCurrentInstance(instance: ComponentInternalInstance | null) {
103103
if (!currentInstanceTracking) return
104104
const prev = currentInstance
105105
prev?.scope.off()
106-
currentInstance = vm
106+
currentInstance = instance
107107
currentInstance?.scope.on()
108108
}
109109

@@ -191,19 +191,19 @@ const instanceMapCache = new WeakMap<
191191
>()
192192

193193
function toVue3ComponentInstance(
194-
vue2Instance: ComponentInstance
194+
vm: ComponentInstance
195195
): ComponentInternalInstance {
196-
if (instanceMapCache.has(vue2Instance)) {
197-
return instanceMapCache.get(vue2Instance)!
196+
if (instanceMapCache.has(vm)) {
197+
return instanceMapCache.get(vm)!
198198
}
199199

200200
const instance: ComponentInternalInstance = {
201-
proxy: vue2Instance,
202-
update: vue2Instance.$forceUpdate,
203-
uid: vue2Instance._uid,
201+
proxy: vm,
202+
update: vm.$forceUpdate,
203+
uid: vm._uid,
204204

205205
// $emit is defined on prototype and it expected to be bound
206-
emit: vue2Instance.$emit.bind(vue2Instance),
206+
emit: vm.$emit.bind(vm),
207207

208208
parent: null,
209209
root: null!, // to be immediately set
@@ -224,47 +224,47 @@ function toVue3ComponentInstance(
224224
instanceProps.forEach((prop) => {
225225
proxy(instance, prop, {
226226
get() {
227-
return (vue2Instance as any)[`$${prop}`]
227+
return (vm as any)[`$${prop}`]
228228
},
229229
})
230230
})
231231

232232
proxy(instance, 'isMounted', {
233233
get() {
234234
// @ts-expect-error private api
235-
return vue2Instance._isMounted
235+
return vm._isMounted
236236
},
237237
})
238238

239239
proxy(instance, 'isUnmounted', {
240240
get() {
241241
// @ts-expect-error private api
242-
return vue2Instance._isDestroyed
242+
return vm._isDestroyed
243243
},
244244
})
245245

246246
proxy(instance, 'isDeactivated', {
247247
get() {
248248
// @ts-expect-error private api
249-
return vue2Instance._inactive
249+
return vm._inactive
250250
},
251251
})
252252

253253
proxy(instance, 'emitted', {
254254
get() {
255255
// @ts-expect-error private api
256-
return vue2Instance._events
256+
return vm._events
257257
},
258258
})
259259

260-
instanceMapCache.set(vue2Instance, instance)
260+
instanceMapCache.set(vm, instance)
261261

262-
if (vue2Instance.$parent) {
263-
instance.parent = toVue3ComponentInstance(vue2Instance.$parent)
262+
if (vm.$parent) {
263+
instance.parent = toVue3ComponentInstance(vm.$parent)
264264
}
265265

266-
if (vue2Instance.$root) {
267-
instance.root = toVue3ComponentInstance(vue2Instance.$root)
266+
if (vm.$root) {
267+
instance.root = toVue3ComponentInstance(vm.$root)
268268
}
269269

270270
return instance

src/utils/helper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
} from '../runtimeContext'
88
import { warn } from './utils'
99

10-
export function currentVMInFn(
10+
export function getCurrentInstanceForFn(
1111
hook: string,
1212
target?: ComponentInternalInstance | null
13-
): ComponentInstance | undefined {
13+
): ComponentInternalInstance | null {
1414
target = target || getCurrentInstance()
1515
if (__DEV__ && !target) {
1616
warn(
@@ -19,7 +19,7 @@ export function currentVMInFn(
1919
`Lifecycle injection APIs can only be used during execution of setup().`
2020
)
2121
}
22-
return target?.proxy
22+
return target
2323
}
2424

2525
export function defineComponentInstance<V extends Vue = Vue>(

0 commit comments

Comments
 (0)