-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
observablemap.ts
393 lines (358 loc) · 12.5 KB
/
observablemap.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { IEnhancer, deepEnhancer } from "./modifiers"
import { untracked } from "../core/derivation"
import { IObservableArray, ObservableArray } from "./observablearray"
import { ObservableValue, UNCHANGED } from "./observablevalue"
import {
createInstanceofPredicate,
isPlainObject,
getNextId,
Lambda,
invariant,
isES6Map,
getMapLikeKeys,
fail,
addHiddenFinalProp
} from "../utils/utils"
import {
IInterceptable,
IInterceptor,
hasInterceptors,
registerInterceptor,
interceptChange
} from "./intercept-utils"
import { IListenable, registerListener, hasListeners, notifyListeners } from "./listen-utils"
import { isSpyEnabled, spyReportStart, spyReportEnd } from "../core/spy"
import { declareIterator, iteratorSymbol, makeIterable } from "../utils/iterable"
import { transaction } from "../api/transaction"
import { referenceEnhancer } from "./modifiers"
export interface IKeyValueMap<V = any> {
[key: string]: V
}
export type IMapEntry<K = any, V = any> = [K, V]
export type IMapEntries<K = any, V = any> = IMapEntry<K, V>[]
export type IMapDidChange<K = any, V = any> =
| {
object: ObservableMap<K, V>
name: K // actual the key or index, but this is based on the ancient .observe proposal for consistency
type: "update"
newValue: V
oldValue: V
}
| {
object: ObservableMap<K, V>
name: K
type: "add"
newValue: V
}
| {
object: ObservableMap<K, V>
name: K
type: "delete"
oldValue: V
}
export interface IMapWillChange<K = any, V = any> {
object: ObservableMap<K, V>
type: "update" | "add" | "delete"
name: K
newValue?: V
}
const ObservableMapMarker = {}
export type IObservableMapInitialValues<K = any, V = any> =
| IMapEntries<K, V>
| IKeyValueMap<V>
| Map<K, V>
export class ObservableMap<K = any, V = any>
implements Map<K, V>, IInterceptable<IMapWillChange<K, V>>, IListenable {
$mobx = ObservableMapMarker
private _data: Map<K, ObservableValue<V>>
private _hasMap: Map<K, ObservableValue<boolean>> // hasMap, not hashMap >-).
private _keys: IObservableArray<K> = <any>new ObservableArray(
undefined,
referenceEnhancer,
`${this.name}.keys()`,
true
)
interceptors
changeListeners
dehancer: any;
[Symbol.iterator];
[Symbol.toStringTag]
constructor(
initialData?: IObservableMapInitialValues<K, V>,
public enhancer: IEnhancer<V> = deepEnhancer,
public name = "ObservableMap@" + getNextId()
) {
if (typeof Map !== "function") {
throw new Error(
"mobx.map requires Map polyfill for the current browser. Check babel-polyfill or core-js/es6/map.js"
)
}
this._data = new Map()
this._hasMap = new Map()
this.merge(initialData)
}
private _has(key: K): boolean {
return this._data.has(key)
}
has(key: K): boolean {
if (this._hasMap.has(key)) return this._hasMap.get(key)!.get()
return this._updateHasMapEntry(key, false).get()
}
set(key: K, value: V) {
const hasKey = this._has(key)
if (hasInterceptors(this)) {
const change = interceptChange<IMapWillChange<K, V>>(this, {
type: hasKey ? "update" : "add",
object: this,
newValue: value,
name: key
})
if (!change) return this
value = change.newValue!
}
if (hasKey) {
this._updateValue(key, value)
} else {
this._addValue(key, value)
}
return this
}
delete(key: K): boolean {
if (hasInterceptors(this)) {
const change = interceptChange<IMapWillChange<K, V>>(this, {
type: "delete",
object: this,
name: key
})
if (!change) return false
}
if (this._has(key)) {
const notifySpy = isSpyEnabled()
const notify = hasListeners(this)
const change =
notify || notifySpy
? <IMapDidChange<K, V>>{
type: "delete",
object: this,
oldValue: (<any>this._data.get(key)).value,
name: key
}
: null
if (notifySpy) spyReportStart({ ...change, name: this.name, key })
transaction(() => {
this._keys.remove(key)
this._updateHasMapEntry(key, false)
const observable = this._data.get(key)!
observable.setNewValue(undefined as any)
this._data.delete(key)
})
if (notify) notifyListeners(this, change)
if (notifySpy) spyReportEnd()
return true
}
return false
}
private _updateHasMapEntry(key: K, value: boolean): ObservableValue<boolean> {
// optimization; don't fill the hasMap if we are not observing, or remove entry if there are no observers anymore
let entry = this._hasMap.get(key)
if (entry) {
entry.setNewValue(value)
} else {
entry = new ObservableValue(value, referenceEnhancer, `${this.name}.${key}?`, false)
this._hasMap.set(key, entry)
}
return entry
}
private _updateValue(key: K, newValue: V | undefined) {
const observable = this._data.get(key)!
newValue = (observable as any).prepareNewValue(newValue) as V
if (newValue !== UNCHANGED) {
const notifySpy = isSpyEnabled()
const notify = hasListeners(this)
const change =
notify || notifySpy
? <IMapDidChange<K, V>>{
type: "update",
object: this,
oldValue: (observable as any).value,
name: key,
newValue
}
: null
if (notifySpy) spyReportStart({ ...change, name: this.name, key })
observable.setNewValue(newValue as V)
if (notify) notifyListeners(this, change)
if (notifySpy) spyReportEnd()
}
}
private _addValue(key: K, newValue: V) {
transaction(() => {
const observable = new ObservableValue(
newValue,
this.enhancer,
`${this.name}.${key}`,
false
)
this._data.set(key, observable)
newValue = (observable as any).value // value might have been changed
this._updateHasMapEntry(key, true)
this._keys.push(key)
})
const notifySpy = isSpyEnabled()
const notify = hasListeners(this)
const change =
notify || notifySpy
? <IMapDidChange<K, V>>{
type: "add",
object: this,
name: key,
newValue
}
: null
if (notifySpy) spyReportStart({ ...change, name: this.name, key })
if (notify) notifyListeners(this, change)
if (notifySpy) spyReportEnd()
}
get(key: K): V | undefined {
if (this.has(key)) return this.dehanceValue(this._data.get(key)!.get())
return this.dehanceValue(undefined)
}
private dehanceValue<X extends V | undefined>(value: X): X {
if (this.dehancer !== undefined) {
return this.dehancer(value)
}
return value
}
keys(): IterableIterator<K> {
return (this._keys[iteratorSymbol()] as any)()
}
values(): IterableIterator<V> {
const self = this
let nextIndex = 0
return makeIterable({
next() {
return nextIndex < self._keys.length
? { value: self.get(self._keys[nextIndex++]), done: false }
: { value: undefined as any, done: true }
}
})
}
entries(): IterableIterator<IMapEntry<K, V>> {
const self = this
let nextIndex = 0
return makeIterable(
{
next: function() {
if (nextIndex < self._keys.length) {
const key = self._keys[nextIndex++]
return {
value: [key, self.get(key)!] as [K, V],
done: false
}
}
return { done: true }
}
} as any
)
}
forEach(callback: (value: V, key: K, object: Map<K, V>) => void, thisArg?) {
this._keys.forEach(key => callback.call(thisArg, this.get(key), key, this))
}
/** Merge another object into this object, returns this. */
merge(other: ObservableMap<K, V> | IKeyValueMap<V> | any): ObservableMap<K, V> {
if (isObservableMap(other)) {
other = other.toJS()
}
transaction(() => {
if (isPlainObject(other))
Object.keys(other).forEach(key => this.set((key as any) as K, other[key]))
else if (Array.isArray(other)) other.forEach(([key, value]) => this.set(key, value))
else if (isES6Map(other)) other.forEach((value, key) => this.set(key, value))
else if (other !== null && other !== undefined)
fail("Cannot initialize map from " + other)
})
return this
}
clear() {
transaction(() => {
untracked(() => {
this._keys.slice().forEach(key => this.delete(key))
})
})
}
replace(values: ObservableMap<K, V> | IKeyValueMap<V> | any): ObservableMap<K, V> {
transaction(() => {
// grab all the keys that are present in the new map but not present in the current map
// and delete them from the map, then merge the new map
// this will cause reactions only on changed values
const newKeys = (getMapLikeKeys(values) as any) as K[]
const oldKeys = this._keys
const missingKeys = oldKeys.filter(k => newKeys.indexOf(k) === -1)
missingKeys.forEach(k => this.delete(k))
this.merge(values)
})
return this
}
get size(): number {
return this._keys.length
}
/**
* Returns a plain object that represents this map.
* Note that all the keys being stringified.
* If there are duplicating keys after converting them to strings, behaviour is undetermined.
*/
toPOJO(): IKeyValueMap<V> {
const res: IKeyValueMap<V> = {}
this._keys.forEach(key => (res["" + key] = this.get(key)!))
return res
}
/**
* Returns a shallow non observable object clone of this map.
* Note that the values migth still be observable. For a deep clone use mobx.toJS.
*/
toJS(): Map<K, V> {
const res: Map<K, V> = new Map()
this._keys.forEach(key => res.set(key, this.get(key)!))
return res
}
toJSON(): IKeyValueMap<V> {
// Used by JSON.stringify
return this.toPOJO()
}
toString(): string {
return (
this.name +
"[{ " +
this._keys.map(key => `${key}: ${"" + this.get(key)}`).join(", ") +
" }]"
)
}
/**
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
* for callback details
*/
observe(listener: (changes: IMapDidChange<K, V>) => void, fireImmediately?: boolean): Lambda {
process.env.NODE_ENV !== "production" &&
invariant(
fireImmediately !== true,
"`observe` doesn't support fireImmediately=true in combination with maps."
)
return registerListener(this, listener)
}
intercept(handler: IInterceptor<IMapWillChange<K, V>>): Lambda {
return registerInterceptor(this, handler)
}
}
declareIterator(ObservableMap.prototype, function() {
return this.entries()
})
addHiddenFinalProp(
ObservableMap.prototype,
typeof Symbol !== "undefined" ? Symbol.toStringTag : "@@toStringTag" as any,
"Map"
)
/* 'var' fixes small-build issue */
export var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap) as (
thing: any
) => thing is ObservableMap<any, any>