Skip to content

refactor: internal structures #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apis/computed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCurrentVue, getCurrentInstance } from '../runtimeContext'
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
import { createRef, Ref } from '../reactivity'
import { defineComponentInstance } from '../helper'
import { defineComponentInstance } from '../utils/helper'
import { warn } from '../utils'

interface Option<T> {
Expand Down Expand Up @@ -31,7 +31,7 @@ export function computed<T>(
set = options.set
}

const computedHost = defineComponentInstance(getCurrentVue(), {
const computedHost = defineComponentInstance(getVueConstructor(), {
computed: {
$$state: {
get,
Expand Down
6 changes: 3 additions & 3 deletions src/createApp.ts → src/apis/createApp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type Vue from 'vue'
import { VueConstructor } from 'vue/types/umd'
import { getCurrentVue } from './runtimeContext'
import { warn } from './utils'
import { getVueConstructor } from '../runtimeContext'
import { warn } from '../utils'

export interface App {
config: VueConstructor['config']
Expand All @@ -14,7 +14,7 @@ export interface App {
}

export function createApp(rootComponent: any, rootProps: any = undefined): App {
const V = getCurrentVue()!
const V = getVueConstructor()!

let mountedVM: Vue | undefined = undefined

Expand Down
13 changes: 7 additions & 6 deletions src/createElement.ts → src/apis/createElement.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import Vue from 'vue'
import { currentVM, getCurrentVue } from './runtimeContext'
import { defineComponentInstance } from './helper'
import { warn } from './utils'
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
import { defineComponentInstance } from '../utils/helper'
import { warn } from '../utils'

type CreateElement = Vue['$createElement']

let fallbackCreateElement: CreateElement

export const createElement = (function createElement(...args: any) {
if (!currentVM) {
const instance = getCurrentInstance()
if (!instance) {
warn('`createElement()` has been called outside of render function.')
if (!fallbackCreateElement) {
fallbackCreateElement = defineComponentInstance(getCurrentVue())
fallbackCreateElement = defineComponentInstance(getVueConstructor())
.$createElement
}

return fallbackCreateElement.apply(fallbackCreateElement, args)
}

return currentVM.$createElement.apply(currentVM, args)
return instance.$createElement.apply(instance, args)
} as any) as CreateElement
8 changes: 8 additions & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './state'
export * from './lifecycle'
export * from './watch'
export * from './computed'
export * from './inject'
export { createApp } from './createApp'
export { nextTick } from './nextTick'
export { createElement as h } from './createElement'
3 changes: 1 addition & 2 deletions src/apis/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ComponentInstance } from '../component'
import { currentVMInFn } from '../helper'
import { hasOwn, warn } from '../utils'
import { hasOwn, warn, currentVMInFn } from '../utils'
import { getCurrentInstance } from '../runtimeContext'

const NOT_FOUND = {}
Expand Down
12 changes: 6 additions & 6 deletions src/apis/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { VueConstructor } from 'vue'
import { ComponentInstance } from '../component'
import {
getCurrentVue,
setCurrentVM,
getVueConstructor,
setCurrentInstance,
getCurrentInstance,
} from '../runtimeContext'
import { currentVMInFn } from '../helper'
import { currentVMInFn } from '../utils/helper'

const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`
function createLifeCycle(lifeCyclehook: string) {
return (callback: Function) => {
const vm = currentVMInFn(genName(lifeCyclehook))
if (vm) {
injectHookOption(getCurrentVue(), vm, lifeCyclehook, callback)
injectHookOption(getVueConstructor(), vm, lifeCyclehook, callback)
}
}
}
Expand All @@ -31,11 +31,11 @@ function injectHookOption(
function wrapHookCall(vm: ComponentInstance, fn: Function) {
return (...args: any) => {
let preVm = getCurrentInstance()
setCurrentVM(vm)
setCurrentInstance(vm)
try {
return fn(...args)
} finally {
setCurrentVM(preVm)
setCurrentInstance(preVm)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/nextTick.ts → src/apis/nextTick.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Vue from 'vue'
import { currentVue } from './runtimeContext'
import { getVueConstructor } from '../runtimeContext'

type NextTick = Vue['$nextTick']

export const nextTick: NextTick = function nextTick(
this: ThisType<NextTick>,
...args: Parameters<NextTick>
) {
return currentVue?.nextTick.apply(this, args)
return getVueConstructor()?.nextTick.apply(this, args)
} as any
17 changes: 9 additions & 8 deletions src/apis/state.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
export {
isReactive,
isRef,
markRaw,
markReactive,
reactive,
ref,
Ref,
isRef,
toRefs,
set,
toRef,
isReactive,
UnwrapRef,
markRaw,
unref,
shallowReactive,
toRaw,
shallowRef,
toRaw,
toRef,
toRefs,
triggerRef,
unref,
UnwrapRef,
} from '../reactivity'
11 changes: 7 additions & 4 deletions src/apis/watch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ComponentInstance } from '../component'
import { Ref, isRef, isReactive } from '../reactivity'
import { assert, logError, noopFn, warn, isFunction } from '../utils'
import { defineComponentInstance } from '../helper'
import { getCurrentInstance, getCurrentVue } from '../runtimeContext'
import { WatcherPreFlushQueueKey, WatcherPostFlushQueueKey } from '../symbols'
import { defineComponentInstance } from '../utils/helper'
import { getCurrentInstance, getVueConstructor } from '../runtimeContext'
import {
WatcherPreFlushQueueKey,
WatcherPostFlushQueueKey,
} from '../utils/symbols'
import { ComputedRef } from './computed'

export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void
Expand Down Expand Up @@ -98,7 +101,7 @@ function getWatcherVM() {
let vm = getCurrentInstance()
if (!vm) {
if (!fallbackVM) {
fallbackVM = defineComponentInstance(getCurrentVue())
fallbackVM = defineComponentInstance(getVueConstructor())
}
vm = fallbackVM
} else if (!hasWatchEnv(vm)) {
Expand Down
4 changes: 2 additions & 2 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VueConstructor } from 'vue'
import { VfaState } from './vmStateManager'
import { VfaState } from './utils/vmStateManager'
import { VueWatcher } from './apis/watch'

declare global {
Expand All @@ -13,7 +13,7 @@ declare module 'vue/types/vue' {
readonly _uid: number
readonly _data: Record<string, any>
_watchers: VueWatcher[]
__secret_vfa_state__?: VfaState
__composition_api_state__?: VfaState
}

interface VueConstructor {
Expand Down
33 changes: 10 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import Vue, { VueConstructor } from 'vue'
import type Vue from 'vue'
import { Data, SetupFunction } from './component'
import { install } from './install'
import { mixin } from './setup'
import { Plugin } from './install'

declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
setup?: SetupFunction<Data, Data>
}
}

const VueCompositionAPI = {
install: (Vue: VueConstructor) => install(Vue, mixin),
}

export default VueCompositionAPI
export { createApp } from './createApp'
export { nextTick } from './nextTick'
export { createElement as h } from './createElement'
export * from './apis'
export { getCurrentInstance } from './runtimeContext'
export {
defineComponent,
Expand All @@ -25,14 +11,15 @@ export {
PropOptions,
SetupContext,
} from './component'
export default Plugin

export * from './apis/state'
export * from './apis/lifecycle'
export * from './apis/watch'
export * from './apis/computed'
export * from './apis/inject'
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
setup?: SetupFunction<Data, Data>
}
}

// auto install when using CDN
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(VueCompositionAPI)
window.Vue.use(Plugin)
}
20 changes: 11 additions & 9 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { VueConstructor } from 'vue'
import { AnyObject } from './types/basic'
import { hasSymbol, hasOwn, isPlainObject, assert } from './utils'
import { isRef } from './reactivity'
import { setCurrentVue, currentVue } from './runtimeContext'
import { VueConstructor } from 'vue'
import { setVueConstructor, isVueRegistered } from './runtimeContext'
import { mixin } from './mixin'

/**
* Helper that recursively merges two data objects together.
Expand Down Expand Up @@ -38,11 +39,8 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
return to
}

export function install(
Vue: VueConstructor,
_install: (Vue: VueConstructor) => void
) {
if (currentVue && currentVue === Vue) {
export function install(Vue: VueConstructor) {
if (isVueRegistered()) {
if (__DEV__) {
assert(
false,
Expand Down Expand Up @@ -73,6 +71,10 @@ export function install(
}
}

setCurrentVue(Vue)
_install(Vue)
setVueConstructor(Vue)
mixin(Vue)
}

export const Plugin = {
install: (Vue: VueConstructor) => install(Vue),
}
41 changes: 27 additions & 14 deletions src/setup.ts → src/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { VueConstructor } from 'vue'
import type { VueConstructor } from 'vue'
import {
ComponentInstance,
SetupContext,
SetupFunction,
Data,
} from './component'
import { Ref, isRef, isReactive, markRaw } from './reactivity'
import { getCurrentInstance, setCurrentVM } from './runtimeContext'
import { resolveSlots, createSlotProxy } from './helper'
import { hasOwn, isPlainObject, assert, proxy, warn, isFunction } from './utils'
import { ref } from './apis/state'
import vmStateManager from './vmStateManager'
import { unwrapRefProxy } from './reactivity/unwrap'
import { markReactive } from './reactivity/reactive'
import {
Ref,
isRef,
isReactive,
markRaw,
unwrapRefProxy,
markReactive,
} from './reactivity'
import { getCurrentInstance, setCurrentInstance } from './runtimeContext'
import {
resolveSlots,
createSlotProxy,
hasOwn,
isPlainObject,
assert,
proxy,
warn,
isFunction,
} from './utils'
import { ref } from './apis'
import vmStateManager from './utils/vmStateManager'

function asVmProperty(
vm: ComponentInstance,
Expand Down Expand Up @@ -83,11 +96,11 @@ function resolveScopedSlots(
vm: ComponentInstance,
slotsProxy: { [x: string]: Function }
): void {
const parentVode = (vm.$options as any)._parentVnode
if (!parentVode) return
const parentVNode = (vm.$options as any)._parentVnode
if (!parentVNode) return

const prevSlots = vmStateManager.get(vm, 'slots') || []
const curSlots = resolveSlots(parentVode.data.scopedSlots, vm.$slots)
const curSlots = resolveSlots(parentVNode.data.scopedSlots, vm.$slots)
// remove staled slots
for (let index = 0; index < prevSlots.length; index++) {
const key = prevSlots[index]
Expand All @@ -113,7 +126,7 @@ function activateCurrentInstance(
onError?: (err: Error) => void
) {
let preVm = getCurrentInstance()
setCurrentVM(vm)
setCurrentInstance(vm)
try {
return fn(vm)
} catch (err) {
Expand All @@ -123,7 +136,7 @@ function activateCurrentInstance(
throw err
}
} finally {
setCurrentVM(preVm)
setCurrentInstance(preVm)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/reactivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
shallowReactive,
toRaw,
isRaw,
markReactive,
} from './reactive'
export {
ref,
Expand All @@ -19,3 +20,4 @@ export {
triggerRef,
} from './ref'
export { set } from './set'
export { unwrapRefProxy } from './unwrap'
Loading