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
|
|
|
|
|
|
|
export const observable = ((target: any = {}): any => {
|
|
|
|
// 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)) {
|
|
|
|
return immutable(target)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
export const immutable = ((target: any = {}): any => {
|
|
|
|
// 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)
|
|
|
|
effect.active = false
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isObservable(value: any): boolean {
|
|
|
|
return observedToRaw.has(value) || immutableToRaw.has(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isImmutable(value: any): boolean {
|
|
|
|
return immutableToRaw.has(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unwrap<T>(observed: T): T {
|
|
|
|
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
|
|
|
|
}
|