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'
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) {
// TODO warn
} 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) {
// 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)
}
}

View File

@ -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<T, C = null>(
getter: () => T,
setter?: (v: T) => void
): ComputedValue<T> {
): ComputedRef<T> {
const c = _computed(getter, setter)
recordEffect(c.effect)
return c

View File

@ -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<T> = Value<T> | (() => T)
type WatcherSource<T> = Ref<T> | (() => T)
const invoke = (fn: Function) => fn()
@ -34,8 +34,8 @@ export function watch<T>(
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

View File

@ -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<Props, RawBindings> = (
) => RawBindings | (() => VNodeChild)
type RenderFunction<Props = {}, RawBindings = {}> = <
Bindings extends UnwrapValue<RawBindings>
Bindings extends UnwrapRef<RawBindings>
>(
this: ComponentRenderProxy<Props, Bindings>,
ctx: ComponentRenderProxy<Props, Bindings>
@ -135,7 +130,7 @@ export function createComponent<Props>(
export function createComponent<Props, RawBindings>(
options: ComponentOptionsWithoutProps<Props, RawBindings>
): {
new (): ComponentRenderProxy<Props, UnwrapValue<RawBindings>>
new (): ComponentRenderProxy<Props, UnwrapRef<RawBindings>>
}
// overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: unknown }
@ -145,7 +140,7 @@ export function createComponent<PropNames extends string, RawBindings>(
): {
new (): ComponentRenderProxy<
{ [key in PropNames]?: unknown },
UnwrapValue<RawBindings>
UnwrapRef<RawBindings>
>
}
// overload 4: object format with object props declaration
@ -156,7 +151,7 @@ export function createComponent<PropsOptions, RawBindings>(
// for Vetur and TSX support
new (): ComponentRenderProxy<
ExtractPropTypes<PropsOptions>,
UnwrapValue<RawBindings>,
UnwrapRef<RawBindings>,
ExtractPropTypes<PropsOptions, false>
>
}
@ -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
}

View File

@ -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
}