This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
realtimevector.ts
691 lines (632 loc) · 15.4 KB
/
realtimevector.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ArrayIterator, IterableOrArrayLike,
IIterator, each, toArray
} from 'phosphor/lib/algorithm/iteration';
import {
indexOf
} from 'phosphor/lib/algorithm/searching';
import {
JSONObject
} from 'phosphor/lib/algorithm/json';
import {
clearSignalData, defineSignal, ISignal
} from 'phosphor/lib/core/signaling';
import {
IObservableVector, ObservableVector
} from 'jupyterlab/lib/common/observablevector';
import {
IObservableUndoableVector,
} from 'jupyterlab/lib/notebook/common/undo';
import {
ISynchronizable
} from 'jupyterlab/lib/realtime';
declare let gapi : any;
export
class GoogleRealtimeVector<T extends ISynchronizable<T>> implements IObservableUndoableVector<T> {
constructor(factory: (value: JSONObject)=>T, model : any, id : string, initialValue?: IObservableVector<T>) {
this._factory = factory;
//Create and populate the internal vectors
this._vec = new ObservableVector<T>();
this._gvec = model.getRoot().get(id);
if(!this._gvec) {
//Does not exist, use initial values
this._gvec = model.createList(this._toJSONArray(toArray(initialValue)));
model.getRoot().set(id, this._gvec);
for(let i=0; i < initialValue.length; i++) {
let val: T = initialValue.at(i);
this._connectToSync(val);
this._vec.pushBack(val);
}
} else {
//Already exists, populate with that.
let vals = this._gvec.asArray();
for(let i=0; i < this._gvec.length; i++) {
this._vec.pushBack(this._createFromJSON(this._gvec.get(i)));
}
}
//Add event listeners to the collaborativeVector
this._gvec.addEventListener(
gapi.drive.realtime.EventType.VALUES_ADDED,
(evt : any) => {
let vals: T[] = this._fromJSONArray(evt.values);
if(!evt.isLocal) {
this._vec.insertAll(evt.index, vals);
this.changed.emit({
type: 'add',
oldIndex: -1,
newIndex: evt.index,
oldValues: [],
newValues: vals
});
}
});
this._gvec.addEventListener(
gapi.drive.realtime.EventType.VALUES_REMOVED,
(evt : any) => {
let vals: T[] = this._fromJSONArray(evt.values);
if(!evt.isLocal) {
this._vec.removeRange(evt.index, evt.index+vals.length);
this.changed.emit({
type: 'remove',
oldIndex: evt.index,
newIndex: -1,
oldValues: vals,
newValues: []
});
}
});
this._gvec.addEventListener(
gapi.drive.realtime.EventType.VALUES_SET,
(evt : any) => {
let oldVals: T[] = this._fromJSONArray(evt.oldValues);
let newVals: T[] = this._fromJSONArray(evt.newValues);
if(!evt.isLocal) {
for(let i=0; i<oldVals.length; i++) {
this._vec.set(evt.index+i, newVals[i]);
}
this.changed.emit({
type: 'set',
oldIndex: evt.index,
newIndex: evt.index,
oldValues: oldVals,
newValues: newVals
});
}
});
}
/**
* A signal emitted when the vector has changed.
*/
changed: ISignal<IObservableVector<T>, ObservableVector.IChangedArgs<T>>;
/**
* Whether this string is linkable.
*
* @returns `false'
*/
readonly isLinkable: boolean = false;
/**
* Get whether this vector can is linked to another.
*
* @returns `false`.
*/
readonly isLinked: boolean = false;
/**
* The length of the sequence.
*
* #### Notes
* This is a read-only property.
*/
get length(): number {
return this._vec.length;
}
/**
* Test whether the vector is empty.
*
* @returns `true` if the vector is empty, `false` otherwise.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
get isEmpty(): boolean {
return this.length === 0;
}
/**
* Whether the object can redo changes.
*/
get canRedo(): boolean {
return false;
}
/**
* Whether the object can undo changes.
*/
get canUndo(): boolean {
return false;
}
/**
* Get the factory object for deserialization.
*/
get factory(): (value: JSONObject)=>T {
return this._factory;
}
/**
* Begin a compound operation.
*
* @param isUndoAble - Whether the operation is undoable.
* The default is `false`.
*/
beginCompoundOperation(isUndoAble?: boolean): void {
//no-op
}
/**
* End a compound operation.
*/
endCompoundOperation(): void {
//no-op
}
/**
* Undo an operation.
*/
undo(): void {
//no-op
}
/**
* Redo an operation.
*/
redo(): void {
//no-op
}
/**
* Clear the change stack.
*/
clearUndo(): void {
//no-op
}
/**
* Link the vector to another vector.
* Any changes to either are mirrored in the other.
*
* @param vec: the parent vector.
*/
link(vec: IObservableVector<T>): void {
//no-op
}
/**
* Unlink the vector from its parent vector.
*/
unlink(): void {
//no-op
}
/**
* Get the value at the front of the vector.
*
* @returns The value at the front of the vector, or `undefined` if
* the vector is empty.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
get front(): T {
return this.at(0);
}
/**
* Get the value at the back of the vector.
*
* @returns The value at the back of the vector, or `undefined` if
* the vector is empty.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
get back(): T {
return this.at(this.length-1);
}
/**
* Create an iterator over the values in the vector.
*
* @returns A new iterator starting at the front of the vector.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
iter(): IIterator<T> {
return this._vec.iter();
}
/**
* Get the value at the specified index.
*
* @param index - The positive integer index of interest.
*
* @returns The value at the specified index.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*
* #### Undefined Behavior
* An `index` which is non-integral or out of range.
*/
at(index: number): T {
return this._vec.at(index);
}
/**
* Set the value at the specified index.
*
* @param index - The positive integer index of interest.
*
* @param value - The value to set at the specified index.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*
* #### Undefined Behavior
* An `index` which is non-integral or out of range.
*/
set(index: number, value: T): void {
let oldVal: T = this._vec.at(index);
this._vec.set(index, value);
this._gvec.set(index, value.toJSON());
this.changed.emit({
type: 'set',
oldIndex: index,
newIndex: index,
oldValues: [oldVal],
newValues: [value]
});
}
/**
* Add a value to the back of the vector.
*
* @param value - The value to add to the back of the vector.
*
* @returns The new length of the vector.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
pushBack(value: T): number {
let len = this._vec.pushBack(value);
this._connectToSync(value);
this._gvec.push(value.toJSON());
this.changed.emit({
type: 'add',
oldIndex: -1,
newIndex: this.length - 1,
oldValues: [],
newValues: [value]
});
return len;
}
/**
* Remove and return the value at the back of the vector.
*
* @returns The value at the back of the vector, or `undefined` if
* the vector is empty.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* Iterators pointing at the removed value are invalidated.
*/
popBack(): T {
let last = this.length-1;
let value = this.at(last);
this._vec.removeAt(last);
this._gvec.remove(last);
this.changed.emit({
type: 'remove',
oldIndex: this.length,
newIndex: -1,
oldValues: [value],
newValues: []
});
return value;
}
/**
* Insert a value into the vector at a specific index.
*
* @param index - The index at which to insert the value.
*
* @param value - The value to set at the specified index.
*
* @returns The new length of the vector.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* The `index` will be clamped to the bounds of the vector.
*
* #### Undefined Behavior
* An `index` which is non-integral.
*/
insert(index: number, value: T): number {
this._vec.insert(index, value);
this._gvec.insert(index, value.toJSON());
this._connectToSync(value);
this.changed.emit({
type: 'add',
oldIndex: -1,
newIndex: index,
oldValues: [],
newValues: [value]
});
return this.length;
}
/**
* Remove the first occurrence of a value from the vector.
*
* @param value - The value of interest.
*
* @returns The index of the removed value, or `-1` if the value
* is not contained in the vector.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* Iterators pointing at the removed value and beyond are invalidated.
*
* #### Notes
* Comparison is performed using strict `===` equality.
*/
remove(value: T): number {
let index = indexOf(this._vec, value);
this.removeAt(index);
return index;
}
/**
* Remove and return the value at a specific index.
*
* @param index - The index of the value of interest.
*
* @returns The value at the specified index, or `undefined` if the
* index is out of range.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* Iterators pointing at the removed value and beyond are invalidated.
*
* #### Undefined Behavior
* An `index` which is non-integral.
*/
removeAt(index: number): T {
let value = this.at(index);
this._vec.removeAt(index);
this._gvec.remove(index);
this.changed.emit({
type: 'remove',
oldIndex: index,
newIndex: -1,
oldValues: [value],
newValues: []
});
return value;
}
/**
* Remove all values from the vector.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* All current iterators are invalidated.
*/
clear(): void {
let oldValues = toArray(this._vec);
this._vec.clear();
this._gvec.clear();
this.changed.emit({
type: 'remove',
oldIndex: 0,
newIndex: 0,
oldValues,
newValues: []
});
}
/**
* Move a value from one index to another.
*
* @parm fromIndex - The index of the element to move.
*
* @param toIndex - The index to move the element to.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* Iterators pointing at the lesser of the `fromIndex` and the `toIndex`
* and beyond are invalidated.
*
* #### Undefined Behavior
* A `fromIndex` or a `toIndex` which is non-integral.
*/
move(fromIndex: number, toIndex: number): void {
let value = this.at(fromIndex);
this.removeAt(fromIndex);
if (toIndex < fromIndex) {
this.insert(toIndex - 1, value);
} else {
this.insert(toIndex, value);
}
this.changed.emit({
type: 'move',
oldIndex: fromIndex,
newIndex: toIndex,
oldValues: [value],
newValues: [value]
});
}
/**
* Push a set of values to the back of the vector.
*
* @param values - An iterable or array-like set of values to add.
*
* @returns The new length of the vector.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* No changes.
*/
pushAll(values: IterableOrArrayLike<T>): number {
let newIndex = this.length;
let newValues = toArray(values);
each(newValues, value => { this.pushBack(value); });
this.changed.emit({
type: 'add',
oldIndex: -1,
newIndex,
oldValues: [],
newValues
});
return this.length;
}
/**
* Insert a set of items into the vector at the specified index.
*
* @param index - The index at which to insert the values.
*
* @param values - The values to insert at the specified index.
*
* @returns The new length of the vector.
*
* #### Complexity.
* Linear.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* The `index` will be clamped to the bounds of the vector.
*
* #### Undefined Behavior.
* An `index` which is non-integral.
*/
insertAll(index: number, values: IterableOrArrayLike<T>): number {
let newIndex = index;
let newValues = toArray(values);
each(newValues, value => { this.insert(index++, value); });
this.changed.emit({
type: 'add',
oldIndex: -1,
newIndex,
oldValues: [],
newValues
});
return this.length;
}
/**
* Remove a range of items from the vector.
*
* @param startIndex - The start index of the range to remove (inclusive).
*
* @param endIndex - The end index of the range to remove (exclusive).
*
* @returns The new length of the vector.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* Iterators pointing to the first removed value and beyond are invalid.
*
* #### Undefined Behavior
* A `startIndex` or `endIndex` which is non-integral.
*/
removeRange(startIndex: number, endIndex: number): number {
let oldValues: T[] = [];
for (let i = startIndex; i < endIndex; i++) {
oldValues.push(this.removeAt(startIndex));
}
this.changed.emit({
type: 'remove',
oldIndex: startIndex,
newIndex: -1,
oldValues,
newValues: []
});
return this.length;
}
/**
* Test whether the string has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the string.
*/
dispose(): void {
if(this._isDisposed) {
return;
}
this._gvec.removeAllEventListeners();
this._vec.dispose();
clearSignalData(this);
this._isDisposed = true;
}
private _toJSONArray( array: T[] ): JSONObject[] {
let ret: JSONObject[] = [];
array.forEach( val => {
ret.push(val.toJSON());
});
return ret;
}
private _fromJSONArray( array: JSONObject[] ): T[] {
let ret: T[] = [];
array.forEach( val => {
ret.push(this._createFromJSON(val));
});
return ret;
}
private _connectToSync( value: T ): void {
value.synchronizeRequest.connect( ()=>{
let index = indexOf(this._vec, value);
this._gvec.set(index, value.toJSON());
});
}
private _createFromJSON (value: JSONObject): T {
let val: T = this._factory(value);
this._connectToSync(val);
return val;
}
private _factory: (value: JSONObject) => T = null;
private _model : gapi.drive.realtime.Model = null;
//Google collaborativeList of JSONObjects that shadows the ObservableVector
//which represents the canonical vector of objects.
private _gvec : gapi.drive.realtime.CollaborativeList<JSONObject> = null;
//Canonical vector of objects.
private _vec: ObservableVector<T> = null;
private _isDisposed : boolean = false;
}
// Define the signals for the Google realtime vector.
defineSignal(GoogleRealtimeVector.prototype, 'changed');