-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathobserve_sequence.js
449 lines (402 loc) · 15 KB
/
observe_sequence.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
const isObject = function (value) {
var type = typeof value;
return value != null && (type == 'object' || type == 'function');
}
const has = (obj, path) => {
const thisPath = Array.isArray(path) ? path : [path];
const length = thisPath.length;
for (let i = 0; i < length; i++) {
const key = thisPath[i];
const _has = obj != null && Object.hasOwnProperty.call(obj, key);
if (!_has) return false;
obj = obj[key];
}
return !!length;
};
const warn = function () {
if (ObserveSequence._suppressWarnings) {
ObserveSequence._suppressWarnings--;
} else {
if (typeof console !== 'undefined' && console.warn)
console.warn.apply(console, arguments);
ObserveSequence._loggedWarnings++;
}
};
// isArray returns true for arrays of these types:
// standard arrays: instanceof Array === true, _.isArray(arr) === true
// vm generated arrays: instanceOf Array === false, _.isArray(arr) === true
// subclassed arrays: instanceof Array === true, _.isArray(arr) === false
// see specific tests
function isArray(arr) {
return arr instanceof Array || Array.isArray(arr);
}
// isIterable returns trues for objects implementing iterable protocol,
// except strings, as {{#each 'string'}} doesn't make much sense.
// Requires ES6+ and does not work in IE (but degrades gracefully).
// Does not support the `length` + index protocol also supported by Array.from
function isIterable (object) {
const iter = typeof Symbol != 'undefined' && Symbol.iterator;
return iter
&& object instanceof Object // note: returns false for strings
&& typeof object[iter] == 'function'; // implements iterable protocol
}
const idStringify = MongoID.idStringify;
const idParse = MongoID.idParse;
ObserveSequence = {
_suppressWarnings: 0,
_loggedWarnings: 0,
// A mechanism similar to cursor.observe which receives a reactive
// function returning a sequence type and firing appropriate callbacks
// when the value changes.
//
// @param sequenceFunc {Function} a reactive function returning a
// sequence type. The currently supported sequence types are:
// Array, Cursor, and null.
//
// @param callbacks {Object} similar to a specific subset of
// callbacks passed to `cursor.observe`
// (http://docs.meteor.com/#observe), with minor variations to
// support the fact that not all sequences contain objects with
// _id fields. Specifically:
//
// * addedAt(id, item, atIndex, beforeId)
// * changedAt(id, newItem, oldItem, atIndex)
// * removedAt(id, oldItem, atIndex)
// * movedTo(id, item, fromIndex, toIndex, beforeId)
//
// @returns {Object(stop: Function)} call 'stop' on the return value
// to stop observing this sequence function.
//
// We don't make any assumptions about our ability to compare sequence
// elements (ie, we don't assume EJSON.equals works; maybe there is extra
// state/random methods on the objects) so unlike cursor.observe, we may
// sometimes call changedAt() when nothing actually changed.
// XXX consider if we *can* make the stronger assumption and avoid
// no-op changedAt calls (in some cases?)
//
// XXX currently only supports the callbacks used by our
// implementation of {{#each}}, but this can be expanded.
//
// XXX #each doesn't use the indices (though we'll eventually need
// a way to get them when we support `@index`), but calling
// `cursor.observe` causes the index to be calculated on every
// callback using a linear scan (unless you turn it off by passing
// `_no_indices`). Any way to avoid calculating indices on a pure
// cursor observe like we used to?
observe: function (sequenceFunc, callbacks) {
var lastSeq = null;
var activeObserveHandle = null;
// 'lastSeqArray' contains the previous value of the sequence
// we're observing. It is an array of objects with '_id' and
// 'item' fields. 'item' is the element in the array, or the
// document in the cursor.
//
// '_id' is whichever of the following is relevant, unless it has
// already appeared -- in which case it's randomly generated.
//
// * if 'item' is an object:
// * an '_id' field, if present
// * otherwise, the index in the array
//
// * if 'item' is a number or string, use that value
//
// XXX this can be generalized by allowing {{#each}} to accept a
// general 'key' argument which could be a function, a dotted
// field name, or the special @index value.
var lastSeqArray = []; // elements are objects of form {_id, item}
var computation = Tracker.autorun(function () {
var seq = sequenceFunc();
Tracker.nonreactive(function () {
var seqArray; // same structure as `lastSeqArray` above.
if (activeObserveHandle) {
// If we were previously observing a cursor, replace lastSeqArray with
// more up-to-date information. Then stop the old observe.
lastSeqArray = lastSeq.fetch().map(function (doc) {
return {_id: doc._id, item: doc};
});
activeObserveHandle.stop();
activeObserveHandle = null;
}
if (!seq) {
seqArray = seqChangedToEmpty(lastSeqArray, callbacks);
} else if (isArray(seq)) {
seqArray = seqChangedToArray(lastSeqArray, seq, callbacks);
} else if (isStoreCursor(seq)) {
var result /* [seqArray, activeObserveHandle] */ =
seqChangedToCursor(lastSeqArray, seq, callbacks);
seqArray = result[0];
activeObserveHandle = result[1];
} else if (isIterable(seq)) {
const array = Array.from(seq);
seqArray = seqChangedToArray(lastSeqArray, array, callbacks);
} else {
throw badSequenceError(seq);
}
diffArray(lastSeqArray, seqArray, callbacks);
lastSeq = seq;
lastSeqArray = seqArray;
});
});
return {
stop: function () {
computation.stop();
if (activeObserveHandle)
activeObserveHandle.stop();
}
};
},
// Fetch the items of `seq` into an array, where `seq` is of one of the
// sequence types accepted by `observe`. If `seq` is a cursor, a
// dependency is established.
fetch: function (seq) {
if (!seq) {
return [];
} else if (isArray(seq)) {
return seq;
} else if (isStoreCursor(seq)) {
return seq.fetch();
} else if (isIterable(seq)) {
return Array.from(seq);
} else {
throw badSequenceError(seq);
}
}
};
function ellipsis(longStr, maxLength) {
if(!maxLength) maxLength = 100;
if(longStr.length < maxLength) return longStr;
return longStr.substr(0, maxLength-1) + '…';
}
function arrayToDebugStr(value, maxLength) {
var out = '', sep = '';
for(var i = 0; i < value.length; i++) {
var item = value[i];
out += sep + toDebugStr(item, maxLength);
if(out.length > maxLength) return out;
sep = ', ';
}
return out;
}
function toDebugStr(value, maxLength) {
if(!maxLength) maxLength = 150;
const type = typeof value;
switch(type) {
case 'undefined':
return type;
case 'number':
return value.toString();
case 'string':
return JSON.stringify(value); // add quotes
case 'object':
if(value === null) {
return 'null';
} else if(Array.isArray(value)) {
return 'Array [' + arrayToDebugStr(value, maxLength) + ']';
} else if(Symbol.iterator in value) { // Map and Set are not handled by JSON.stringify
return value.constructor.name
+ ' [' + arrayToDebugStr(Array.from(value), maxLength)
+ ']'; // Array.from doesn't work in IE, but neither do iterators so it's unreachable
} else { // use JSON.stringify (sometimes toString can be better but we don't know)
return value.constructor.name + ' '
+ ellipsis(JSON.stringify(value), maxLength);
}
default:
return type + ': ' + value.toString();
}
}
function sequenceGotValue(sequence) {
try {
return ' Got ' + toDebugStr(sequence);
} catch(e) {
return ''
}
}
const badSequenceError = function (sequence) {
return new Error("{{#each}} currently only accepts " +
"arrays, cursors, iterables or falsey values." +
sequenceGotValue(sequence));
};
const isFunction = (func) => {
return typeof func === "function";
}
const isStoreCursor = function (cursor) {
return cursor && isObject(cursor) &&
isFunction(cursor.observe) && isFunction(cursor.fetch);
};
// Calculates the differences between `lastSeqArray` and
// `seqArray` and calls appropriate functions from `callbacks`.
// Reuses Minimongo's diff algorithm implementation.
const diffArray = function (lastSeqArray, seqArray, callbacks) {
var diffFn = Package['diff-sequence'].DiffSequence.diffQueryOrderedChanges;
var oldIdObjects = [];
var newIdObjects = [];
var posOld = {}; // maps from idStringify'd ids
var posNew = {}; // ditto
var posCur = {};
var lengthCur = lastSeqArray.length;
seqArray.forEach(function (doc, i) {
newIdObjects.push({_id: doc._id});
posNew[idStringify(doc._id)] = i;
});
lastSeqArray.forEach(function (doc, i) {
oldIdObjects.push({_id: doc._id});
posOld[idStringify(doc._id)] = i;
posCur[idStringify(doc._id)] = i;
});
// Arrays can contain arbitrary objects. We don't diff the
// objects. Instead we always fire 'changedAt' callback on every
// object. The consumer of `observe-sequence` should deal with
// it appropriately.
diffFn(oldIdObjects, newIdObjects, {
addedBefore: function (id, doc, before) {
var position = before ? posCur[idStringify(before)] : lengthCur;
if (before) {
// If not adding at the end, we need to update indexes.
// XXX this can still be improved greatly!
Object.entries(posCur).forEach(function ([id, pos]) {
if (pos >= position)
posCur[id]++;
});
}
lengthCur++;
posCur[idStringify(id)] = position;
callbacks.addedAt(
id,
seqArray[posNew[idStringify(id)]].item,
position,
before);
},
movedBefore: function (id, before) {
if (id === before)
return;
var oldPosition = posCur[idStringify(id)];
var newPosition = before ? posCur[idStringify(before)] : lengthCur;
// Moving the item forward. The new element is losing one position as it
// was removed from the old position before being inserted at the new
// position.
// Ex.: 0 *1* 2 3 4
// 0 2 3 *1* 4
// The original issued callback is "1" before "4".
// The position of "1" is 1, the position of "4" is 4.
// The generated move is (1) -> (3)
if (newPosition > oldPosition) {
newPosition--;
}
// Fix up the positions of elements between the old and the new positions
// of the moved element.
//
// There are two cases:
// 1. The element is moved forward. Then all the positions in between
// are moved back.
// 2. The element is moved back. Then the positions in between *and* the
// element that is currently standing on the moved element's future
// position are moved forward.
Object.entries(posCur).forEach(function ([id, elCurPosition]) {
if (oldPosition < elCurPosition && elCurPosition < newPosition)
posCur[id]--;
else if (newPosition <= elCurPosition && elCurPosition < oldPosition)
posCur[id]++;
});
// Finally, update the position of the moved element.
posCur[idStringify(id)] = newPosition;
callbacks.movedTo(
id,
seqArray[posNew[idStringify(id)]].item,
oldPosition,
newPosition,
before);
},
removed: function (id) {
var prevPosition = posCur[idStringify(id)];
Object.entries(posCur).forEach(function ([id, pos]) {
if (pos >= prevPosition)
posCur[id]--;
});
delete posCur[idStringify(id)];
lengthCur--;
callbacks.removedAt(
id,
lastSeqArray[posOld[idStringify(id)]].item,
prevPosition);
}
});
Object.entries(posNew).forEach(function ([idString, pos]) {
var id = idParse(idString);
if (has(posOld, idString)) {
// specifically for primitive types, compare equality before
// firing the 'changedAt' callback. otherwise, always fire it
// because doing a deep EJSON comparison is not guaranteed to
// work (an array can contain arbitrary objects, and 'transform'
// can be used on cursors). also, deep diffing is not
// necessarily the most efficient (if only a specific subfield
// of the object is later accessed).
var newItem = seqArray[pos].item;
var oldItem = lastSeqArray[posOld[idString]].item;
if (typeof newItem === 'object' || newItem !== oldItem)
callbacks.changedAt(id, newItem, oldItem, pos);
}
});
};
seqChangedToEmpty = function (lastSeqArray, callbacks) {
return [];
};
seqChangedToArray = function (lastSeqArray, array, callbacks) {
var idsUsed = {};
var seqArray = array.map(function (item, index) {
var id;
if (typeof item === 'string') {
// ensure not empty, since other layers (eg DomRange) assume this as well
id = "-" + item;
} else if (typeof item === 'number' ||
typeof item === 'boolean' ||
item === undefined ||
item === null) {
id = item;
} else if (typeof item === 'object') {
id = (item && ('_id' in item)) ? item._id : index;
} else {
throw new Error("{{#each}} doesn't support arrays with " +
"elements of type " + typeof item);
}
var idString = idStringify(id);
if (idsUsed[idString]) {
if (item && typeof item === 'object' && '_id' in item)
warn("duplicate id " + id + " in", array);
id = Random.id();
} else {
idsUsed[idString] = true;
}
return { _id: id, item: item };
});
return seqArray;
};
seqChangedToCursor = function (lastSeqArray, cursor, callbacks) {
var initial = true; // are we observing initial data from cursor?
var seqArray = [];
var observeHandle = cursor.observe({
addedAt: function (document, atIndex, before) {
if (initial) {
// keep track of initial data so that we can diff once
// we exit `observe`.
if (before !== null)
throw new Error("Expected initial data from observe in order");
seqArray.push({ _id: document._id, item: document });
} else {
callbacks.addedAt(document._id, document, atIndex, before);
}
},
changedAt: function (newDocument, oldDocument, atIndex) {
callbacks.changedAt(newDocument._id, newDocument, oldDocument,
atIndex);
},
removedAt: function (oldDocument, atIndex) {
callbacks.removedAt(oldDocument._id, oldDocument, atIndex);
},
movedTo: function (document, fromIndex, toIndex, before) {
callbacks.movedTo(
document._id, document, fromIndex, toIndex, before);
}
});
initial = false;
return [seqArray, observeHandle];
};