UNPKG

2.4 kBPlain TextView Raw
1import { isAction } from "../api/action"
2import {
3 $mobx,
4 IDepTreeNode,
5 isAtom,
6 isComputedValue,
7 isObservableArray,
8 isObservableMap,
9 isObservableObject,
10 isReaction,
11 isObservableSet,
12 die,
13 isFunction
14} from "../internal"
15
16export function getAtom(thing: any, property?: PropertyKey): IDepTreeNode {
17 if (typeof thing === "object" && thing !== null) {
18 if (isObservableArray(thing)) {
19 if (property !== undefined) die(23)
20 return (thing as any)[$mobx].atom_
21 }
22 if (isObservableSet(thing)) {
23 return (thing as any)[$mobx]
24 }
25 if (isObservableMap(thing)) {
26 if (property === undefined) return thing.keysAtom_
27 const observable = thing.data_.get(property) || thing.hasMap_.get(property)
28 if (!observable) die(25, property, getDebugName(thing))
29 return observable
30 }
31 if (property && !thing[$mobx]) thing[property] // See #1072
32 if (isObservableObject(thing)) {
33 if (!property) return die(26)
34 const observable = (thing as any)[$mobx].values_.get(property)
35 if (!observable) die(27, property, getDebugName(thing))
36 return observable
37 }
38 if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) {
39 return thing
40 }
41 } else if (isFunction(thing)) {
42 if (isReaction(thing[$mobx])) {
43 // disposer function
44 return thing[$mobx]
45 }
46 }
47 die(28)
48}
49
50export function getAdministration(thing: any, property?: string) {
51 if (!thing) die(29)
52 if (property !== undefined) return getAdministration(getAtom(thing, property))
53 if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) return thing
54 if (isObservableMap(thing) || isObservableSet(thing)) return thing
55 if (thing[$mobx]) return thing[$mobx]
56 die(24, thing)
57}
58
59export function getDebugName(thing: any, property?: string): string {
60 let named
61 if (property !== undefined) {
62 named = getAtom(thing, property)
63 } else if (isAction(thing)) {
64 return thing.name
65 } else if (isObservableObject(thing) || isObservableMap(thing) || isObservableSet(thing)) {
66 named = getAdministration(thing)
67 } else {
68 // valid for arrays as well
69 named = getAtom(thing)
70 }
71 return named.name_
72}