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 {

src/index.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import Vue, { VueConstructor } from 'vue'
1+
import type Vue from 'vue'
22
import { Data, SetupFunction } from './component'
3-
import { install } from './install'
4-
import { mixin } from './setup'
3+
import { Plugin } from './install'
54

6-
declare module 'vue/types/options' {
7-
interface ComponentOptions<V extends Vue> {
8-
setup?: SetupFunction<Data, Data>
9-
}
10-
}
11-
12-
const VueCompositionAPI = {
13-
install: (Vue: VueConstructor) => install(Vue, mixin),
14-
}
15-
16-
export default VueCompositionAPI
17-
export { createApp } from './createApp'
18-
export { nextTick } from './nextTick'
19-
export { createElement as h } from './createElement'
5+
export * from './apis'
206
export { getCurrentInstance } from './runtimeContext'
217
export {
228
defineComponent,
@@ -25,14 +11,15 @@ export {
2511
PropOptions,
2612
SetupContext,
2713
} from './component'
14+
export default Plugin
2815

29-
export * from './apis/state'
30-
export * from './apis/lifecycle'
31-
export * from './apis/watch'
32-
export * from './apis/computed'
33-
export * from './apis/inject'
16+
declare module 'vue/types/options' {
17+
interface ComponentOptions<V extends Vue> {
18+
setup?: SetupFunction<Data, Data>
19+
}
20+
}
3421

3522
// auto install when using CDN
3623
if (typeof window !== 'undefined' && window.Vue) {
37-
window.Vue.use(VueCompositionAPI)
24+
window.Vue.use(Plugin)
3825
}

src/install.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { VueConstructor } from 'vue'
12
import { AnyObject } from './types/basic'
23
import { hasSymbol, hasOwn, isPlainObject, assert } from './utils'
34
import { isRef } from './reactivity'
4-
import { setCurrentVue, currentVue } from './runtimeContext'
5-
import { VueConstructor } from 'vue'
5+
import { setVueConstructor, isVueRegistered } from './runtimeContext'
6+
import { mixin } from './mixin'
67

78
/**
89
* Helper that recursively merges two data objects together.
@@ -38,11 +39,8 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
3839
return to
3940
}
4041

41-
export function install(
42-
Vue: VueConstructor,
43-
_install: (Vue: VueConstructor) => void
44-
) {
45-
if (currentVue && currentVue === Vue) {
42+
export function install(Vue: VueConstructor) {
43+
if (isVueRegistered()) {
4644
if (__DEV__) {
4745
assert(
4846
false,
@@ -73,6 +71,10 @@ export function install(
7371
}
7472
}
7573

76-
setCurrentVue(Vue)
77-
_install(Vue)
74+
setVueConstructor(Vue)
75+
mixin(Vue)
76+
}
77+
78+
export const Plugin = {
79+
install: (Vue: VueConstructor) => install(Vue),
7880
}

src/setup.ts renamed to src/mixin.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
import { VueConstructor } from 'vue'
1+
import type { VueConstructor } from 'vue'
22
import {
33
ComponentInstance,
44
SetupContext,
55
SetupFunction,
66
Data,
77
} from './component'
8-
import { Ref, isRef, isReactive, markRaw } from './reactivity'
9-
import { getCurrentInstance, setCurrentVM } from './runtimeContext'
10-
import { resolveSlots, createSlotProxy } from './helper'
11-
import { hasOwn, isPlainObject, assert, proxy, warn, isFunction } from './utils'
12-
import { ref } from './apis/state'
13-
import vmStateManager from './vmStateManager'
14-
import { unwrapRefProxy } from './reactivity/unwrap'
15-
import { markReactive } from './reactivity/reactive'
8+
import {
9+
Ref,
10+
isRef,
11+
isReactive,
12+
markRaw,
13+
unwrapRefProxy,
14+
markReactive,
15+
} from './reactivity'
16+
import { getCurrentInstance, setCurrentInstance } from './runtimeContext'
17+
import {
18+
resolveSlots,
19+
createSlotProxy,
20+
hasOwn,
21+
isPlainObject,
22+
assert,
23+
proxy,
24+
warn,
25+
isFunction,
26+
} from './utils'
27+
import { ref } from './apis'
28+
import vmStateManager from './utils/vmStateManager'
1629

1730
function asVmProperty(
1831
vm: ComponentInstance,
@@ -83,11 +96,11 @@ function resolveScopedSlots(
8396
vm: ComponentInstance,
8497
slotsProxy: { [x: string]: Function }
8598
): void {
86-
const parentVode = (vm.$options as any)._parentVnode
87-
if (!parentVode) return
99+
const parentVNode = (vm.$options as any)._parentVnode
100+
if (!parentVNode) return
88101

89102
const prevSlots = vmStateManager.get(vm, 'slots') || []
90-
const curSlots = resolveSlots(parentVode.data.scopedSlots, vm.$slots)
103+
const curSlots = resolveSlots(parentVNode.data.scopedSlots, vm.$slots)
91104
// remove staled slots
92105
for (let index = 0; index < prevSlots.length; index++) {
93106
const key = prevSlots[index]
@@ -113,7 +126,7 @@ function activateCurrentInstance(
113126
onError?: (err: Error) => void
114127
) {
115128
let preVm = getCurrentInstance()
116-
setCurrentVM(vm)
129+
setCurrentInstance(vm)
117130
try {
118131
return fn(vm)
119132
} catch (err) {
@@ -123,7 +136,7 @@ function activateCurrentInstance(
123136
throw err
124137
}
125138
} finally {
126-
setCurrentVM(preVm)
139+
setCurrentInstance(preVm)
127140
}
128141
}
129142

src/reactivity/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
shallowReactive,
66
toRaw,
77
isRaw,
8+
markReactive,
89
} from './reactive'
910
export {
1011
ref,
@@ -19,3 +20,4 @@ export {
1920
triggerRef,
2021
} from './ref'
2122
export { set } from './set'
23+
export { unwrapRefProxy } from './unwrap'

0 commit comments

Comments
 (0)