forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisobservable.ts
More file actions
50 lines (47 loc) · 1.82 KB
/
Copy pathisobservable.ts
File metadata and controls
50 lines (47 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { isObservableArray } from "../types/observablearray"
import { isObservableMap } from "../types/observablemap"
import { isObservableObject, ObservableObjectAdministration } from "../types/observableobject"
import { isAtom } from "../core/atom"
import { isComputedValue } from "../core/computedvalue"
import { isReaction } from "../core/reaction"
import { fail } from "../utils/utils"
function _isObservable(value, property?: string): boolean {
if (value === null || value === undefined) return false
if (property !== undefined) {
if (
process.env.NODE_ENV !== "production" &&
(isObservableMap(value) || isObservableArray(value))
)
return fail(
"isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead."
)
if (isObservableObject(value)) {
const o = <ObservableObjectAdministration>(value as any).$mobx
return o.values && !!o.values[property]
}
return false
}
// For first check, see #701
return (
isObservableObject(value) ||
!!value.$mobx ||
isAtom(value) ||
isReaction(value) ||
isComputedValue(value)
)
}
export function isObservable(value: any): boolean {
if (arguments.length !== 1)
fail(
process.env.NODE_ENV !== "production" &&
`isObservable expects only 1 argument. Use isObsevableProp to inspect the observability of a property`
)
return _isObservable(value)
}
export function isObservableProp(value: any, propName: string): boolean {
if (typeof propName !== "string")
return fail(
process.env.NODE_ENV !== "production" && `expected a property name as second argument`
)
return _isObservable(value, propName)
}