@jupyterlab/observables 2.1.1-alpha.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +6 -6
- package/lib/index.js +15 -15
- package/lib/modeldb.d.ts +427 -427
- package/lib/modeldb.js +294 -294
- package/lib/observablejson.d.ts +61 -61
- package/lib/observablejson.js +54 -54
- package/lib/observablelist.d.ts +504 -504
- package/lib/observablelist.js +384 -384
- package/lib/observablemap.d.ts +226 -226
- package/lib/observablemap.js +180 -180
- package/lib/observablestring.d.ts +147 -147
- package/lib/observablestring.js +109 -109
- package/lib/undoablelist.d.ts +132 -132
- package/lib/undoablelist.js +228 -228
- package/package.json +7 -4
package/lib/observablelist.js
CHANGED
|
@@ -1,384 +1,384 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright (c) Jupyter Development Team.
|
|
3
|
-
// Distributed under the terms of the Modified BSD License.
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
const algorithm_1 = require("@phosphor/algorithm");
|
|
6
|
-
const signaling_1 = require("@phosphor/signaling");
|
|
7
|
-
/**
|
|
8
|
-
* A concrete implementation of [[IObservableList]].
|
|
9
|
-
*/
|
|
10
|
-
class ObservableList {
|
|
11
|
-
/**
|
|
12
|
-
* Construct a new observable map.
|
|
13
|
-
*/
|
|
14
|
-
constructor(options = {}) {
|
|
15
|
-
this._array = [];
|
|
16
|
-
this._isDisposed = false;
|
|
17
|
-
this._changed = new signaling_1.Signal(this);
|
|
18
|
-
if (options.values !== void 0) {
|
|
19
|
-
algorithm_1.each(options.values, value => {
|
|
20
|
-
this._array.push(value);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
this._itemCmp = options.itemCmp || Private.itemCmp;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* The type of this object.
|
|
27
|
-
*/
|
|
28
|
-
get type() {
|
|
29
|
-
return 'List';
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* A signal emitted when the list has changed.
|
|
33
|
-
*/
|
|
34
|
-
get changed() {
|
|
35
|
-
return this._changed;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* The length of the list.
|
|
39
|
-
*/
|
|
40
|
-
get length() {
|
|
41
|
-
return this._array.length;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Test whether the list has been disposed.
|
|
45
|
-
*/
|
|
46
|
-
get isDisposed() {
|
|
47
|
-
return this._isDisposed;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Dispose of the resources held by the list.
|
|
51
|
-
*/
|
|
52
|
-
dispose() {
|
|
53
|
-
if (this._isDisposed) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
this._isDisposed = true;
|
|
57
|
-
signaling_1.Signal.clearData(this);
|
|
58
|
-
this.clear();
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Create an iterator over the values in the list.
|
|
62
|
-
*
|
|
63
|
-
* @returns A new iterator starting at the front of the list.
|
|
64
|
-
*
|
|
65
|
-
* #### Complexity
|
|
66
|
-
* Constant.
|
|
67
|
-
*
|
|
68
|
-
* #### Iterator Validity
|
|
69
|
-
* No changes.
|
|
70
|
-
*/
|
|
71
|
-
iter() {
|
|
72
|
-
return new algorithm_1.ArrayIterator(this._array);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Get the value at the specified index.
|
|
76
|
-
*
|
|
77
|
-
* @param index - The positive integer index of interest.
|
|
78
|
-
*
|
|
79
|
-
* @returns The value at the specified index.
|
|
80
|
-
*
|
|
81
|
-
* #### Undefined Behavior
|
|
82
|
-
* An `index` which is non-integral or out of range.
|
|
83
|
-
*/
|
|
84
|
-
get(index) {
|
|
85
|
-
return this._array[index];
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Set the value at the specified index.
|
|
89
|
-
*
|
|
90
|
-
* @param index - The positive integer index of interest.
|
|
91
|
-
*
|
|
92
|
-
* @param value - The value to set at the specified index.
|
|
93
|
-
*
|
|
94
|
-
* #### Complexity
|
|
95
|
-
* Constant.
|
|
96
|
-
*
|
|
97
|
-
* #### Iterator Validity
|
|
98
|
-
* No changes.
|
|
99
|
-
*
|
|
100
|
-
* #### Undefined Behavior
|
|
101
|
-
* An `index` which is non-integral or out of range.
|
|
102
|
-
*/
|
|
103
|
-
set(index, value) {
|
|
104
|
-
let oldValue = this._array[index];
|
|
105
|
-
if (value === undefined) {
|
|
106
|
-
throw new Error('Cannot set an undefined item');
|
|
107
|
-
}
|
|
108
|
-
// Bail if the value does not change.
|
|
109
|
-
let itemCmp = this._itemCmp;
|
|
110
|
-
if (itemCmp(oldValue, value)) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
this._array[index] = value;
|
|
114
|
-
this._changed.emit({
|
|
115
|
-
type: 'set',
|
|
116
|
-
oldIndex: index,
|
|
117
|
-
newIndex: index,
|
|
118
|
-
oldValues: [oldValue],
|
|
119
|
-
newValues: [value]
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Add a value to the end of the list.
|
|
124
|
-
*
|
|
125
|
-
* @param value - The value to add to the end of the list.
|
|
126
|
-
*
|
|
127
|
-
* @returns The new length of the list.
|
|
128
|
-
*
|
|
129
|
-
* #### Complexity
|
|
130
|
-
* Constant.
|
|
131
|
-
*
|
|
132
|
-
* #### Iterator Validity
|
|
133
|
-
* No changes.
|
|
134
|
-
*/
|
|
135
|
-
push(value) {
|
|
136
|
-
let num = this._array.push(value);
|
|
137
|
-
this._changed.emit({
|
|
138
|
-
type: 'add',
|
|
139
|
-
oldIndex: -1,
|
|
140
|
-
newIndex: this.length - 1,
|
|
141
|
-
oldValues: [],
|
|
142
|
-
newValues: [value]
|
|
143
|
-
});
|
|
144
|
-
return num;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Insert a value into the list at a specific index.
|
|
148
|
-
*
|
|
149
|
-
* @param index - The index at which to insert the value.
|
|
150
|
-
*
|
|
151
|
-
* @param value - The value to set at the specified index.
|
|
152
|
-
*
|
|
153
|
-
* #### Complexity
|
|
154
|
-
* Linear.
|
|
155
|
-
*
|
|
156
|
-
* #### Iterator Validity
|
|
157
|
-
* No changes.
|
|
158
|
-
*
|
|
159
|
-
* #### Notes
|
|
160
|
-
* The `index` will be clamped to the bounds of the list.
|
|
161
|
-
*
|
|
162
|
-
* #### Undefined Behavior
|
|
163
|
-
* An `index` which is non-integral.
|
|
164
|
-
*/
|
|
165
|
-
insert(index, value) {
|
|
166
|
-
algorithm_1.ArrayExt.insert(this._array, index, value);
|
|
167
|
-
this._changed.emit({
|
|
168
|
-
type: 'add',
|
|
169
|
-
oldIndex: -1,
|
|
170
|
-
newIndex: index,
|
|
171
|
-
oldValues: [],
|
|
172
|
-
newValues: [value]
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Remove the first occurrence of a value from the list.
|
|
177
|
-
*
|
|
178
|
-
* @param value - The value of interest.
|
|
179
|
-
*
|
|
180
|
-
* @returns The index of the removed value, or `-1` if the value
|
|
181
|
-
* is not contained in the list.
|
|
182
|
-
*
|
|
183
|
-
* #### Complexity
|
|
184
|
-
* Linear.
|
|
185
|
-
*
|
|
186
|
-
* #### Iterator Validity
|
|
187
|
-
* Iterators pointing at the removed value and beyond are invalidated.
|
|
188
|
-
*/
|
|
189
|
-
removeValue(value) {
|
|
190
|
-
let itemCmp = this._itemCmp;
|
|
191
|
-
let index = algorithm_1.ArrayExt.findFirstIndex(this._array, item => {
|
|
192
|
-
return itemCmp(item, value);
|
|
193
|
-
});
|
|
194
|
-
this.remove(index);
|
|
195
|
-
return index;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Remove and return the value at a specific index.
|
|
199
|
-
*
|
|
200
|
-
* @param index - The index of the value of interest.
|
|
201
|
-
*
|
|
202
|
-
* @returns The value at the specified index, or `undefined` if the
|
|
203
|
-
* index is out of range.
|
|
204
|
-
*
|
|
205
|
-
* #### Complexity
|
|
206
|
-
* Constant.
|
|
207
|
-
*
|
|
208
|
-
* #### Iterator Validity
|
|
209
|
-
* Iterators pointing at the removed value and beyond are invalidated.
|
|
210
|
-
*
|
|
211
|
-
* #### Undefined Behavior
|
|
212
|
-
* An `index` which is non-integral.
|
|
213
|
-
*/
|
|
214
|
-
remove(index) {
|
|
215
|
-
let value = algorithm_1.ArrayExt.removeAt(this._array, index);
|
|
216
|
-
if (value === undefined) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
this._changed.emit({
|
|
220
|
-
type: 'remove',
|
|
221
|
-
oldIndex: index,
|
|
222
|
-
newIndex: -1,
|
|
223
|
-
newValues: [],
|
|
224
|
-
oldValues: [value]
|
|
225
|
-
});
|
|
226
|
-
return value;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Remove all values from the list.
|
|
230
|
-
*
|
|
231
|
-
* #### Complexity
|
|
232
|
-
* Linear.
|
|
233
|
-
*
|
|
234
|
-
* #### Iterator Validity
|
|
235
|
-
* All current iterators are invalidated.
|
|
236
|
-
*/
|
|
237
|
-
clear() {
|
|
238
|
-
let copy = this._array.slice();
|
|
239
|
-
this._array.length = 0;
|
|
240
|
-
this._changed.emit({
|
|
241
|
-
type: 'remove',
|
|
242
|
-
oldIndex: 0,
|
|
243
|
-
newIndex: 0,
|
|
244
|
-
newValues: [],
|
|
245
|
-
oldValues: copy
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Move a value from one index to another.
|
|
250
|
-
*
|
|
251
|
-
* @parm fromIndex - The index of the element to move.
|
|
252
|
-
*
|
|
253
|
-
* @param toIndex - The index to move the element to.
|
|
254
|
-
*
|
|
255
|
-
* #### Complexity
|
|
256
|
-
* Constant.
|
|
257
|
-
*
|
|
258
|
-
* #### Iterator Validity
|
|
259
|
-
* Iterators pointing at the lesser of the `fromIndex` and the `toIndex`
|
|
260
|
-
* and beyond are invalidated.
|
|
261
|
-
*
|
|
262
|
-
* #### Undefined Behavior
|
|
263
|
-
* A `fromIndex` or a `toIndex` which is non-integral.
|
|
264
|
-
*/
|
|
265
|
-
move(fromIndex, toIndex) {
|
|
266
|
-
if (this.length <= 1 || fromIndex === toIndex) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
let values = [this._array[fromIndex]];
|
|
270
|
-
algorithm_1.ArrayExt.move(this._array, fromIndex, toIndex);
|
|
271
|
-
this._changed.emit({
|
|
272
|
-
type: 'move',
|
|
273
|
-
oldIndex: fromIndex,
|
|
274
|
-
newIndex: toIndex,
|
|
275
|
-
oldValues: values,
|
|
276
|
-
newValues: values
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Push a set of values to the back of the list.
|
|
281
|
-
*
|
|
282
|
-
* @param values - An iterable or array-like set of values to add.
|
|
283
|
-
*
|
|
284
|
-
* @returns The new length of the list.
|
|
285
|
-
*
|
|
286
|
-
* #### Complexity
|
|
287
|
-
* Linear.
|
|
288
|
-
*
|
|
289
|
-
* #### Iterator Validity
|
|
290
|
-
* No changes.
|
|
291
|
-
*/
|
|
292
|
-
pushAll(values) {
|
|
293
|
-
let newIndex = this.length;
|
|
294
|
-
algorithm_1.each(values, value => {
|
|
295
|
-
this._array.push(value);
|
|
296
|
-
});
|
|
297
|
-
this._changed.emit({
|
|
298
|
-
type: 'add',
|
|
299
|
-
oldIndex: -1,
|
|
300
|
-
newIndex,
|
|
301
|
-
oldValues: [],
|
|
302
|
-
newValues: algorithm_1.toArray(values)
|
|
303
|
-
});
|
|
304
|
-
return this.length;
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Insert a set of items into the list at the specified index.
|
|
308
|
-
*
|
|
309
|
-
* @param index - The index at which to insert the values.
|
|
310
|
-
*
|
|
311
|
-
* @param values - The values to insert at the specified index.
|
|
312
|
-
*
|
|
313
|
-
* #### Complexity.
|
|
314
|
-
* Linear.
|
|
315
|
-
*
|
|
316
|
-
* #### Iterator Validity
|
|
317
|
-
* No changes.
|
|
318
|
-
*
|
|
319
|
-
* #### Notes
|
|
320
|
-
* The `index` will be clamped to the bounds of the list.
|
|
321
|
-
*
|
|
322
|
-
* #### Undefined Behavior.
|
|
323
|
-
* An `index` which is non-integral.
|
|
324
|
-
*/
|
|
325
|
-
insertAll(index, values) {
|
|
326
|
-
let newIndex = index;
|
|
327
|
-
algorithm_1.each(values, value => {
|
|
328
|
-
algorithm_1.ArrayExt.insert(this._array, index++, value);
|
|
329
|
-
});
|
|
330
|
-
this._changed.emit({
|
|
331
|
-
type: 'add',
|
|
332
|
-
oldIndex: -1,
|
|
333
|
-
newIndex,
|
|
334
|
-
oldValues: [],
|
|
335
|
-
newValues: algorithm_1.toArray(values)
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Remove a range of items from the list.
|
|
340
|
-
*
|
|
341
|
-
* @param startIndex - The start index of the range to remove (inclusive).
|
|
342
|
-
*
|
|
343
|
-
* @param endIndex - The end index of the range to remove (exclusive).
|
|
344
|
-
*
|
|
345
|
-
* @returns The new length of the list.
|
|
346
|
-
*
|
|
347
|
-
* #### Complexity
|
|
348
|
-
* Linear.
|
|
349
|
-
*
|
|
350
|
-
* #### Iterator Validity
|
|
351
|
-
* Iterators pointing to the first removed value and beyond are invalid.
|
|
352
|
-
*
|
|
353
|
-
* #### Undefined Behavior
|
|
354
|
-
* A `startIndex` or `endIndex` which is non-integral.
|
|
355
|
-
*/
|
|
356
|
-
removeRange(startIndex, endIndex) {
|
|
357
|
-
let oldValues = this._array.slice(startIndex, endIndex);
|
|
358
|
-
for (let i = startIndex; i < endIndex; i++) {
|
|
359
|
-
algorithm_1.ArrayExt.removeAt(this._array, startIndex);
|
|
360
|
-
}
|
|
361
|
-
this._changed.emit({
|
|
362
|
-
type: 'remove',
|
|
363
|
-
oldIndex: startIndex,
|
|
364
|
-
newIndex: -1,
|
|
365
|
-
oldValues,
|
|
366
|
-
newValues: []
|
|
367
|
-
});
|
|
368
|
-
return this.length;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
exports.ObservableList = ObservableList;
|
|
372
|
-
/**
|
|
373
|
-
* The namespace for module private data.
|
|
374
|
-
*/
|
|
375
|
-
var Private;
|
|
376
|
-
(function (Private) {
|
|
377
|
-
/**
|
|
378
|
-
* The default strict equality item cmp.
|
|
379
|
-
*/
|
|
380
|
-
function itemCmp(first, second) {
|
|
381
|
-
return first === second;
|
|
382
|
-
}
|
|
383
|
-
Private.itemCmp = itemCmp;
|
|
384
|
-
})(Private || (Private = {}));
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Jupyter Development Team.
|
|
3
|
+
// Distributed under the terms of the Modified BSD License.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
const algorithm_1 = require("@phosphor/algorithm");
|
|
6
|
+
const signaling_1 = require("@phosphor/signaling");
|
|
7
|
+
/**
|
|
8
|
+
* A concrete implementation of [[IObservableList]].
|
|
9
|
+
*/
|
|
10
|
+
class ObservableList {
|
|
11
|
+
/**
|
|
12
|
+
* Construct a new observable map.
|
|
13
|
+
*/
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this._array = [];
|
|
16
|
+
this._isDisposed = false;
|
|
17
|
+
this._changed = new signaling_1.Signal(this);
|
|
18
|
+
if (options.values !== void 0) {
|
|
19
|
+
algorithm_1.each(options.values, value => {
|
|
20
|
+
this._array.push(value);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
this._itemCmp = options.itemCmp || Private.itemCmp;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The type of this object.
|
|
27
|
+
*/
|
|
28
|
+
get type() {
|
|
29
|
+
return 'List';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A signal emitted when the list has changed.
|
|
33
|
+
*/
|
|
34
|
+
get changed() {
|
|
35
|
+
return this._changed;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The length of the list.
|
|
39
|
+
*/
|
|
40
|
+
get length() {
|
|
41
|
+
return this._array.length;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Test whether the list has been disposed.
|
|
45
|
+
*/
|
|
46
|
+
get isDisposed() {
|
|
47
|
+
return this._isDisposed;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Dispose of the resources held by the list.
|
|
51
|
+
*/
|
|
52
|
+
dispose() {
|
|
53
|
+
if (this._isDisposed) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this._isDisposed = true;
|
|
57
|
+
signaling_1.Signal.clearData(this);
|
|
58
|
+
this.clear();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create an iterator over the values in the list.
|
|
62
|
+
*
|
|
63
|
+
* @returns A new iterator starting at the front of the list.
|
|
64
|
+
*
|
|
65
|
+
* #### Complexity
|
|
66
|
+
* Constant.
|
|
67
|
+
*
|
|
68
|
+
* #### Iterator Validity
|
|
69
|
+
* No changes.
|
|
70
|
+
*/
|
|
71
|
+
iter() {
|
|
72
|
+
return new algorithm_1.ArrayIterator(this._array);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the value at the specified index.
|
|
76
|
+
*
|
|
77
|
+
* @param index - The positive integer index of interest.
|
|
78
|
+
*
|
|
79
|
+
* @returns The value at the specified index.
|
|
80
|
+
*
|
|
81
|
+
* #### Undefined Behavior
|
|
82
|
+
* An `index` which is non-integral or out of range.
|
|
83
|
+
*/
|
|
84
|
+
get(index) {
|
|
85
|
+
return this._array[index];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Set the value at the specified index.
|
|
89
|
+
*
|
|
90
|
+
* @param index - The positive integer index of interest.
|
|
91
|
+
*
|
|
92
|
+
* @param value - The value to set at the specified index.
|
|
93
|
+
*
|
|
94
|
+
* #### Complexity
|
|
95
|
+
* Constant.
|
|
96
|
+
*
|
|
97
|
+
* #### Iterator Validity
|
|
98
|
+
* No changes.
|
|
99
|
+
*
|
|
100
|
+
* #### Undefined Behavior
|
|
101
|
+
* An `index` which is non-integral or out of range.
|
|
102
|
+
*/
|
|
103
|
+
set(index, value) {
|
|
104
|
+
let oldValue = this._array[index];
|
|
105
|
+
if (value === undefined) {
|
|
106
|
+
throw new Error('Cannot set an undefined item');
|
|
107
|
+
}
|
|
108
|
+
// Bail if the value does not change.
|
|
109
|
+
let itemCmp = this._itemCmp;
|
|
110
|
+
if (itemCmp(oldValue, value)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this._array[index] = value;
|
|
114
|
+
this._changed.emit({
|
|
115
|
+
type: 'set',
|
|
116
|
+
oldIndex: index,
|
|
117
|
+
newIndex: index,
|
|
118
|
+
oldValues: [oldValue],
|
|
119
|
+
newValues: [value]
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Add a value to the end of the list.
|
|
124
|
+
*
|
|
125
|
+
* @param value - The value to add to the end of the list.
|
|
126
|
+
*
|
|
127
|
+
* @returns The new length of the list.
|
|
128
|
+
*
|
|
129
|
+
* #### Complexity
|
|
130
|
+
* Constant.
|
|
131
|
+
*
|
|
132
|
+
* #### Iterator Validity
|
|
133
|
+
* No changes.
|
|
134
|
+
*/
|
|
135
|
+
push(value) {
|
|
136
|
+
let num = this._array.push(value);
|
|
137
|
+
this._changed.emit({
|
|
138
|
+
type: 'add',
|
|
139
|
+
oldIndex: -1,
|
|
140
|
+
newIndex: this.length - 1,
|
|
141
|
+
oldValues: [],
|
|
142
|
+
newValues: [value]
|
|
143
|
+
});
|
|
144
|
+
return num;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Insert a value into the list at a specific index.
|
|
148
|
+
*
|
|
149
|
+
* @param index - The index at which to insert the value.
|
|
150
|
+
*
|
|
151
|
+
* @param value - The value to set at the specified index.
|
|
152
|
+
*
|
|
153
|
+
* #### Complexity
|
|
154
|
+
* Linear.
|
|
155
|
+
*
|
|
156
|
+
* #### Iterator Validity
|
|
157
|
+
* No changes.
|
|
158
|
+
*
|
|
159
|
+
* #### Notes
|
|
160
|
+
* The `index` will be clamped to the bounds of the list.
|
|
161
|
+
*
|
|
162
|
+
* #### Undefined Behavior
|
|
163
|
+
* An `index` which is non-integral.
|
|
164
|
+
*/
|
|
165
|
+
insert(index, value) {
|
|
166
|
+
algorithm_1.ArrayExt.insert(this._array, index, value);
|
|
167
|
+
this._changed.emit({
|
|
168
|
+
type: 'add',
|
|
169
|
+
oldIndex: -1,
|
|
170
|
+
newIndex: index,
|
|
171
|
+
oldValues: [],
|
|
172
|
+
newValues: [value]
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Remove the first occurrence of a value from the list.
|
|
177
|
+
*
|
|
178
|
+
* @param value - The value of interest.
|
|
179
|
+
*
|
|
180
|
+
* @returns The index of the removed value, or `-1` if the value
|
|
181
|
+
* is not contained in the list.
|
|
182
|
+
*
|
|
183
|
+
* #### Complexity
|
|
184
|
+
* Linear.
|
|
185
|
+
*
|
|
186
|
+
* #### Iterator Validity
|
|
187
|
+
* Iterators pointing at the removed value and beyond are invalidated.
|
|
188
|
+
*/
|
|
189
|
+
removeValue(value) {
|
|
190
|
+
let itemCmp = this._itemCmp;
|
|
191
|
+
let index = algorithm_1.ArrayExt.findFirstIndex(this._array, item => {
|
|
192
|
+
return itemCmp(item, value);
|
|
193
|
+
});
|
|
194
|
+
this.remove(index);
|
|
195
|
+
return index;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Remove and return the value at a specific index.
|
|
199
|
+
*
|
|
200
|
+
* @param index - The index of the value of interest.
|
|
201
|
+
*
|
|
202
|
+
* @returns The value at the specified index, or `undefined` if the
|
|
203
|
+
* index is out of range.
|
|
204
|
+
*
|
|
205
|
+
* #### Complexity
|
|
206
|
+
* Constant.
|
|
207
|
+
*
|
|
208
|
+
* #### Iterator Validity
|
|
209
|
+
* Iterators pointing at the removed value and beyond are invalidated.
|
|
210
|
+
*
|
|
211
|
+
* #### Undefined Behavior
|
|
212
|
+
* An `index` which is non-integral.
|
|
213
|
+
*/
|
|
214
|
+
remove(index) {
|
|
215
|
+
let value = algorithm_1.ArrayExt.removeAt(this._array, index);
|
|
216
|
+
if (value === undefined) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this._changed.emit({
|
|
220
|
+
type: 'remove',
|
|
221
|
+
oldIndex: index,
|
|
222
|
+
newIndex: -1,
|
|
223
|
+
newValues: [],
|
|
224
|
+
oldValues: [value]
|
|
225
|
+
});
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Remove all values from the list.
|
|
230
|
+
*
|
|
231
|
+
* #### Complexity
|
|
232
|
+
* Linear.
|
|
233
|
+
*
|
|
234
|
+
* #### Iterator Validity
|
|
235
|
+
* All current iterators are invalidated.
|
|
236
|
+
*/
|
|
237
|
+
clear() {
|
|
238
|
+
let copy = this._array.slice();
|
|
239
|
+
this._array.length = 0;
|
|
240
|
+
this._changed.emit({
|
|
241
|
+
type: 'remove',
|
|
242
|
+
oldIndex: 0,
|
|
243
|
+
newIndex: 0,
|
|
244
|
+
newValues: [],
|
|
245
|
+
oldValues: copy
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Move a value from one index to another.
|
|
250
|
+
*
|
|
251
|
+
* @parm fromIndex - The index of the element to move.
|
|
252
|
+
*
|
|
253
|
+
* @param toIndex - The index to move the element to.
|
|
254
|
+
*
|
|
255
|
+
* #### Complexity
|
|
256
|
+
* Constant.
|
|
257
|
+
*
|
|
258
|
+
* #### Iterator Validity
|
|
259
|
+
* Iterators pointing at the lesser of the `fromIndex` and the `toIndex`
|
|
260
|
+
* and beyond are invalidated.
|
|
261
|
+
*
|
|
262
|
+
* #### Undefined Behavior
|
|
263
|
+
* A `fromIndex` or a `toIndex` which is non-integral.
|
|
264
|
+
*/
|
|
265
|
+
move(fromIndex, toIndex) {
|
|
266
|
+
if (this.length <= 1 || fromIndex === toIndex) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
let values = [this._array[fromIndex]];
|
|
270
|
+
algorithm_1.ArrayExt.move(this._array, fromIndex, toIndex);
|
|
271
|
+
this._changed.emit({
|
|
272
|
+
type: 'move',
|
|
273
|
+
oldIndex: fromIndex,
|
|
274
|
+
newIndex: toIndex,
|
|
275
|
+
oldValues: values,
|
|
276
|
+
newValues: values
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Push a set of values to the back of the list.
|
|
281
|
+
*
|
|
282
|
+
* @param values - An iterable or array-like set of values to add.
|
|
283
|
+
*
|
|
284
|
+
* @returns The new length of the list.
|
|
285
|
+
*
|
|
286
|
+
* #### Complexity
|
|
287
|
+
* Linear.
|
|
288
|
+
*
|
|
289
|
+
* #### Iterator Validity
|
|
290
|
+
* No changes.
|
|
291
|
+
*/
|
|
292
|
+
pushAll(values) {
|
|
293
|
+
let newIndex = this.length;
|
|
294
|
+
algorithm_1.each(values, value => {
|
|
295
|
+
this._array.push(value);
|
|
296
|
+
});
|
|
297
|
+
this._changed.emit({
|
|
298
|
+
type: 'add',
|
|
299
|
+
oldIndex: -1,
|
|
300
|
+
newIndex,
|
|
301
|
+
oldValues: [],
|
|
302
|
+
newValues: algorithm_1.toArray(values)
|
|
303
|
+
});
|
|
304
|
+
return this.length;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Insert a set of items into the list at the specified index.
|
|
308
|
+
*
|
|
309
|
+
* @param index - The index at which to insert the values.
|
|
310
|
+
*
|
|
311
|
+
* @param values - The values to insert at the specified index.
|
|
312
|
+
*
|
|
313
|
+
* #### Complexity.
|
|
314
|
+
* Linear.
|
|
315
|
+
*
|
|
316
|
+
* #### Iterator Validity
|
|
317
|
+
* No changes.
|
|
318
|
+
*
|
|
319
|
+
* #### Notes
|
|
320
|
+
* The `index` will be clamped to the bounds of the list.
|
|
321
|
+
*
|
|
322
|
+
* #### Undefined Behavior.
|
|
323
|
+
* An `index` which is non-integral.
|
|
324
|
+
*/
|
|
325
|
+
insertAll(index, values) {
|
|
326
|
+
let newIndex = index;
|
|
327
|
+
algorithm_1.each(values, value => {
|
|
328
|
+
algorithm_1.ArrayExt.insert(this._array, index++, value);
|
|
329
|
+
});
|
|
330
|
+
this._changed.emit({
|
|
331
|
+
type: 'add',
|
|
332
|
+
oldIndex: -1,
|
|
333
|
+
newIndex,
|
|
334
|
+
oldValues: [],
|
|
335
|
+
newValues: algorithm_1.toArray(values)
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Remove a range of items from the list.
|
|
340
|
+
*
|
|
341
|
+
* @param startIndex - The start index of the range to remove (inclusive).
|
|
342
|
+
*
|
|
343
|
+
* @param endIndex - The end index of the range to remove (exclusive).
|
|
344
|
+
*
|
|
345
|
+
* @returns The new length of the list.
|
|
346
|
+
*
|
|
347
|
+
* #### Complexity
|
|
348
|
+
* Linear.
|
|
349
|
+
*
|
|
350
|
+
* #### Iterator Validity
|
|
351
|
+
* Iterators pointing to the first removed value and beyond are invalid.
|
|
352
|
+
*
|
|
353
|
+
* #### Undefined Behavior
|
|
354
|
+
* A `startIndex` or `endIndex` which is non-integral.
|
|
355
|
+
*/
|
|
356
|
+
removeRange(startIndex, endIndex) {
|
|
357
|
+
let oldValues = this._array.slice(startIndex, endIndex);
|
|
358
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
359
|
+
algorithm_1.ArrayExt.removeAt(this._array, startIndex);
|
|
360
|
+
}
|
|
361
|
+
this._changed.emit({
|
|
362
|
+
type: 'remove',
|
|
363
|
+
oldIndex: startIndex,
|
|
364
|
+
newIndex: -1,
|
|
365
|
+
oldValues,
|
|
366
|
+
newValues: []
|
|
367
|
+
});
|
|
368
|
+
return this.length;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
exports.ObservableList = ObservableList;
|
|
372
|
+
/**
|
|
373
|
+
* The namespace for module private data.
|
|
374
|
+
*/
|
|
375
|
+
var Private;
|
|
376
|
+
(function (Private) {
|
|
377
|
+
/**
|
|
378
|
+
* The default strict equality item cmp.
|
|
379
|
+
*/
|
|
380
|
+
function itemCmp(first, second) {
|
|
381
|
+
return first === second;
|
|
382
|
+
}
|
|
383
|
+
Private.itemCmp = itemCmp;
|
|
384
|
+
})(Private || (Private = {}));
|