mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Merge pull request #8888 from mitar/minimongo-iterator
Make Minimongo cursor iterable.
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
## v.NEXT
|
||||
|
||||
* Minimongo cursors are now JavaScript iterable objects and can now be iterated over
|
||||
using `for...of` loops, spread operator, `yield*`, and destructuring assignments.
|
||||
[PR #8888](https://github.com/meteor/meteor/pull/8888)
|
||||
|
||||
* `meteor list --tree` can now be used to list all transitive package
|
||||
dependencies (and versions) in an application. Weakly referenced dependencies
|
||||
can also be listed by using the `--weak` option. For more information, run
|
||||
|
||||
@@ -72,6 +72,35 @@ export default class Cursor {
|
||||
return result;
|
||||
}
|
||||
|
||||
[Symbol.iterator]() {
|
||||
if (this.reactive) {
|
||||
this._depend({
|
||||
addedBefore: true,
|
||||
removed: true,
|
||||
changed: true,
|
||||
movedBefore: true});
|
||||
}
|
||||
|
||||
let index = 0;
|
||||
const objects = this._getRawObjects({ordered: true});
|
||||
|
||||
return {
|
||||
next: () => {
|
||||
if (index < objects.length) {
|
||||
// This doubles as a clone operation.
|
||||
let element = this._projectionFn(objects[index++]);
|
||||
|
||||
if (this._transform)
|
||||
element = this._transform(element);
|
||||
|
||||
return {value: element};
|
||||
}
|
||||
|
||||
return {done: true};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @callback IterationCallback
|
||||
* @param {Object} doc
|
||||
|
||||
@@ -220,6 +220,17 @@ Tinytest.add('minimongo - cursors', test => {
|
||||
// call it again, it still works
|
||||
test.length(q.fetch(), 20);
|
||||
|
||||
// iterator
|
||||
count = 0;
|
||||
for (let obj of q) {
|
||||
test.equal(obj.i, count++);
|
||||
};
|
||||
test.equal(count, 20);
|
||||
// call it again, it still works
|
||||
test.length(q.fetch(), 20);
|
||||
// test spread operator
|
||||
test.equal([...q], q.fetch());
|
||||
|
||||
// map
|
||||
res = q.map(function(obj, i, cursor) {
|
||||
test.equal(obj.i, i);
|
||||
|
||||
Reference in New Issue
Block a user