2018-10-17 03:47:51 +08:00
|
|
|
import { isObject, EMPTY_OBJ } from '@vue/shared'
|
2018-09-19 23:35:38 +08:00
|
|
|
import { mutableHandlers, immutableHandlers } from './baseHandlers'
|
|
|
|
|
|
|
|
import {
|
|
|
|
mutableCollectionHandlers,
|
|
|
|
immutableCollectionHandlers
|
|
|
|
} from './collectionHandlers'
|
|
|
|
|
|
|
|
import {
|
|
|
|
targetMap,
|
|
|
|
observedToRaw,
|
|
|
|
rawToObserved,
|
|
|
|
immutableToRaw,
|
|
|
|
rawToImmutable,
|
|
|
|
immutableValues,
|
|
|
|
nonReactiveValues
|
|
|
|
} from './state'
|
|
|
|
|
|
|
|
import {
|
2018-11-14 00:03:35 +08:00
|
|
|
createReactiveEffect,
|
2018-09-19 23:35:38 +08:00
|
|
|
cleanup,
|
2018-11-14 00:03:35 +08:00
|
|
|
ReactiveEffect,
|
|
|
|
ReactiveEffectOptions,
|
2018-09-19 23:35:38 +08:00
|
|
|
DebuggerEvent
|
2018-11-14 00:03:35 +08:00
|
|
|
} from './effect'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-05-30 13:35:50 +08:00
|
|
|
import { UnwrapValue } from './value'
|
2019-05-29 22:11:33 +08:00
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
export { ReactiveEffect, ReactiveEffectOptions, DebuggerEvent }
|
2018-09-20 05:45:19 +08:00
|
|
|
export { OperationTypes } from './operations'
|
2019-05-29 18:44:50 +08:00
|
|
|
export { computed, ComputedValue } from './computed'
|
2018-09-19 23:35:38 +08:00
|
|
|
export { lock, unlock } from './lock'
|
2019-05-30 13:35:50 +08:00
|
|
|
export { value, isValue, Value, UnwrapValue } from './value'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
|
|
|
const collectionTypes: Set<any> = new Set([Set, Map, WeakMap, WeakSet])
|
|
|
|
const observableValueRE = /^\[object (?:Object|Array|Map|Set|WeakMap|WeakSet)\]$/
|
|
|
|
|
|
|
|
const canObserve = (value: any): boolean => {
|
|
|
|
return (
|
|
|
|
!value._isVue &&
|
|
|
|
!value._isVNode &&
|
|
|
|
observableValueRE.test(Object.prototype.toString.call(value)) &&
|
|
|
|
!nonReactiveValues.has(value)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:35:50 +08:00
|
|
|
type ObservableFactory = <T>(target?: T) => UnwrapValue<T>
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-06-11 23:50:28 +08:00
|
|
|
export const state = ((target: any = {}): any => {
|
2018-09-19 23:35:38 +08:00
|
|
|
// if trying to observe an immutable proxy, return the immutable version.
|
|
|
|
if (immutableToRaw.has(target)) {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
// target is explicitly marked as immutable by user
|
|
|
|
if (immutableValues.has(target)) {
|
2019-06-11 23:50:28 +08:00
|
|
|
return immutableState(target)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
return createObservable(
|
|
|
|
target,
|
|
|
|
rawToObserved,
|
|
|
|
observedToRaw,
|
|
|
|
mutableHandlers,
|
|
|
|
mutableCollectionHandlers
|
|
|
|
)
|
2019-05-29 22:11:33 +08:00
|
|
|
}) as ObservableFactory
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-06-11 23:50:28 +08:00
|
|
|
export const immutableState = ((target: any = {}): any => {
|
2018-09-19 23:35:38 +08:00
|
|
|
// value is a mutable observable, retrive its original and return
|
|
|
|
// a readonly version.
|
|
|
|
if (observedToRaw.has(target)) {
|
|
|
|
target = observedToRaw.get(target)
|
|
|
|
}
|
|
|
|
return createObservable(
|
|
|
|
target,
|
|
|
|
rawToImmutable,
|
|
|
|
immutableToRaw,
|
|
|
|
immutableHandlers,
|
|
|
|
immutableCollectionHandlers
|
|
|
|
)
|
2019-05-29 22:11:33 +08:00
|
|
|
}) as ObservableFactory
|
2018-09-19 23:35:38 +08:00
|
|
|
|
|
|
|
function createObservable(
|
|
|
|
target: any,
|
|
|
|
toProxy: WeakMap<any, any>,
|
|
|
|
toRaw: WeakMap<any, any>,
|
|
|
|
baseHandlers: ProxyHandler<any>,
|
|
|
|
collectionHandlers: ProxyHandler<any>
|
|
|
|
) {
|
2018-10-17 03:47:51 +08:00
|
|
|
if (!isObject(target)) {
|
2018-09-24 11:16:14 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
console.warn(`value is not observable: ${String(target)}`)
|
|
|
|
}
|
|
|
|
return target
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
// target already has corresponding Proxy
|
|
|
|
let observed = toProxy.get(target)
|
|
|
|
if (observed !== void 0) {
|
|
|
|
return observed
|
|
|
|
}
|
|
|
|
// target is already a Proxy
|
|
|
|
if (toRaw.has(target)) {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
// only a whitelist of value types can be observed.
|
|
|
|
if (!canObserve(target)) {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
const handlers = collectionTypes.has(target.constructor)
|
|
|
|
? collectionHandlers
|
|
|
|
: baseHandlers
|
|
|
|
observed = new Proxy(target, handlers)
|
|
|
|
toProxy.set(target, observed)
|
|
|
|
toRaw.set(observed, target)
|
2018-09-21 04:18:22 +08:00
|
|
|
if (!targetMap.has(target)) {
|
|
|
|
targetMap.set(target, new Map())
|
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
return observed
|
|
|
|
}
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
export function effect(
|
2018-09-19 23:35:38 +08:00
|
|
|
fn: Function,
|
2018-11-14 00:03:35 +08:00
|
|
|
options: ReactiveEffectOptions = EMPTY_OBJ
|
|
|
|
): ReactiveEffect {
|
|
|
|
if ((fn as ReactiveEffect).isEffect) {
|
|
|
|
fn = (fn as ReactiveEffect).raw
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2018-11-14 00:03:35 +08:00
|
|
|
const effect = createReactiveEffect(fn, options)
|
2018-09-19 23:35:38 +08:00
|
|
|
if (!options.lazy) {
|
2018-11-14 00:03:35 +08:00
|
|
|
effect()
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2018-11-14 00:03:35 +08:00
|
|
|
return effect
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
export function stop(effect: ReactiveEffect) {
|
|
|
|
if (effect.active) {
|
|
|
|
cleanup(effect)
|
2019-06-06 13:25:05 +08:00
|
|
|
if (effect.onStop) {
|
|
|
|
effect.onStop()
|
|
|
|
}
|
2018-11-14 00:03:35 +08:00
|
|
|
effect.active = false
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:50:28 +08:00
|
|
|
export function isState(value: any): boolean {
|
2018-09-19 23:35:38 +08:00
|
|
|
return observedToRaw.has(value) || immutableToRaw.has(value)
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:50:28 +08:00
|
|
|
export function isImmutableState(value: any): boolean {
|
2018-09-19 23:35:38 +08:00
|
|
|
return immutableToRaw.has(value)
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:50:28 +08:00
|
|
|
export function toRaw<T>(observed: T): T {
|
2018-09-19 23:35:38 +08:00
|
|
|
return observedToRaw.get(observed) || immutableToRaw.get(observed) || observed
|
|
|
|
}
|
|
|
|
|
|
|
|
export function markImmutable<T>(value: T): T {
|
|
|
|
immutableValues.add(value)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
export function markNonReactive<T>(value: T): T {
|
|
|
|
nonReactiveValues.add(value)
|
|
|
|
return value
|
|
|
|
}
|