From e1e4a3854c9d1a43fb63358b7afe63cbc41542d5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 16 Aug 2019 10:02:53 -0400 Subject: [PATCH] wip: update runtime core to updated reactivity api names --- packages/runtime-core/src/apiInject.ts | 20 +++++++++++-------- packages/runtime-core/src/apiState.ts | 22 ++++++++++----------- packages/runtime-core/src/apiWatch.ts | 10 +++++----- packages/runtime-core/src/component.ts | 19 +++++++----------- packages/runtime-core/src/componentProps.ts | 6 +++--- 5 files changed, 38 insertions(+), 39 deletions(-) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index f3133fb5..602fc1c0 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -1,9 +1,9 @@ -import { value, isValue, Value } from './apiState' +import { ref, isRef, Ref } from './apiState' import { currentInstance } from './component' -export interface Key extends Symbol {} +export interface InjectionKey extends Symbol {} -export function provide(key: Key | string, value: T | Value) { +export function provide(key: InjectionKey | string, value: T | Ref) { if (!currentInstance) { // TODO warn } else { @@ -22,15 +22,19 @@ export function provide(key: Key | string, value: T | Value) { } } -export function inject(key: Key | string): Value | undefined { +export function inject(key: InjectionKey | string): Ref | undefined +export function inject( + key: InjectionKey | string, + defaultValue: T +): Ref +export function inject(key: InjectionKey | string, defaultValue?: any) { if (!currentInstance) { // TODO warn } else { // TODO should also check for app-level provides const provides = currentInstance.parent && currentInstance.provides - if (provides) { - const val = provides[key as any] as any - return isValue(val) ? val : value(val) - } + const val = + provides && key in provides ? (provides[key as any] as any) : defaultValue + return isRef(val) ? val : ref(val) } } diff --git a/packages/runtime-core/src/apiState.ts b/packages/runtime-core/src/apiState.ts index b66199f2..a40df59c 100644 --- a/packages/runtime-core/src/apiState.ts +++ b/packages/runtime-core/src/apiState.ts @@ -1,10 +1,10 @@ export { - value, - isValue, - state, - isState, - immutableState, - isImmutableState, + ref, + isRef, + reactive, + isReactive, + immutable, + isImmutable, toRaw, markImmutable, markNonReactive, @@ -14,14 +14,14 @@ export { ReactiveEffectOptions, DebuggerEvent, OperationTypes, - Value, - ComputedValue, - UnwrapValue + Ref, + ComputedRef, + UnwrapRef } from '@vue/reactivity' import { computed as _computed, - ComputedValue, + ComputedRef, ReactiveEffect } from '@vue/reactivity' @@ -39,7 +39,7 @@ export function recordEffect(effect: ReactiveEffect) { export function computed( getter: () => T, setter?: (v: T) => void -): ComputedValue { +): ComputedRef { const c = _computed(getter, setter) recordEffect(c.effect) return c diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 85e33e75..a92596f4 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -1,8 +1,8 @@ import { effect, stop, - isValue, - Value, + isRef, + Ref, ReactiveEffectOptions } from '@vue/reactivity' import { queueJob, queuePostFlushCb } from './scheduler' @@ -17,7 +17,7 @@ export interface WatchOptions { onTrigger?: ReactiveEffectOptions['onTrigger'] } -type WatcherSource = Value | (() => T) +type WatcherSource = Ref | (() => T) const invoke = (fn: Function) => fn() @@ -34,8 +34,8 @@ export function watch( flush === 'sync' ? invoke : flush === 'pre' ? queueJob : queuePostFlushCb const baseGetter = isArray(source) - ? () => source.map(s => (isValue(s) ? s.value : s())) - : isValue(source) + ? () => source.map(s => (isRef(s) ? s.value : s())) + : isRef(source) ? () => source.value : source const getter = deep ? () => traverse(baseGetter()) : baseGetter diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 8df68a69..117f4b47 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -1,10 +1,5 @@ import { VNode, normalizeVNode, VNodeChild } from './vnode' -import { - ReactiveEffect, - UnwrapValue, - state, - immutableState -} from '@vue/reactivity' +import { ReactiveEffect, UnwrapRef, reactive, immutable } from '@vue/reactivity' import { EMPTY_OBJ, isFunction, capitalize, invokeHandlers } from '@vue/shared' import { RenderProxyHandlers } from './componentProxy' import { ComponentPropsOptions, ExtractPropTypes } from './componentProps' @@ -34,7 +29,7 @@ type SetupFunction = ( ) => RawBindings | (() => VNodeChild) type RenderFunction = < - Bindings extends UnwrapValue + Bindings extends UnwrapRef >( this: ComponentRenderProxy, ctx: ComponentRenderProxy @@ -135,7 +130,7 @@ export function createComponent( export function createComponent( options: ComponentOptionsWithoutProps ): { - new (): ComponentRenderProxy> + new (): ComponentRenderProxy> } // overload 3: object format with array props declaration // props inferred as { [key in PropNames]?: unknown } @@ -145,7 +140,7 @@ export function createComponent( ): { new (): ComponentRenderProxy< { [key in PropNames]?: unknown }, - UnwrapValue + UnwrapRef > } // overload 4: object format with object props declaration @@ -156,7 +151,7 @@ export function createComponent( // for Vetur and TSX support new (): ComponentRenderProxy< ExtractPropTypes, - UnwrapValue, + UnwrapRef, ExtractPropTypes > } @@ -232,7 +227,7 @@ export function setupStatefulComponent(instance: ComponentInstance) { // so props change can be tracked by watchers // it will be updated in resolveProps() on updates before render const propsProxy = (instance.propsProxy = setup.length - ? immutableState(instance.props) + ? immutable(instance.props) : null) const setupContext = (instance.setupContext = setup.length > 1 ? createSetupContext(instance) : null) @@ -247,7 +242,7 @@ export function setupStatefulComponent(instance: ComponentInstance) { } else { // setup returned bindings. // assuming a render function compiled from template is present. - instance.data = state(setupResult) + instance.data = reactive(setupResult) if (__DEV__ && !Component.render) { // TODO warn missing render fn } diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index c7bf0804..cb8ebb2c 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -1,4 +1,4 @@ -import { immutableState, toRaw, lock, unlock } from '@vue/reactivity' +import { immutable, toRaw, lock, unlock } from '@vue/reactivity' import { EMPTY_OBJ, camelize, @@ -179,10 +179,10 @@ export function resolveProps( // lock immutable lock() - instance.props = __DEV__ ? immutableState(props) : props + instance.props = __DEV__ ? immutable(props) : props instance.attrs = options ? __DEV__ - ? immutableState(attrs) + ? immutable(attrs) : attrs : instance.props }