Re: PSA: nsISimpleEnumerator now works as a JS iterator

2018-08-23 Thread Mike Conley
Wow, this is a great improvement! Thank you!

On Thu, Aug 23, 2018, 6:26 PM Kris Maglione  wrote:

> Oh, and from C++, there's also now a range iterator wrapper with
> similar semantics:
>
>   for (auto& docShell : SimpleEnumerator(docShellEnum)) {
> ...
>   }
>
> On Thu, Aug 23, 2018 at 03:19:55PM -0700, Kris Maglione 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
>
> --
> Kris Maglione
> Senior Firefox Add-ons Engineer
> Mozilla Corporation
>
> There is no greater mistake than the hasty conclusion that opinions
> are worthless because they are badly argued.
> --Thomas Huxley
>
> ___
> dev-platform mailing list
> dev-platform@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-platform
>
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Re: PSA: nsISimpleEnumerator now works as a JS iterator

2018-08-23 Thread Kris Maglione
Oh, and from C++, there's also now a range iterator wrapper with 
similar semantics:


 for (auto& docShell : SimpleEnumerator(docShellEnum)) {
   ...
 }

On Thu, Aug 23, 2018 at 03:19:55PM -0700, Kris Maglione 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


--
Kris Maglione
Senior Firefox Add-ons Engineer
Mozilla Corporation

There is no greater mistake than the hasty conclusion that opinions
are worthless because they are badly argued.
--Thomas Huxley

___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform