This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdirty_checking.js
1070 lines (901 loc) · 29 KB
/
dirty_checking.js
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* these cannot currently be defined in the DirtyCheckingRecord class itself,
* unfortunately. They've been moved outside and de-const-ified for this
* reason. When a better approach is found, it will be used instead.
*/
var _MODE_NAMES = [
'MARKER', 'IDENT', 'REFLECT', 'GETTER', 'MAP[]', 'ITERABLE', 'MAP'
];
var _MODE_MARKER_ = 0;
var _MODE_IDENTITY_ = 1;
var _MODE_REFLECT_ = 2;
var _MODE_GETTER_ = 3;
var _MODE_MAP_FIELD_ = 4;
var _MODE_ITERABLE_ = 5;
var _MODE_MAP_ = 6;
var _NOT_NOTIFIED_= 10;
var _NOTIFIED_= 11;
export class GetterCache {
constructor(map) {
this._map = map;
}
get(field) {
return this._map[field] || null;
}
}
export class ChangeRecordIterator {
constructor(next) {
this.current = null;
this._next = next;
}
iterate() {
this.current = this._next;
if (this._next !== null) {
this._next = this.current.nextChange;
/**
* This is important to prevent memory leaks. If the nextChange record is not reset, then a
* record may be pointing to a deleted change detector group, and it will not release the
* reference until it fires again. So we have to be eager about releasing references.
*/
this.current.nextChange = null;
}
return this.current !== null;
}
}
export class ChangeRecord {
constructor(group, object, fieldName, getter, handler) {
this._group = group;
this._getter = getter;
this.handler = handler;
this.field = fieldName;
// Do we really need reflection here?
// this._symbol = fieldName === null ? null : new Symbol(fieldName);
this.object = object;
this.nextRecord = this.prevRecord = this.nextChange = null;
}
static marker() {
var record = new ChangeRecord(null, null, null, null, null);
record._mode = _MODE_MARKER_;
record.isMarker = true;
return record;
}
_clearObject() {
if(this._observer){
this._observer.close();
this._observer = null;
}
this._object = null;
}
get object() {
return this._object;
}
set object(obj) {
this._clearObject(obj);
this._object = obj;
if (obj === null) {
this._mode = _MODE_IDENTITY_;
return;
}
if (this.field === null) {
// _instanceMirror = null; --- Again, do we need reflection?
if (typeof obj === "object") {
if (Array.isArray(obj)) { // TODO: Browser compat, cross-script context support, perf
if (this._mode !== _MODE_ITERABLE_) {
// Last one was collection as well, don't reset state.
this._mode = _MODE_ITERABLE_;
this.currentValue = new CollectionChangeRecord();
}
} else if (this._mode !== _MODE_MAP_) {
// Last one was collection as well, don't reset state.
this._mode = _MODE_MAP_;
this.currentValue = new MapChangeRecord();
}
} else {
this._mode = _MODE_IDENTITY_;
}
return;
}
this._observer = this._group && this._group._rootGroup.getObserver(obj, this.field);
if(this._observer){
this._mode = _NOTIFIED_;
this.newValue = this._observer.open((value) =>{
this.newValue = value;
this._mode = _NOTIFIED_;
});
}else if(this._getter !== null){
this._mode = _MODE_GETTER_;
}else{
this._mode = _MODE_MAP_FIELD_;
}
}
check() {
// assert(_mode != null); --- Traceur v0.0.24 missing assert()
var current;
switch (this._mode) {
case _NOT_NOTIFIED_:
case _MODE_MARKER_:
return false;
case _NOTIFIED_:
current = this.newValue;
this._mode = _NOT_NOTIFIED_;
break;
case _MODE_REFLECT_:
// TODO:
// I'm not sure how much support for Reflection is available in Traceur
// just yet, but I will look into this later...
// current = _instanceMirror.getField(_symbol).reflectee;
if (!this.object) return undefined;
current = this.object[this.field];
break;
case _MODE_GETTER_:
current = this._getter(this.object);
break;
case _MODE_MAP_FIELD_:
if (!this.object) return undefined;
current = this.object[this.field];
break;
case _MODE_IDENTITY_:
current = this.object;
break;
case _MODE_MAP_:
case _MODE_ITERABLE_:
return this.currentValue._check(this.object);
default:
throw "UNREACHABLE";
// assert(false); --- Traceur 0.0.24 missing assert()
}
var last = this.currentValue;
if (last !== current) {
// TODO:
// I'm fairly sure we don't have this issue in JS, with the exception of non-primitive
// Strings. However, I'll look into this.
//
//if (typeof last === "string" && typeof current === "string" && last === current) {
// This is false change in strings we need to recover, and pretend it
// is the same. We save the value so that next time identity will pass
//currentValue = current;
//} else
if (!((typeof last === "number" && last !== last) &&
(typeof current === "number" && current !== current))) {
// Ignore NaN -> NaN changes
this.previousValue = last;
this.currentValue = current;
return true;
}
}
return false;
}
remove() {
// TODO: This is not called when a WatchGroup is destroyed.
// TODO: Should also be called when a parent WatchGroup is destroyed!
this._clearObject();
this._group._recordRemove(this);
}
toString() {
return `${_MODE_NAMES[this._mode]}[${this.field}]`;
}
}
export class MapChangeRecord {
constructor() {
this._records = {}; // WeakMap perhaps?
this.map = {};
this.mapHead = null;
this.changesHead = this.changesTail = null;
this.additionsHead = this.additionsTail = null;
this.removalsHead = this.removalsTail = null;
}
get isDirty() {
return this.additionsHead !== null ||
this.changesHead !== null ||
this.removalsHead !== null;
}
forEachChange(fn) {
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.changesHead;
while (record !== null) {
fn(record);
record = record.nextChangedKeyValue;
}
}
forEachAddition(fn) {
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.additionsHead;
while (record !== null) {
fn(record);
record = record.nextAddedKeyValue;
}
}
forEachRemoval(fn) {
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.removalsHead;
while (record !== null) {
fn(record);
record = record.nextRemovedKeyValue;
}
}
_check(map) {
this._reset();
this.map = map;
var records = this._records;
var oldSeqRecord = this.mapHead;
var lastOldSeqRecord = null, lastNewSeqRecord = null;
var seqChanged = false;
// TODO: Use getOwnPropertyNames instead?
var keys = Object.keys(map);
for (var i = 0, ii = keys.length; i < ii; ++i) {
var key = keys[i], value = map[key], newSeqRecord = null;
if (oldSeqRecord !== null && key === oldSeqRecord.key) {
newSeqRecord = oldSeqRecord;
if (value !== oldSeqRecord.currentValue) {
var prev = oldSeqRecord.previousValue = oldSeqRecord.currentValue;
oldSeqRecord.currentValue = value;
if (!((typeof prev === "number" && prev !== prev) &&
(typeof value === "number" && value !== value))) {
// Ignore NaN -> NaN changes
this._addToChanges(oldSeqRecord);
}
}
} else {
seqChanged = true;
if (oldSeqRecord !== null) {
this._removeFromSeq(lastOldSeqRecord, oldSeqRecord);
this._addToRemovals(oldSeqRecord);
}
if (records.hasOwnProperty(key)) {
newSeqRecord = records[key];
} else {
newSeqRecord = records[key] = new KeyValueRecord(key);
newSeqRecord.currentValue = value;
this._addToAdditions(newSeqRecord);
}
}
if (seqChanged) {
if (this._isInRemovals(newSeqRecord)) {
this._removeFromRemovals(newSeqRecord);
}
if (lastNewSeqRecord === null) {
this.mapHead = newSeqRecord;
} else {
lastNewSeqRecord.nextKeyValue = newSeqRecord;
}
}
lastOldSeqRecord = oldSeqRecord;
lastNewSeqRecord = newSeqRecord;
oldSeqRecord = oldSeqRecord === null ? null : oldSeqRecord.nextKeyValue;
}
this._truncate(lastOldSeqRecord, oldSeqRecord);
return this.isDirty;
}
_reset() {
var record = this.changesHead,
nextRecord;
while (record !== null) {
nextRecord = record.nextChangedKeyValue;
record.previousValue = record.currentValue;
record.nextChangedKeyValue = null;
record = nextRecord;
}
record = this.additionsHead;
while (record !== null) {
nextRecord = record.nextAddedKeyValue;
record.previousValue = record.currentValue;
record.nextAddedKeyValue = null;
record = nextRecord;
}
record = this.removalsHead;
while (record !== null) {
nextRecord = record.nextRemovedKeyValue;
record.nextRemovedKeyValue = null;
record = nextRecord;
}
this.changesHead = this.changesTail = null;
this.additionsHead = this.additionsTail = null;
this.removalsHead = this.removalsTail = null;
}
_truncate(lastRecord, record) {
while (record !== null) {
if (lastRecord === null) {
this.mapHead = null;
} else {
lastRecord.nextKeyValue = null;
}
var nextRecord = record.nextKeyValue;
record.nextKeyValue = null;
this._addToRemovals(record);
lastRecord = record;
record = nextRecord;
}
record = this.removalsHead;
while (record !== null) {
record.previousValue = record.currentValue;
record.currentValue = null;
delete this._records[record.key];
record = record.nextRemovedKeyValue;
}
}
_isInRemovals(record) {
return record === this.removalsHead ||
record.nextRemovedKeyValue !== null ||
record.prevRemovedKeyValue !== null;
}
_addToRemovals(record) {
// TODO: traceur assertions
// assert(record.nextKeyValue === null);
// assert(record.nextAddedKeyValue === null);
// assert(record.nextChangedKeyValue === null);
// assert(record.nextRemovedKeyValue === null);
// assert(record.prevRemovedKeyValue === null);
if (this.removalsHead === null) {
this.removalsHead = this.removalsTail = record;
} else {
this.removalsTail.nextRemovedKeyValue = record;
record.prevRemovedKeyValue = this.removalsTail;
this.removalsTail = record;
}
}
_removeFromSeq(prev, record) {
var next = record.nextKeyValue;
if (prev === null) {
this.mapHead = next;
} else {
prev.nextKeyValue = next;
}
record.nextKeyValue = null;
}
_removeFromRemovals(record) {
// TODO: traceur assertions
// assert(record.nextKeyValue === null)
// assert(record.nextAddedKeyValue === null)
// assert(record.nextChangedKeyValue === null)
var prev = record.prevRemovedKeyValue,
next = record.nextRemovedKeyValue;
if (prev === null) {
this.removalsHead = next;
} else {
prev.nextRemovedKeyValue = next;
}
if (next === null) {
this.removalsTail = prev;
} else {
next.prevRemovedKeyValue = prev;
}
record.prevRemovedKeyValue = record.nextRemovedKeyValue = null;
}
_addToAdditions(record) {
// TODO: traceur assertions
// assert(record.nextKeyValue === null)
// assert(record.nextAddedKeyValue === null)
// assert(record.nextChangedKeyValue === null)
// assert(record.nextRemovedKeyValue === null)
// assert(record.prevRemovedKeyValue === null)
if (this.additionsHead === null) {
this.additionsHead = this.additionsTail = record;
} else {
this.additionsTail.nextAddedKeyValue = record;
this.additionsTail = record;
}
}
_addToChanges(record) {
// TODO: traceur assertions
// assert(record.nextAddedKeyValue === null)
// assert(record.nextChangedKeyValue === null)
// assert(record.nextRemovedKeyValue === null)
// assert(record.prevRemovedKeyValue === null)
if (this.changesHead === null) {
this.changesHead = this.changesTail = record;
} else {
this.changesTail.nextChangedKeyValue = record;
this.changesTail = record;
}
}
}
class KeyValueRecord {
constructor(key) {
this.key = key;
this.previousValue = this.currentValue = null;
this.nextKeyValue = this.nextAddedKeyValue = this.nextChangedKeyValue = null;
this.nextRemovedKeyValue = this.prevRemovedKeyValue = null;
}
toString() {
return this.previousValue === this.currentValue
? this.key
: `${this.key}[${this.previousValue} -> ${this.currentValue}]`;
}
}
export class CollectionChangeRecord {
constructor() {
this._items = new DuplicateMap();
this._removedItems = new DuplicateMap();
this.iterable = null;
this.collectionHead = this.collectionTail = null;
this.additionsHead = this.additionsTail = null;
this.movesHead = this.movesTail = null;
this.removalsHead = this.removalsTail = null;
}
forEachAddition(fn){
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.additionsHead;
while(record !== null) {
fn(record);
record = record.nextAddedRec;
}
}
forEachMove(fn) {
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.movesHead;
while(record !== null) {
fn(record);
record = record.nextMovedRec;
}
}
forEachRemoval(fn){
// TODO: assert(typeof fn === "function" && fn.length === 1)
var record = this.removalsHead;
while(record !== null) {
fn(record);
record = record.nextRemovedRec;
}
}
_check(collection) {
this._reset();
var record = this.collectionHead,
maybeDirty = false,
index,
end,
item;
// TODO: Optimization for frozen arrays / sets / iteratables
// if ((collection is UnmodifiableListView) && identical(_iterable, collection)) {
// Short circuit and assume that the list has not been modified.
// return false;
// }
if (Array.isArray(collection)) {
// TODO:
// Is a separate branch for Array really needed, if the object is known to be
// iterable? In the current implementation, the other branch will never be
// executed, so this shouldn't hurt. But it also causes problems for other
// ES6 iterable types (using generators or custom iterators)
var list = collection;
for (index = 0, end = list.length; index < end; index++) {
item = list[index];
if (record === null || item !== record.item) {
record = this.mismatch(record, item, index);
maybeDirty = true;
} else if (maybeDirty) {
// TODO(misko): can we limit this to duplicates only?
record = this.verifyReinsertion(record, item, index);
}
record = record.nextRec;
}
} else {
index = 0;
for (item in collection) {
if (record === null || item !== record.item) {
record = this.mismatch(record, item, index);
maybeDirty = true;
} else if (maybeDirty) {
// TODO(misko): can we limit this to duplicates only?
record = this.verifyReinsertion(record, item, index);
}
record = record.nextRec;
index++;
}
}
this._truncate(record);
this.iterable = collection;
return this.isDirty;
}
/**
* Reset the state of the change objects to show no changes. This means set
* previousKey to currentKey, and clear all of the queues (additions, moves,
* removals).
*/
_reset() {
var record;
record = this.additionsHead;
while(record !== null) {
record.previousIndex = record.currentIndex;
record = record.nextAddedRec;
}
this.additionsHead = this.additionsTail = null;
record = this.movesHead;
while(record !== null) {
record.previousIndex = record.currentIndex;
var nextRecord = record.nextMovedRec;
// wat.
// assert((record.nextMovedRec = null) == null);
record.nextMovedRec = null;
record = nextRecord;
}
this.movesHead = this.movesTail = null;
this.removalsHead = this.removalsTail = null;
// TODO: Traceur assertions
// assert(isDirty == false);
}
/**
* A [CollectionChangeRecord] is considered dirty if it has additions, moves
* or removals.
*/
get isDirty() {
return this.additionsHead !== null ||
this.movesHead !== null ||
this.removalsHead !== null;
}
/**
* This is the core function which handles differences between collections.
*
* - [record] is the record which we saw at this position last time. If `null`
* then it is a new item.
* - [item] is the current item in the collection
* - [index] is the position of the item in the collection
*/
mismatch(record, item, index) {
// Guard against bogus String changes
if (record !== null) {
//if (item is String && record.item is String && record.item == item) {
// TODO: This probably doesn't matter in ES6, with the exception of non-primitive Strings.
// Figure out a solution for these...
if (typeof item === "string" && typeof record.item === "string" && record.item === item) {
// this is false change in strings we need to recover, and pretend it is
// the same. We save the value so that next time identity can pass
record.item = item;
return record;
}
if ((typeof item === "number" && item !== item) &&
(typeof record.item === "number" && record.item !== record.item)) {
// we need this for JavaScript since in JS NaN !== NaN.
return record;
}
}
// find the previous record so that we know where to insert after.
var prev = record === null ? this.collectionTail : record.prevRec;
// Remove the record from the collection since we know it does not match the
// item.
if (record !== null) {
this._collection_remove(record);
}
// Attempt to see if we have seen the item before.
record = this._items.get(item, index);
if (record !== null) {
// We have seen this before, we need to move it forward in the collection.
this._collection_moveAfter(record, prev, index);
} else {
// Never seen it, check evicted list.
record = this._removedItems.get(item);
if (record !== null) {
// It is an item which we have earlier evict it, reinsert it back into
// the list.
this._collection_reinsertAfter(record, prev, index);
} else {
// It is a new item add it.
record = this._collection_addAfter(new ItemRecord(item), prev, index);
}
}
return record;
}
/**
* This check is only needed if an array contains duplicates. (Short circuit
* of nothing dirty)
*
* Use case: `[a, a]` => `[b, a, a]`
*
* If we did not have this check then the insertion of `b` would:
* 1) evict first `a`
* 2) insert `b` at `0` index.
* 3) leave `a` at index `1` as is. <-- this is wrong!
* 3) reinsert `a` at index 2. <-- this is wrong!
*
* The correct behavior is:
* 1) evict first `a`
* 2) insert `b` at `0` index.
* 3) reinsert `a` at index 1.
* 3) move `a` at from `1` to `2`.
*
*
* Double check that we have not evicted a duplicate item. We need to check if
* the item type may have already been removed:
* The insertion of b will evict the first 'a'. If we don't reinsert it now it
* will be reinserted at the end. Which will show up as the two 'a's switching
* position. This is incorrect, since a better way to think of it is as insert
* of 'b' rather then switch 'a' with 'b' and then add 'a' at the end.
*/
verifyReinsertion(record, item, index) {
var reinsertRecord = this._removedItems.get(item);
if (reinsertRecord !== null) {
record = this._collection_reinsertAfter(reinsertRecord, record.prevRec, index);
} else if (record.currentIndex != index) {
record.currentIndex = index;
this._moves_add(record);
}
return record;
}
/**
* Get rid of any excess [ItemRecord]s from the previous collection
*
* - [record] The first excess [ItemRecord].
*/
_truncate(record) {
// Anything after that needs to be removed;
while(record !== null) {
var nextRecord = record.nextRec;
this._removals_add(this._collection_unlink(record));
record = nextRecord;
}
this._removedItems.clear();
}
_collection_reinsertAfter(record, insertPrev, index) {
this._removedItems.remove(record);
var prev = record.prevRemovedRec;
var next = record.nextRemovedRec;
// TODO: Traceur assertions... also wat.
//assert((record.prevRemovedRec = null) == null);
//assert((record.nextRemovedRec = null) == null);
record.prevRemovedRec = record.nextRemovedRec = null;
if (prev === null) {
this.removalsHead = next;
} else {
prev.nextRemovedRec = next;
}
if (next === null) {
this.removalsTail = prev;
} else {
next.prevRemovedRec = prev;
}
this._collection_insertAfter(record, insertPrev, index);
this._moves_add(record);
return record;
}
_collection_moveAfter(record, prev, index) {
this._collection_unlink(record);
this._collection_insertAfter(record, prev, index);
this._moves_add(record);
return record;
}
_collection_addAfter(record, prev, index) {
this._collection_insertAfter(record, prev, index);
if (this.additionsTail === null) {
// TODO: Traceur assertions
//assert(additionsHead == null);
this.additionsTail = this.additionsHead = record;
} else {
// TODO: Traceur assertions
//assert(additionsTail.nextAddedRec == null);
//assert(record.nextAddedRec == null);
this.additionsTail = this.additionsTail.nextAddedRec = record;
}
return record;
}
_collection_insertAfter(record, prev, index) {
// TODO: Traceur assertions
// assert(record != prev);
// assert(record.nextRec == null);
// assert(record.prevRec == null);
var next = prev === null ? this.collectionHead : prev.nextRec;
// TODO: Traceur assertions
//assert(next != record);
//assert(prev != record);
record.nextRec = next;
record.prevRec = prev;
if (next === null) {
this.collectionTail = record;
} else {
next.prevRec = record;
}
if (prev === null) {
this.collectionHead = record;
} else {
prev.nextRec = record;
}
this._items.put(record);
record.currentIndex = index;
return record;
}
_collection_remove(record) {
this._removals_add(this._collection_unlink(record));
}
_collection_unlink(record) {
this._items.remove(record);
var prev = record.prevRec;
var next = record.nextRec;
// TODO: Traceur assertions. wat.
//assert((record.prevRec = null) == null);
//assert((record.nextRec = null) == null);
record.prevRec = record.nextRec = null;
if (prev === null) {
this.collectionHead = next;
} else {
prev.nextRec = next;
}
if (next === null) {
this.collectionTail = prev;
} else {
next.prevRec = prev;
}
return record;
}
_moves_add(record) {
// TODO: Traceur assertions
//assert(record.nextMovedRec == null);
if (this.movesTail === null) {
// TODO: Traceur assertions
//assert(movesHead == null);
this.movesTail = this.movesHead = record;
} else {
// TODO: Traceur assertions
// assert(movesTail.nextMovedRec == null);
this.movesTail = this.movesTail.nextMovedRec = record;
}
return record;
}
_removals_add(record) {
record.currentIndex = null;
this._removedItems.put(record);
if (this.removalsTail === null) {
// TODO: Traceur assertions
// assert(removalsHead === null);
this.removalsTail = this.removalsHead = record;
} else {
// TODO: Traceur assertions
// assert(removalsTail.nextRemovedRec == null);
// assert(record.nextRemovedRec == null);
record.prevRemovedRec = this.removalsTail;
this.removalsTail = this.removalsTail.nextRemovedRec = record;
}
return record;
}
toString() {
var record;
var list = [];
record = this.collectionHead;
while(record !== null) {
list.push(record);
record = record.nextRec;
}
var additions = [];
record = this.additionsHead;
while(record !== null) {
additions.push(record);
record = record.nextAddedRec;
}
var moves = [];
record = this.movesHead;
while(record !== null) {
moves.push(record);
record = record.nextMovedRec;
}
var removals = [];
record = this.removalsHead;
while(record !== null) {
removals.push(record);
record = record.nextRemovedRec;
}
return "collection: " + list.join(', ') + "\n" +
"additions: " + additions.join(', ') + "\n" +
"moves: " + moves.join(', ') + "\n" +
"removals: " + removals.join(', ') + "\n";
}
}
class ItemRecord {
constructor(item) {
this.item = item;
this.previousIndex = this.currentIndex = null;
this.prevRec = this.nextRec = null;
this.prevDupRec = this.nextDupRec = null;
this.prevRemovedRec = this.nextRemovedRec = null;
this.nextAddedRec = this.nextMovedRec = null;
}
toString() {
return this.previousIndex === this.currentIndex
? `${this.item}`
: `${this.item}[${this.previousIndex} -> ${this.currentIndex}]`;
}
}
class _DuplicateItemRecordList {
constructor() {
this.head = this.tail = null;
}
add(record, beforeRecord) {
// TODO: Traceur assertions
// assert(record.prevDupRec == null);
// assert(record.nextDupRec == null);
// assert(beforeRecord == null ? true : beforeRecord.item == record.item);
if (this.head === null) {
//assert(beforeRecord == null);
this.head = this.tail = record;
} else {
// TODO: Traceur assertions
//assert(record.item === head.item);
if (beforeRecord === null) {
this.tail.nextDupRec = record;
record.prevDupRec = this.tail;
this.tail = record;
} else {
var prev = beforeRecord.prevDupRec;
var next = beforeRecord;
record.prevDupRec = prev;
record.nextDupRec = next;
if (prev === null) {
this.head = record;
} else {
prev.nextDupRec = record;
}
next.prevDupRec = record;
}
}
}
get(key, hideIndex) {
var record = this.head;
if (typeof hideIndex !== "number") {
hideIndex = null;
}
while(record !== null) {
if (hideIndex === null || hideIndex < record.currentIndex && record.item === key) {
return record;
}
record = record.nextDupRec;
}
return record;
}
remove(record) {
// TODO: Add assertion to ensure that the record is within the list.
// Since this is a private API, this may not be necessary, but it should assist in ensuring
// that the routine (and library) behaves correctly.
var prev = record.prevDupRec;
var next = record.nextDupRec;
if (prev === null) {