forked from jashkenas/backbone
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvirtualcollection.js
More file actions
317 lines (280 loc) · 8.66 KB
/
virtualcollection.js
File metadata and controls
317 lines (280 loc) · 8.66 KB
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
import { createMicrotaskBatcher, createWatchedProxy } from './dist/class-utils.js';
import { Collection } from 'nextbone';
import { isFunction, sortedIndexBy, extend } from 'lodash-es';
/**
* @import { Model, CollectionComparator } from 'nextbone'
*
* @template {Record<string, any>} [Params=Record<string, any>]
* @typedef VirtualCollectionOptions
* @property {ModelFilter} [filter]
* @property {Model | Collection} [destroyWith]
* @property {CollectionComparator<Model>} [comparator]
* @property {new (...args: any[]) => Model | ((...args: any[]) => Model)} [model]
* @property {Params} [params]
*
*
* @callback ModelFilterFunction
* @param {Model} model
* @param {Params} params
* @param {number} index
* @return boolean
*
* @typedef {Record<string, any> | ModelFilterFunction<Params>} ModelFilter
*/
var explicitlyHandledEvents = ['add', 'remove', 'change', 'reset', 'sort'];
function onModelAllEvent(eventName) {
if (explicitlyHandledEvents.indexOf(eventName) === -1) {
this.trigger.apply(this, arguments);
}
}
var buildFilter = function (options) {
if (!options) {
return function () {
return true;
};
} else if (isFunction(options)) {
return options;
} else if (options.constructor === Object) {
return function (model) {
return Object.keys(options).every(function (key) {
return model.get(key) === options[key];
});
};
}
};
/**
* @class VirtualCollection
* @description A virtual collection is a collection that is a filtered view of another collection.
* @template {Model} [TModel=Model]
* @template {Record<string, any>} [Params=Record<string, any>]
* @extends {Collection<TModel>}
*/
class VirtualCollection extends Collection {
/** @type {Collection} */
_parent;
/**
* @param {Collection | null} [parent]
* @param {VirtualCollectionOptions<Params>} [options]
*/
constructor(parent, options = {}) {
super(null, options);
const { destroyWith, filter, params = {} } = options;
this._queueFilterUpdate = createMicrotaskBatcher(() => {
this.updateFilter();
});
/** @type {Params} */
this._params = createWatchedProxy({ ...params }, this._queueFilterUpdate);
if (destroyWith) this.listenTo(destroyWith, 'destroy', this.stopListening);
this._clearChangesCache();
this.accepts = buildFilter(filter || this.acceptModel);
this.parent = parent;
}
/**
* Default filter hook for subclasses.
* @param {TModel} model
* @param {Params} params
* @param {number} index
* @returns {boolean}
*/
acceptModel(model, params, index) {
return true;
}
/**
* @type {Collection}
*/
get parent() {
return this._parent;
}
set parent(value) {
if (value == this._parent) return; // eslint-disable-line eqeqeq
if (this._parent) this.stopListening();
this._parent = value;
if (value) {
this.isLoading = value.isLoading;
if (value.constructor.model) this.model = value.constructor.model;
this._rebuildIndex();
this.listenTo(value, 'add', this._onAdd);
this.listenTo(value, 'remove', this._onRemove);
this.listenTo(value, 'change', this._onChange);
this.listenTo(value, 'reset', this._onReset);
this.listenTo(value, 'filter', this._onFilter);
this.listenTo(value, 'sort', this._onSort);
this.listenTo(value, 'update', this._onUpdate);
this._proxyParentEvents(value, ['sync', 'request', 'load', 'error']);
}
}
/** @returns {Params} */
get params() {
return this._params;
}
/** @param {Params} value */
set params(params) {
if (!params || typeof params !== 'object') {
throw new Error('VirtualCollection: params should be an object');
}
this._params = createWatchedProxy({ ...params }, this._queueFilterUpdate);
this._queueFilterUpdate();
}
/**
* @param {ModelFilter<Params>} [filter]
* @returns {VirtualCollection}
*/
updateFilter(filter) {
if (filter) {
this.accepts = buildFilter(filter);
}
this._rebuildIndex();
this.trigger('filter', this, filter);
this.trigger('reset', this, filter);
return this;
}
_rebuildIndex() {
var params = { ...this._params };
this.models.forEach((model) => this.stopListening(model, 'all'));
this._reset();
this._parent?.each((model, i) => {
if (this.accepts(model, params, i)) {
this.listenTo(model, 'all', onModelAllEvent);
this.models.push(model);
this._byId[model.cid] = model;
if (model.id) this._byId[model.id] = model;
}
});
this.length = this.models.length;
if (this.comparator) this.sort({ silent: true });
}
orderViaParent(options) {
this.models = this._parent.filter((model) => {
return this._byId[model.cid] !== undefined;
});
if (!options.silent) this.trigger('sort', this, options);
}
_onSort(collection, options) {
if (this.comparator !== undefined) return;
this.orderViaParent(options);
}
_proxyParentEvents(collection, events) {
events.forEach((eventName) => {
this.listenTo(collection, eventName, (...args) => {
this.isLoading = collection.isLoading;
this.trigger(eventName, ...args);
});
});
}
_clearChangesCache() {
this._changeCache = {
added: [],
removed: [],
merged: [],
};
}
_onUpdate(collection, options) {
var changes = this._changeCache;
if (changes.added.length || changes.removed.length || changes.merged.length) {
var newOptions = extend({}, options, { changes });
this.trigger('update', this, newOptions);
this._clearChangesCache();
}
}
_onAdd(model, collection, options) {
if (this.get(model) || !this.accepts(model, options.index)) return;
this._changeCache.added.push(model);
this._indexAdd(model);
this.listenTo(model, 'all', onModelAllEvent);
this.trigger('add', model, this, options);
if (this.comparator !== undefined) {
this.trigger('sort', this, options);
}
}
_onRemove(model, collection, options) {
if (!this.get(model)) return;
this._changeCache.removed.push(model);
var index = this._indexRemove(model);
this.stopListening(model, 'all');
this.trigger('remove', model, this, { ...options, index });
}
_onChange(model, options) {
if (!model || !options) return; // ignore malformed arguments coming from custom events
var alreadyHere = this.get(model);
if (this.accepts(model, options.index)) {
if (alreadyHere) {
if (!this._byId[model.id] && model.id) {
this._byId[model.id] = model;
}
this.trigger('change', model, this, options);
} else {
this._onAdd(model, this._parent, options);
}
} else if (alreadyHere) {
var index = this._indexRemove(model);
this.trigger('remove', model, this, { ...options, index });
}
}
_onReset(collection, options) {
this._rebuildIndex();
this.trigger('reset', this, options);
}
_onFilter(collection, options) {
this.trigger('filter', this, options);
}
sortedIndex(model, value, context) {
var iterator = isFunction(value)
? value
: function (target) {
return target.get(value);
};
if (iterator.length === 1) {
return sortedIndexBy(this.models, model, iterator.bind(context));
}
return sortedIndexTwo(this.models, model, iterator, context);
}
_indexAdd(model) {
if (this.get(model)) return;
var i;
// uses a binsearch to find the right index
if (this.comparator) {
i = this.sortedIndex(model, this.comparator, this);
} else if (this.comparator === undefined) {
i = this.sortedIndex(
model,
function (target) {
//TODO: indexOf traverses the array every time the iterator is called
return this._parent.indexOf(target);
},
this,
);
} else {
i = this.length;
}
this.models.splice(i, 0, model);
this._byId[model.cid] = model;
if (model.id) this._byId[model.id] = model;
this.length += 1;
}
_indexRemove(model) {
this.stopListening(model, 'all');
var i = this.indexOf(model);
if (i === -1) return i;
this.models.splice(i, 1);
delete this._byId[model.cid];
if (model.id) delete this._byId[model.id];
this.length -= 1;
return i;
}
clone() {
return new this._parent.constructor(this.models);
}
}
/**
* Equivalent to sortedIndex, but for comparators with two arguments
**/
function sortedIndexTwo(array, obj, iterator, context) {
var low = 0,
high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
iterator.call(context, obj, array[mid]) > 0 ? (low = mid + 1) : (high = mid);
}
return low;
}
export { VirtualCollection, buildFilter };