-
Notifications
You must be signed in to change notification settings - Fork 184
/
set.js
250 lines (215 loc) · 8.75 KB
/
set.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
"use strict";
var Set = require("./_set");
var PropertyChanges = require("./listen/property-changes");
var RangeChanges = require("./listen/range-changes");
var MapChanges = require("./listen/map-changes");
var GlobalSet;
var SIZE = "size";
if( (global.Set !== void 0) && (typeof global.Set.prototype.values === "function")) {
GlobalSet = global.Set;
module.exports = Set
// use different strategies for making sets observable between Internet
// Explorer and other browsers.
var protoIsSupported = {}.__proto__ === Object.prototype,
set_makeObservable;
if (protoIsSupported) {
set_makeObservable = function () {
this.__proto__ = ChangeDispatchSet;
};
} else {
set_makeObservable = function () {
Object.defineProperties(this, observableSetProperties);
};
}
Object.defineProperty(GlobalSet.prototype, "makeObservable", {
value: set_makeObservable,
writable: true,
configurable: true,
enumerable: false
});
var set_clear = GlobalSet.prototype.clear,
set_add = GlobalSet.prototype.add,
set_delete = GlobalSet.prototype.delete;
var observableSetProperties = {
"_dispatchEmptyArray": {
value: []
},
"clear": {
value: function () {
var size = this.size,
clearing;
if (size) {
this.dispatchBeforeOwnPropertyChange(SIZE, size);
}
if (this.dispatchesRangeChanges) {
clearing = this.toArray();
this.dispatchBeforeRangeChange(this._dispatchEmptyArray, clearing, 0);
}
set_clear.call(this);
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(this._dispatchEmptyArray, clearing, 0);
}
if (size) {
this.dispatchOwnPropertyChange(SIZE, 0);
}
},
writable: true,
configurable: true
},
"add": {
value: function (value) {
if (!this.has(value)) {
var index = this.size;
var dispatchValueArray = [value];
this.dispatchBeforeOwnPropertyChange(SIZE, index);
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(dispatchValueArray, this._dispatchEmptyArray, index);
}
set_add.call(this,value);
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(dispatchValueArray, this._dispatchEmptyArray, index);
}
this.dispatchOwnPropertyChange(SIZE, index + 1);
return true;
}
return false;
},
writable: true,
configurable: true
},
"delete": {
value: function (value,index) {
if (this.has(value)) {
var size = this.size;
if(index === undefined) {
var setIterator = this.values();
index = 0
while(setIterator.next().value !== value) {
index++;
}
}
this.dispatchBeforeOwnPropertyChange(SIZE, size);
var dispatchValueArray = [value];
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(this._dispatchEmptyArray, dispatchValueArray, index);
}
set_delete.call(this, value);
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(this._dispatchEmptyArray, dispatchValueArray, index);
}
this.dispatchOwnPropertyChange(SIZE, size - 1);
return true;
}
return false;
}
}
};
var ChangeDispatchSet = Object.create(GlobalSet.prototype, observableSetProperties);
Object.defineEach(Set.prototype, PropertyChanges.prototype, false, /*configurable*/true, /*enumerable*/ false, /*writable*/true);
//This is a no-op test in property-changes.js - PropertyChanges.prototype.makePropertyObservable, so might as well not pay the price every time....
Object.defineProperty(Set.prototype, "makePropertyObservable", {
value: function(){},
writable: true,
configurable: true,
enumerable: false
});
Object.defineEach(Set.prototype, RangeChanges.prototype, false, /*configurable*/true, /*enumerable*/ false, /*writable*/true);
Object.defineEach(Set.prototype, MapChanges.prototype, false, /*configurable*/true, /*enumerable*/ false, /*writable*/true);
//This is really only for testing
Object.defineProperty(Set, "_setupCollectionSet", {
value: setupCollectionSet,
writable: true,
configurable: true,
enumerable: false
});
}
else {
setupCollectionSet();
}
function setupCollectionSet() {
var _CollectionsSet = Set.CollectionsSet;
var CollectionsSet = function CollectionsSet(values, equals, hash, getDefault) {
return _CollectionsSet._init(CollectionsSet, this, values, equals, hash, getDefault);
}
// hack so require("set").Set will work in MontageJS
CollectionsSet.Set = CollectionsSet;
CollectionsSet.from = _CollectionsSet.from;
Set.CollectionsSet = CollectionsSet;
CollectionsSet.prototype = new _CollectionsSet();
CollectionsSet.prototype.constructor = CollectionsSet;
var List = require("./list");
var FastSet = require("./fast-set");
CollectionsSet.prototype.Order = List;
CollectionsSet.prototype.Store = FastSet;
Object.defineProperty(CollectionsSet.prototype,"_dispatchEmptyArray", {
value: []
});
CollectionsSet.prototype.add = function (value) {
var node = new this.order.Node(value);
if (!this.store.has(node)) {
var index = this.length;
var dispatchValueArray = [value];
this.dispatchBeforeOwnPropertyChange(SIZE, index);
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(dispatchValueArray, this._dispatchEmptyArray, index);
}
this.order.add(value);
node = this.order.head.prev;
this.store.add(node);
this.length++;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(dispatchValueArray, this._dispatchEmptyArray, index);
}
this.dispatchOwnPropertyChange(SIZE, index + 1);
return true;
}
return false;
};
CollectionsSet.prototype["delete"] = function (value, equals) {
if (equals) {
throw new Error("Set#delete does not support second argument: equals");
}
var node = new this.order.Node(value);
if (this.store.has(node)) {
node = this.store.get(node);
var dispatchValueArray = [value];
this.dispatchBeforeOwnPropertyChange(SIZE, this.length);
if (this.dispatchesRangeChanges) {
this.dispatchBeforeRangeChange(this._dispatchEmptyArray, dispatchValueArray, node.index);
}
this.store["delete"](node); // removes from the set
this.order.splice(node, 1); // removes the node from the list
this.length--;
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(this._dispatchEmptyArray, dispatchValueArray, node.index);
}
this.dispatchOwnPropertyChange(SIZE, this.length);
return true;
}
return false;
};
CollectionsSet.prototype.clear = function () {
var clearing;
var length = this.length;
if (length) {
this.dispatchBeforeOwnPropertyChange(SIZE, length);
}
if (this.dispatchesRangeChanges) {
clearing = this.toArray();
this.dispatchBeforeRangeChange(this._dispatchEmptyArray, clearing, 0);
}
this._clear();
if (this.dispatchesRangeChanges) {
this.dispatchRangeChange(this._dispatchEmptyArray, clearing, 0);
}
if (length) {
this.dispatchOwnPropertyChange(SIZE, 0);
}
};
Object.addEach(Set.CollectionsSet.prototype, PropertyChanges.prototype);
Object.addEach(Set.CollectionsSet.prototype, RangeChanges.prototype);
Set.CollectionsSet.prototype.makeObservable = function () {
this.order.makeObservable();
};
module.exports = CollectionsSet;
}