Skip to content

Commit a24951a

Browse files
authored
refactor: internal structures (#426)
1 parent 43d8732 commit a24951a

24 files changed

+151
-123
lines changed

src/apis/computed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getCurrentVue, getCurrentInstance } from '../runtimeContext'
1+
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
22
import { createRef, Ref } from '../reactivity'
3-
import { defineComponentInstance } from '../helper'
3+
import { defineComponentInstance } from '../utils/helper'
44
import { warn } from '../utils'
55

66
interface Option<T> {
@@ -31,7 +31,7 @@ export function computed<T>(
3131
set = options.set
3232
}
3333

34-
const computedHost = defineComponentInstance(getCurrentVue(), {
34+
const computedHost = defineComponentInstance(getVueConstructor(), {
3535
computed: {
3636
$$state: {
3737
get,

src/createApp.ts renamed to src/apis/createApp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type Vue from 'vue'
22
import { VueConstructor } from 'vue/types/umd'
3-
import { getCurrentVue } from './runtimeContext'
4-
import { warn } from './utils'
3+
import { getVueConstructor } from '../runtimeContext'
4+
import { warn } from '../utils'
55

66
export interface App {
77
config: VueConstructor['config']
@@ -14,7 +14,7 @@ export interface App {
1414
}
1515

1616
export function createApp(rootComponent: any, rootProps: any = undefined): App {
17-
const V = getCurrentVue()!
17+
const V = getVueConstructor()!
1818

1919
let mountedVM: Vue | undefined = undefined
2020

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import Vue from 'vue'
2-
import { currentVM, getCurrentVue } from './runtimeContext'
3-
import { defineComponentInstance } from './helper'
4-
import { warn } from './utils'
2+
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
3+
import { defineComponentInstance } from '../utils/helper'
4+
import { warn } from '../utils'
55

66
type CreateElement = Vue['$createElement']
77

88
let fallbackCreateElement: CreateElement
99

1010
export const createElement = (function createElement(...args: any) {
11-
if (!currentVM) {
11+
const instance = getCurrentInstance()
12+
if (!instance) {
1213
warn('`createElement()` has been called outside of render function.')
1314
if (!fallbackCreateElement) {
14-
fallbackCreateElement = defineComponentInstance(getCurrentVue())
15+
fallbackCreateElement = defineComponentInstance(getVueConstructor())
1516
.$createElement
1617
}
1718

1819
return fallbackCreateElement.apply(fallbackCreateElement, args)
1920
}
2021

21-
return currentVM.$createElement.apply(currentVM, args)
22+
return instance.$createElement.apply(instance, args)
2223
} as any) as CreateElement

src/apis/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from './state'
2+
export * from './lifecycle'
3+
export * from './watch'
4+
export * from './computed'
5+
export * from './inject'
6+
export { createApp } from './createApp'
7+
export { nextTick } from './nextTick'
8+
export { createElement as h } from './createElement'

src/apis/inject.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ComponentInstance } from '../component'
2-
import { currentVMInFn } from '../helper'
3-
import { hasOwn, warn } from '../utils'
2+
import { hasOwn, warn, currentVMInFn } from '../utils'
43
import { getCurrentInstance } from '../runtimeContext'
54

65
const NOT_FOUND = {}

src/apis/lifecycle.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { VueConstructor } from 'vue'
22
import { ComponentInstance } from '../component'
33
import {
4-
getCurrentVue,
5-
setCurrentVM,
4+
getVueConstructor,
5+
setCurrentInstance,
66
getCurrentInstance,
77
} from '../runtimeContext'
8-
import { currentVMInFn } from '../helper'
8+
import { currentVMInFn } from '../utils/helper'
99

1010
const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`
1111
function createLifeCycle(lifeCyclehook: string) {
1212
return (callback: Function) => {
1313
const vm = currentVMInFn(genName(lifeCyclehook))
1414
if (vm) {
15-
injectHookOption(getCurrentVue(), vm, lifeCyclehook, callback)
15+
injectHookOption(getVueConstructor(), vm, lifeCyclehook, callback)
1616
}
1717
}
1818
}
@@ -31,11 +31,11 @@ function injectHookOption(
3131
function wrapHookCall(vm: ComponentInstance, fn: Function) {
3232
return (...args: any) => {
3333
let preVm = getCurrentInstance()
34-
setCurrentVM(vm)
34+
setCurrentInstance(vm)
3535
try {
3636
return fn(...args)
3737
} finally {
38-
setCurrentVM(preVm)
38+
setCurrentInstance(preVm)
3939
}
4040
}
4141
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Vue from 'vue'
2-
import { currentVue } from './runtimeContext'
2+
import { getVueConstructor } from '../runtimeContext'
33

44
type NextTick = Vue['$nextTick']
55

66
export const nextTick: NextTick = function nextTick(
77
this: ThisType<NextTick>,
88
...args: Parameters<NextTick>
99
) {
10-
return currentVue?.nextTick.apply(this, args)
10+
return getVueConstructor()?.nextTick.apply(this, args)
1111
} as any

src/apis/state.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
export {
2+
isReactive,
3+
isRef,
4+
markRaw,
5+
markReactive,
26
reactive,
37
ref,
48
Ref,
5-
isRef,
6-
toRefs,
79
set,
8-
toRef,
9-
isReactive,
10-
UnwrapRef,
11-
markRaw,
12-
unref,
1310
shallowReactive,
14-
toRaw,
1511
shallowRef,
12+
toRaw,
13+
toRef,
14+
toRefs,
1615
triggerRef,
16+
unref,
17+
UnwrapRef,
1718
} from '../reactivity'

src/apis/watch.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { ComponentInstance } from '../component'
22
import { Ref, isRef, isReactive } from '../reactivity'
33
import { assert, logError, noopFn, warn, isFunction } from '../utils'
4-
import { defineComponentInstance } from '../helper'
5-
import { getCurrentInstance, getCurrentVue } from '../runtimeContext'
6-
import { WatcherPreFlushQueueKey, WatcherPostFlushQueueKey } from '../symbols'
4+
import { defineComponentInstance } from '../utils/helper'
5+
import { getCurrentInstance, getVueConstructor } from '../runtimeContext'
6+
import {
7+
WatcherPreFlushQueueKey,
8+
WatcherPostFlushQueueKey,
9+
} from '../utils/symbols'
710
import { ComputedRef } from './computed'
811

912
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void
@@ -98,7 +101,7 @@ function getWatcherVM() {
98101
let vm = getCurrentInstance()
99102
if (!vm) {
100103
if (!fallbackVM) {
101-
fallbackVM = defineComponentInstance(getCurrentVue())
104+
fallbackVM = defineComponentInstance(getVueConstructor())
102105
}
103106
vm = fallbackVM
104107
} else if (!hasWatchEnv(vm)) {

src/env.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VueConstructor } from 'vue'
2-
import { VfaState } from './vmStateManager'
2+
import { VfaState } from './utils/vmStateManager'
33
import { VueWatcher } from './apis/watch'
44

55
declare global {
@@ -13,7 +13,7 @@ declare module 'vue/types/vue' {
1313
readonly _uid: number
1414
readonly _data: Record<string, any>
1515
_watchers: VueWatcher[]
16-
__secret_vfa_state__?: VfaState
16+
__composition_api_state__?: VfaState
1717
}
1818

1919
interface VueConstructor {

0 commit comments

Comments
 (0)