-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
object-api.ts
147 lines (141 loc) · 5.5 KB
/
object-api.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { isObservableMap, ObservableMap } from "../types/observablemap"
import {
isObservableObject,
IIsObservableObject,
defineObservableProperty,
ObservableObjectAdministration
} from "../types/observableobject"
import { isObservableArray, IObservableArray } from "../types/observablearray"
import { fail, invariant } from "../utils/utils"
import { startBatch, endBatch } from "../core/observable"
import { getAdministration } from "../types/type-utils"
import { ObservableValue } from "../types/observablevalue"
export function keys<K>(map: ObservableMap<K, any>): ReadonlyArray<K>
export function keys<T extends Object>(obj: T): ReadonlyArray<string>
export function keys(obj: any): any {
if (isObservableObject(obj)) {
return ((obj as any) as IIsObservableObject).$mobx.getKeys()
}
if (isObservableMap(obj)) {
return (obj as any)._keys.slice()
}
return fail(
process.env.NODE_ENV !== "production" &&
"'keys()' can only be used on observable objects and maps"
)
}
export function values<K, T>(map: ObservableMap<K, T>): ReadonlyArray<T>
export function values<T>(ar: IObservableArray<T>): ReadonlyArray<T>
export function values<T = any>(obj: T): ReadonlyArray<any>
export function values(obj: any): string[] {
if (isObservableObject(obj)) {
return keys(obj).map(key => obj[key])
}
if (isObservableMap(obj)) {
return keys(obj).map(key => obj.get(key))
}
if (isObservableArray(obj)) {
return obj.slice()
}
return fail(
process.env.NODE_ENV !== "production" &&
"'values()' can only be used on observable objects, arrays and maps"
)
}
export function set<V>(obj: ObservableMap<string, V>, values: { [key: string]: V })
export function set<K, V>(obj: ObservableMap<K, V>, key: K, value: V)
export function set<T>(obj: IObservableArray<T>, index: number, value: T)
export function set<T extends Object>(obj: T, values: { [key: string]: any })
export function set<T extends Object>(obj: T, key: string, value: any)
export function set(obj: any, key: any, value?: any): void {
if (arguments.length === 2) {
startBatch()
const values = key
try {
for (let key in values) set(obj, key, values[key])
} finally {
endBatch()
}
return
}
if (isObservableObject(obj)) {
const adm = ((obj as any) as IIsObservableObject).$mobx
const existingObservable = adm.values[key]
if (existingObservable) {
existingObservable.set(value)
} else {
defineObservableProperty(obj, key, value, adm.defaultEnhancer)
}
} else if (isObservableMap(obj)) {
obj.set(key, value)
} else if (isObservableArray(obj)) {
if (typeof key !== "number") key = parseInt(key, 10)
invariant(key >= 0, `Not a valid index: '${key}'`)
startBatch()
if (key >= obj.length) obj.length = key + 1
obj[key] = value
endBatch()
} else {
return fail(
process.env.NODE_ENV !== "production" &&
"'set()' can only be used on observable objects, arrays and maps"
)
}
}
export function remove<K, V>(obj: ObservableMap<K, V>, key: K)
export function remove<T>(obj: IObservableArray<T>, index: number)
export function remove<T extends Object>(obj: T, key: string)
export function remove(obj: any, key: any): void {
if (isObservableObject(obj)) {
;((obj as any) as IIsObservableObject).$mobx.remove(key)
} else if (isObservableMap(obj)) {
obj.delete(key)
} else if (isObservableArray(obj)) {
if (typeof key !== "number") key = parseInt(key, 10)
invariant(key >= 0, `Not a valid index: '${key}'`)
obj.splice(key, 1)
} else {
return fail(
process.env.NODE_ENV !== "production" &&
"'remove()' can only be used on observable objects, arrays and maps"
)
}
}
export function has<K>(obj: ObservableMap<K, any>, key: K): boolean
export function has<T>(obj: IObservableArray<T>, index: number): boolean
export function has<T extends Object>(obj: T, key: string): boolean
export function has(obj: any, key: any): boolean {
if (isObservableObject(obj)) {
// return keys(obj).indexOf(key) >= 0
const adm = getAdministration(obj) as ObservableObjectAdministration
adm.getKeys() // make sure we get notified of key changes, but for performance, use the values map to look up existence
return adm.values[key] instanceof ObservableValue
} else if (isObservableMap(obj)) {
return obj.has(key)
} else if (isObservableArray(obj)) {
return key >= 0 && key < obj.length
} else {
return fail(
process.env.NODE_ENV !== "production" &&
"'has()' can only be used on observable objects, arrays and maps"
)
}
}
export function get<K, V>(obj: ObservableMap<K, V>, key: K): V | undefined
export function get<T>(obj: IObservableArray<T>, index: number): T | undefined
export function get<T extends Object>(obj: T, key: string): any
export function get(obj: any, key: any): any {
if (!has(obj, key)) return undefined
if (isObservableObject(obj)) {
return obj[key]
} else if (isObservableMap(obj)) {
return obj.get(key)
} else if (isObservableArray(obj)) {
return obj[key]
} else {
return fail(
process.env.NODE_ENV !== "production" &&
"'get()' can only be used on observable objects, arrays and maps"
)
}
}