wip: update runtime core to updated reactivity api names

This commit is contained in:
Evan You 2019-08-16 10:02:53 -04:00
parent 96d65e1ab5
commit e1e4a3854c
5 changed files with 38 additions and 39 deletions

View File

@ -1,9 +1,9 @@
import { value, isValue, Value } from './apiState' import { ref, isRef, Ref } from './apiState'
import { currentInstance } from './component' import { currentInstance } from './component'
export interface Key<T> extends Symbol {} export interface InjectionKey<T> extends Symbol {}
export function provide<T>(key: Key<T> | string, value: T | Value<T>) { export function provide<T>(key: InjectionKey<T> | string, value: T | Ref<T>) {
if (!currentInstance) { if (!currentInstance) {
// TODO warn // TODO warn
} else { } else {
@ -22,15 +22,19 @@ export function provide<T>(key: Key<T> | string, value: T | Value<T>) {
} }
} }
export function inject<T>(key: Key<T> | string): Value<T> | undefined { export function inject<T>(key: InjectionKey<T> | string): Ref<T> | undefined
export function inject<T>(
key: InjectionKey<T> | string,
defaultValue: T
): Ref<T>
export function inject(key: InjectionKey<any> | string, defaultValue?: any) {
if (!currentInstance) { if (!currentInstance) {
// TODO warn // TODO warn
} else { } else {
// TODO should also check for app-level provides // TODO should also check for app-level provides
const provides = currentInstance.parent && currentInstance.provides const provides = currentInstance.parent && currentInstance.provides
if (provides) { const val =
const val = provides[key as any] as any provides && key in provides ? (provides[key as any] as any) : defaultValue
return isValue(val) ? val : value(val) return isRef(val) ? val : ref(val)
}
} }
} }

View File

@ -1,10 +1,10 @@
export { export {
value, ref,
isValue, isRef,
state, reactive,
isState, isReactive,
immutableState, immutable,
isImmutableState, isImmutable,
toRaw, toRaw,
markImmutable, markImmutable,
markNonReactive, markNonReactive,
@ -14,14 +14,14 @@ export {
ReactiveEffectOptions, ReactiveEffectOptions,
DebuggerEvent, DebuggerEvent,
OperationTypes, OperationTypes,
Value, Ref,
ComputedValue, ComputedRef,
UnwrapValue UnwrapRef
} from '@vue/reactivity' } from '@vue/reactivity'
import { import {
computed as _computed, computed as _computed,
ComputedValue, ComputedRef,
ReactiveEffect ReactiveEffect
} from '@vue/reactivity' } from '@vue/reactivity'
@ -39,7 +39,7 @@ export function recordEffect(effect: ReactiveEffect) {
export function computed<T, C = null>( export function computed<T, C = null>(
getter: () => T, getter: () => T,
setter?: (v: T) => void setter?: (v: T) => void
): ComputedValue<T> { ): ComputedRef<T> {
const c = _computed(getter, setter) const c = _computed(getter, setter)
recordEffect(c.effect) recordEffect(c.effect)
return c return c

View File

@ -1,8 +1,8 @@
import { import {
effect, effect,
stop, stop,
isValue, isRef,
Value, Ref,
ReactiveEffectOptions ReactiveEffectOptions
} from '@vue/reactivity' } from '@vue/reactivity'
import { queueJob, queuePostFlushCb } from './scheduler' import { queueJob, queuePostFlushCb } from './scheduler'
@ -17,7 +17,7 @@ export interface WatchOptions {
onTrigger?: ReactiveEffectOptions['onTrigger'] onTrigger?: ReactiveEffectOptions['onTrigger']
} }
type WatcherSource<T> = Value<T> | (() => T) type WatcherSource<T> = Ref<T> | (() => T)
const invoke = (fn: Function) => fn() const invoke = (fn: Function) => fn()
@ -34,8 +34,8 @@ export function watch<T>(
flush === 'sync' ? invoke : flush === 'pre' ? queueJob : queuePostFlushCb flush === 'sync' ? invoke : flush === 'pre' ? queueJob : queuePostFlushCb
const baseGetter = isArray(source) const baseGetter = isArray(source)
? () => source.map(s => (isValue(s) ? s.value : s())) ? () => source.map(s => (isRef(s) ? s.value : s()))
: isValue(source) : isRef(source)
? () => source.value ? () => source.value
: source : source
const getter = deep ? () => traverse(baseGetter()) : baseGetter const getter = deep ? () => traverse(baseGetter()) : baseGetter

View File

@ -1,10 +1,5 @@
import { VNode, normalizeVNode, VNodeChild } from './vnode' import { VNode, normalizeVNode, VNodeChild } from './vnode'
import { import { ReactiveEffect, UnwrapRef, reactive, immutable } from '@vue/reactivity'
ReactiveEffect,
UnwrapValue,
state,
immutableState
} from '@vue/reactivity'
import { EMPTY_OBJ, isFunction, capitalize, invokeHandlers } from '@vue/shared' import { EMPTY_OBJ, isFunction, capitalize, invokeHandlers } from '@vue/shared'
import { RenderProxyHandlers } from './componentProxy' import { RenderProxyHandlers } from './componentProxy'
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps' import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
@ -34,7 +29,7 @@ type SetupFunction<Props, RawBindings> = (
) => RawBindings | (() => VNodeChild) ) => RawBindings | (() => VNodeChild)
type RenderFunction<Props = {}, RawBindings = {}> = < type RenderFunction<Props = {}, RawBindings = {}> = <
Bindings extends UnwrapValue<RawBindings> Bindings extends UnwrapRef<RawBindings>
>( >(
this: ComponentRenderProxy<Props, Bindings>, this: ComponentRenderProxy<Props, Bindings>,
ctx: ComponentRenderProxy<Props, Bindings> ctx: ComponentRenderProxy<Props, Bindings>
@ -135,7 +130,7 @@ export function createComponent<Props>(
export function createComponent<Props, RawBindings>( export function createComponent<Props, RawBindings>(
options: ComponentOptionsWithoutProps<Props, RawBindings> options: ComponentOptionsWithoutProps<Props, RawBindings>
): { ): {
new (): ComponentRenderProxy<Props, UnwrapValue<RawBindings>> new (): ComponentRenderProxy<Props, UnwrapRef<RawBindings>>
} }
// overload 3: object format with array props declaration // overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: unknown } // props inferred as { [key in PropNames]?: unknown }
@ -145,7 +140,7 @@ export function createComponent<PropNames extends string, RawBindings>(
): { ): {
new (): ComponentRenderProxy< new (): ComponentRenderProxy<
{ [key in PropNames]?: unknown }, { [key in PropNames]?: unknown },
UnwrapValue<RawBindings> UnwrapRef<RawBindings>
> >
} }
// overload 4: object format with object props declaration // overload 4: object format with object props declaration
@ -156,7 +151,7 @@ export function createComponent<PropsOptions, RawBindings>(
// for Vetur and TSX support // for Vetur and TSX support
new (): ComponentRenderProxy< new (): ComponentRenderProxy<
ExtractPropTypes<PropsOptions>, ExtractPropTypes<PropsOptions>,
UnwrapValue<RawBindings>, UnwrapRef<RawBindings>,
ExtractPropTypes<PropsOptions, false> ExtractPropTypes<PropsOptions, false>
> >
} }
@ -232,7 +227,7 @@ export function setupStatefulComponent(instance: ComponentInstance) {
// so props change can be tracked by watchers // so props change can be tracked by watchers
// it will be updated in resolveProps() on updates before render // it will be updated in resolveProps() on updates before render
const propsProxy = (instance.propsProxy = setup.length const propsProxy = (instance.propsProxy = setup.length
? immutableState(instance.props) ? immutable(instance.props)
: null) : null)
const setupContext = (instance.setupContext = const setupContext = (instance.setupContext =
setup.length > 1 ? createSetupContext(instance) : null) setup.length > 1 ? createSetupContext(instance) : null)
@ -247,7 +242,7 @@ export function setupStatefulComponent(instance: ComponentInstance) {
} else { } else {
// setup returned bindings. // setup returned bindings.
// assuming a render function compiled from template is present. // assuming a render function compiled from template is present.
instance.data = state(setupResult) instance.data = reactive(setupResult)
if (__DEV__ && !Component.render) { if (__DEV__ && !Component.render) {
// TODO warn missing render fn // TODO warn missing render fn
} }

View File

@ -1,4 +1,4 @@
import { immutableState, toRaw, lock, unlock } from '@vue/reactivity' import { immutable, toRaw, lock, unlock } from '@vue/reactivity'
import { import {
EMPTY_OBJ, EMPTY_OBJ,
camelize, camelize,
@ -179,10 +179,10 @@ export function resolveProps(
// lock immutable // lock immutable
lock() lock()
instance.props = __DEV__ ? immutableState(props) : props instance.props = __DEV__ ? immutable(props) : props
instance.attrs = options instance.attrs = options
? __DEV__ ? __DEV__
? immutableState(attrs) ? immutable(attrs)
: attrs : attrs
: instance.props : instance.props
} }