Make Minimongo cursor iterable #8888
Conversation
@@ -180,6 +180,42 @@ LocalCollection.Cursor.prototype.forEach = function (callback, thisArg) { | |||
}); | |||
}; | |||
|
|||
LocalCollection.Cursor.prototype[Symbol.iterator] = function () { | |||
var self = this; |
zimme
Jul 9, 2017
•
Contributor
As ecmascript is available in this package and this is a completely new function maybe it's ok that we use new syntax here instead? const
/let
, arrow functions, etc.
As ecmascript is available in this package and this is a completely new function maybe it's ok that we use new syntax here instead? const
/let
, arrow functions, etc.
mitar
Jul 9, 2017
Author
Collaborator
I just wanted to keep it similar to the rest of the code. Especially because code is very similar to forEach
. I do not really care though. :-)
I just wanted to keep it similar to the rest of the code. Especially because code is very similar to forEach
. I do not really care though. :-)
radekmie
Jul 9, 2017
Contributor
As I said in already mentioned issue, I'm planning to submit a PR with making minimongo
more ES5/6.
As I said in already mentioned issue, I'm planning to submit a PR with making minimongo
more ES5/6.
|
||
return { | ||
value: elt | ||
}; |
radekmie
Jul 9, 2017
Contributor
To be more compliant with the standard, done
should be set to false
here.
To be more compliant with the standard, done
should be set to false
here.
mitar
Jul 9, 2017
Author
Collaborator
Not necessary:
Has the value false if the iterator was able to produce the next value in the sequence. This is equivalent of not specifying the done property altogether.
Not necessary:
Has the value false if the iterator was able to produce the next value in the sequence. This is equivalent of not specifying the done property altogether.
radekmie
Jul 9, 2017
Contributor
OK, ECMA-262 6th Edition, Section 25.1.1.3:
done
This is the result status of an iterator next
method call. If the end of the iterator was reached done
is true
. If the end was not reached done
is false
and a value is available. If a done
property (either own or inherited) does not exist, it is consider to have the value false
.
OK, ECMA-262 6th Edition, Section 25.1.1.3:
done
This is the result status of an iteratornext
method call. If the end of the iterator was reacheddone
istrue
. If the end was not reacheddone
isfalse
and a value is available. If adone
property (either own or inherited) does not exist, it is consider to have the valuefalse
.
@@ -180,6 +180,42 @@ LocalCollection.Cursor.prototype.forEach = function (callback, thisArg) { | |||
}); | |||
}; | |||
|
|||
LocalCollection.Cursor.prototype[Symbol.iterator] = function () { | |||
var self = this; |
radekmie
Jul 9, 2017
Contributor
As I said in already mentioned issue, I'm planning to submit a PR with making minimongo
more ES5/6.
As I said in already mentioned issue, I'm planning to submit a PR with making minimongo
more ES5/6.
I would prefer the more optimized version, but I think this would be nice until we can make that happen. |
I'm already preparing an issue with a plan for modernization... |
Yes, I think it is useful to expose the API now. With old JS code. And then we decide on how to change internals to use ES6 and that can then be a separate PR changing everything. |
I think this is a good idea! |
@mitar - given the |
I made it conditional. But one thing to consider maybe instead is that we could just poly-fill Anyway, I think this is it then for this iteration of code. So I would then say this is ready. Probably we should add tests and documentation and history entry? Anything else? |
Actually, it looks like we already have Maybe we don't need the conditional? I guess it doesn't hurt to have it though - thanks again! |
OK, so just add tests and documentation then? |
Yes tests / documentation would be great @mitar - thanks! |
Hi @mitar - any luck with tests / docs? Thanks! |
What about using this approach: LocalCollection.Cursor.prototype[Symbol.iterator] = function* () {
yield* this.fetch();
}; |
@mitar no, I did not. You can update it - only few minor changes are needed. If you want, I can do it. |
@daniel-mf, does that work? I am not familiar with this syntax and how it would work. |
I am currently a bit overwhelmed with other work, so if you could do it, that would be great. I added you as a collaborator to my repo. |
@mitar: don't you mind if I use EDIT: Maybe in the future - I'll leave it for now. |
But it is sad that we have to generate whole array just to then iterate over it. This should really be improved. |
@mitar I've tested, it works. |
@mitar: I thought about |
Since we can't make it really iterable, I guess there's no point in writing a |
I won't be able to get to it before next week. @mitar? |
@hwillson: I looked at this but I am not sure how to document this? It is not a method. So how does one document this with current documentation syntax? |
I added tests and history entry. |
Thanks for these changes @mitar. Maybe adding a small mention of cursors now being iterable would make sense in the following docs locations:
If you don't have time to get a https://github.com/meteor/docs PR ready for these changes let me know, and I'll make it happen. As far as things go with this PR however, I think we're all set. Thanks all - LGTM! |
I am slightly under time pressure at the moment, so if you could update the docs, that would be great. Sorry for putting more on your plate. :-( Otherwise I will try, but not sure when. (You do not think there is a place in autogenerated code to document iterator?) |
No problem at all, I'll take care of it. No, I didn't see a good place to mention it in the jsdoc's (but if anyone reading this can think of another good place, we'll add it there as well). |
0ebca07
into
meteor:devel
This pull requests makes Minimongo cursors iterable using the iteration protocol.
This means that somebody can do
[...cursor]
to get all values in the cursor (instead ofcursor.fetch()
). Or used insidefor..of
loop.Not sure what do we want to do about compatibility. Maybe we can define it only if
Symbol.iterator
exists and is available. Or, if we are good with having it always, we could then changeforEach
to use it internally (instead of having code duplicated a bit). And thenfetch
could really just be[...cursor]
and maybe that will be even faster then our current version.BTW, this currently is not very optimized because it creates internally the whole array and then iterates over. It would be great to replace
_getRawObjects
with something which is returning object by object by itself. And then we would be creating arrays of all objects only when really requested (infetch
, and not inmap
orforEach
).