UNPKG

1.38 kBPlain TextView Raw
1import {
2 $mobx,
3 isAtom,
4 isComputedValue,
5 isObservableArray,
6 isObservableMap,
7 isObservableObject,
8 isReaction,
9 die,
10 isStringish
11} from "../internal"
12
13function _isObservable(value, property?: PropertyKey): boolean {
14 if (!value) return false
15 if (property !== undefined) {
16 if (__DEV__ && (isObservableMap(value) || isObservableArray(value)))
17 return die(
18 "isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead."
19 )
20 if (isObservableObject(value)) {
21 return value[$mobx].values_.has(property)
22 }
23 return false
24 }
25 // For first check, see #701
26 return (
27 isObservableObject(value) ||
28 !!value[$mobx] ||
29 isAtom(value) ||
30 isReaction(value) ||
31 isComputedValue(value)
32 )
33}
34
35export function isObservable(value: any): boolean {
36 if (__DEV__ && arguments.length !== 1)
37 die(
38 `isObservable expects only 1 argument. Use isObservableProp to inspect the observability of a property`
39 )
40 return _isObservable(value)
41}
42
43export function isObservableProp(value: any, propName: PropertyKey): boolean {
44 if (__DEV__ && !isStringish(propName)) return die(`expected a property name as second argument`)
45 return _isObservable(value, propName)
46}