-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathcollection-observation.js
138 lines (116 loc) · 3.82 KB
/
collection-observation.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
import {calcSplices, projectArraySplices} from './array-change-records';
import {getChangeRecords} from './map-change-records';
import {subscriberCollection} from './subscriber-collection';
@subscriberCollection()
export class ModifyCollectionObserver {
constructor(taskQueue, collection) {
this.taskQueue = taskQueue;
this.queued = false;
this.changeRecords = null;
this.oldCollection = null;
this.collection = collection;
this.lengthPropertyName = collection instanceof Map || collection instanceof Set ? 'size' : 'length';
}
subscribe(context, callable) {
this.addSubscriber(context, callable);
}
unsubscribe(context, callable) {
this.removeSubscriber(context, callable);
}
addChangeRecord(changeRecord) {
if (!this.hasSubscribers() && !this.lengthObserver) {
return;
}
if (changeRecord.type === 'splice') {
let index = changeRecord.index;
let arrayLength = changeRecord.object.length;
if (index > arrayLength) {
index = arrayLength - changeRecord.addedCount;
} else if (index < 0) {
index = arrayLength + changeRecord.removed.length + index - changeRecord.addedCount;
}
if (index < 0) {
index = 0;
}
changeRecord.index = index;
}
if (this.changeRecords === null) {
this.changeRecords = [changeRecord];
} else {
this.changeRecords.push(changeRecord);
}
if (!this.queued) {
this.queued = true;
this.taskQueue.queueMicroTask(this);
}
}
flushChangeRecords() {
if ((this.changeRecords && this.changeRecords.length) || this.oldCollection) {
this.call();
}
}
reset(oldCollection) {
this.oldCollection = oldCollection;
if (this.hasSubscribers() && !this.queued) {
this.queued = true;
this.taskQueue.queueMicroTask(this);
}
}
getLengthObserver() {
return this.lengthObserver || (this.lengthObserver = new CollectionLengthObserver(this.collection));
}
call() {
let changeRecords = this.changeRecords;
let oldCollection = this.oldCollection;
let records;
this.queued = false;
this.changeRecords = [];
this.oldCollection = null;
if (this.hasSubscribers()) {
if (oldCollection) {
// TODO (martingust) we might want to refactor this to a common, independent of collection type, way of getting the records
if (this.collection instanceof Map || this.collection instanceof Set) {
records = getChangeRecords(oldCollection);
} else {
//we might need to combine this with existing change records....
records = calcSplices(this.collection, 0, this.collection.length, oldCollection, 0, oldCollection.length);
}
} else {
if (this.collection instanceof Map || this.collection instanceof Set) {
records = changeRecords;
} else {
records = projectArraySplices(this.collection, changeRecords);
}
}
this.callSubscribers(records);
}
if (this.lengthObserver) {
this.lengthObserver.call(this.collection[this.lengthPropertyName]);
}
}
}
@subscriberCollection()
export class CollectionLengthObserver {
constructor(collection) {
this.collection = collection;
this.lengthPropertyName = collection instanceof Map || collection instanceof Set ? 'size' : 'length';
this.currentValue = collection[this.lengthPropertyName];
}
getValue() {
return this.collection[this.lengthPropertyName];
}
setValue(newValue) {
this.collection[this.lengthPropertyName] = newValue;
}
subscribe(context, callable) {
this.addSubscriber(context, callable);
}
unsubscribe(context, callable) {
this.removeSubscriber(context, callable);
}
call(newValue) {
let oldValue = this.currentValue;
this.callSubscribers(newValue, oldValue);
this.currentValue = newValue;
}
}