PSA: nsISimpleEnumerator now works as a JS iterator

Dave Townsend dtownsend at mozilla.com
Thu Aug 23 22:50:13 UTC 2018


This is amazing. Thank you so much for this.

On Thu, Aug 23, 2018 at 3:20 PM Kris Maglione <kmaglione at mozilla.com> wrote:

> As of bug 1484496, any C++-implemented nsISimpleEnumertor instance can be
> used
> as a JS iterator. And, when used this way, the iterators now have
> intrinsic
> type information, and therefore do not require QIing their elements.
>
> Which is to say, now you can simply do:
>
>   for (let window of Services.wm.getEnumerator("navigator:browser")) {
>     ...;
>   }
>
>   for (let docShell of docShellEnum) {
>     ...;
>   }
>
> rather than:
>
>   let winEnum = Services.wm.getEnumerator("navigator:browser");
>   while (winEnum.hasMoreElements()) {
>     let window = winEnum.getNext();
>     ...
>   }
>
>   while (docShellEnum.hasMoreElements()) {
>     let docShell = winEnum.getNext().QueryInterface(Ci.nsIDocShell);
>     ...
>   }
>
> If you happen to be using an nsIArray from directly from JavaScript, you
> unfortunately still need to specify the expected types, since nsIArray has
> no
> idea what types it can contain:
>
>   for (let thing of array.enumerate(Ci.nsIThing)) {
>     ...
>   }
>
> Aside from being easier to maintain, these forms should also be somewhat
> faster than the old protocol, since they only require one XPConnect call
> per
> iteration rather than 3(+).
>
> -Kris
> _______________________________________________
> firefox-dev mailing list
> firefox-dev at mozilla.org
> https://mail.mozilla.org/listinfo/firefox-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/firefox-dev/attachments/20180823/c27e33f0/attachment.html>


More information about the firefox-dev mailing list