forked from jupyterlab/jupyterlab
/
observablemap.ts
351 lines (313 loc) · 6.99 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IDisposable } from '@phosphor/disposable';
import { ISignal, Signal } from '@phosphor/signaling';
import { IObservable } from './modeldb';
/**
* A map which can be observed for changes.
*/
export interface IObservableMap<T> extends IDisposable, IObservable {
/**
* The type of the Observable.
*/
type: 'Map';
/**
* A signal emitted when the map has changed.
*/
readonly changed: ISignal<this, IObservableMap.IChangedArgs<T>>;
/**
* The number of key-value pairs in the map.
*/
readonly size: number;
/**
* Set a key-value pair in the map
*
* @param key - The key to set.
*
* @param value - The value for the key.
*
* @returns the old value for the key, or undefined
* if that did not exist.
*/
set(key: string, value: T): T | undefined;
/**
* Get a value for a given key.
*
* @param key - the key.
*
* @returns the value for that key.
*/
get(key: string): T | undefined;
/**
* Check whether the map has a key.
*
* @param key - the key to check.
*
* @returns `true` if the map has the key, `false` otherwise.
*/
has(key: string): boolean;
/**
* Get a list of the keys in the map.
*
* @returns - a list of keys.
*/
keys(): string[];
/**
* Get a list of the values in the map.
*
* @returns - a list of values.
*/
values(): T[];
/**
* Remove a key from the map
*
* @param key - the key to remove.
*
* @returns the value of the given key,
* or undefined if that does not exist.
*/
delete(key: string): T | undefined;
/**
* Set the ObservableMap to an empty map.
*/
clear(): void;
/**
* Dispose of the resources held by the map.
*/
dispose(): void;
}
/**
* The interfaces associated with an IObservableMap.
*/
export namespace IObservableMap {
/**
* The change types which occur on an observable map.
*/
export type ChangeType =
/**
* An entry was added.
*/
| 'add'
/**
* An entry was removed.
*/
| 'remove'
/**
* An entry was changed.
*/
| 'change';
/**
* The changed args object which is emitted by an observable map.
*/
export interface IChangedArgs<T> {
/**
* The type of change undergone by the map.
*/
type: ChangeType;
/**
* The key of the change.
*/
key: string;
/**
* The old value of the change.
*/
oldValue: T | undefined;
/**
* The new value of the change.
*/
newValue: T | undefined;
}
}
/**
* A concrete implementation of IObservbleMap<T>.
*/
export class ObservableMap<T> implements IObservableMap<T> {
/**
* Construct a new observable map.
*/
constructor(options: ObservableMap.IOptions<T> = {}) {
this._itemCmp = options.itemCmp || Private.itemCmp;
if (options.values) {
for (let key in options.values) {
this._map.set(key, options.values[key]);
}
}
}
/**
* The type of the Observable.
*/
get type(): 'Map' {
return 'Map';
}
/**
* A signal emitted when the map has changed.
*/
get changed(): ISignal<this, IObservableMap.IChangedArgs<T>> {
return this._changed;
}
/**
* Whether this map has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* The number of key-value pairs in the map.
*/
get size(): number {
return this._map.size;
}
/**
* Set a key-value pair in the map
*
* @param key - The key to set.
*
* @param value - The value for the key.
*
* @returns the old value for the key, or undefined
* if that did not exist.
*
* @throws if the new value is undefined.
*
* #### Notes
* This is a no-op if the value does not change.
*/
set(key: string, value: T): T | undefined {
let oldVal = this._map.get(key);
if (value === undefined) {
throw Error('Cannot set an undefined value, use remove');
}
// Bail if the value does not change.
let itemCmp = this._itemCmp;
if (oldVal !== undefined && itemCmp(oldVal, value)) {
return oldVal;
}
this._map.set(key, value);
this._changed.emit({
type: oldVal ? 'change' : 'add',
key: key,
oldValue: oldVal,
newValue: value
});
return oldVal;
}
/**
* Get a value for a given key.
*
* @param key - the key.
*
* @returns the value for that key.
*/
get(key: string): T | undefined {
return this._map.get(key);
}
/**
* Check whether the map has a key.
*
* @param key - the key to check.
*
* @returns `true` if the map has the key, `false` otherwise.
*/
has(key: string): boolean {
return this._map.has(key);
}
/**
* Get a list of the keys in the map.
*
* @returns - a list of keys.
*/
keys(): string[] {
let keyList: string[] = [];
this._map.forEach((v: T, k: string) => {
keyList.push(k);
});
return keyList;
}
/**
* Get a list of the values in the map.
*
* @returns - a list of values.
*/
values(): T[] {
let valList: T[] = [];
this._map.forEach((v: T, k: string) => {
valList.push(v);
});
return valList;
}
/**
* Remove a key from the map
*
* @param key - the key to remove.
*
* @returns the value of the given key,
* or undefined if that does not exist.
*/
delete(key: string): T | undefined {
let oldVal = this._map.get(key);
this._map.delete(key);
this._changed.emit({
type: 'remove',
key: key,
oldValue: oldVal,
newValue: undefined
});
return oldVal;
}
/**
* Set the ObservableMap to an empty map.
*/
clear(): void {
// Delete one by one to emit the correct signals.
let keyList = this.keys();
for (let i = 0; i < keyList.length; i++) {
this.delete(keyList[i]);
}
}
/**
* Dispose of the resources held by the map.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
this._map.clear();
}
private _map: Map<string, T> = new Map<string, T>();
private _itemCmp: (first: T, second: T) => boolean;
private _changed = new Signal<this, IObservableMap.IChangedArgs<T>>(this);
private _isDisposed = false;
}
/**
* The namespace for `ObservableMap` class statics.
*/
export namespace ObservableMap {
/**
* The options used to initialize an observable map.
*/
export interface IOptions<T> {
/**
* An optional initial set of values.
*/
values?: { [key: string]: T };
/**
* The item comparison function for change detection on `set`.
*
* If not given, strict `===` equality will be used.
*/
itemCmp?: (first: T, second: T) => boolean;
}
}
/**
* The namespace for module private data.
*/
namespace Private {
/**
* The default strict equality item comparator.
*/
export function itemCmp(first: any, second: any): boolean {
return first === second;
}
}