PSA: new helper class for MozPromise in DOM code

2018-04-26 Thread Ben Kelly
Hi all,

I pushed a new helper class to inbound today to make it easier to work with
MozPromise in DOM code.  Specifically, it allows you to auto-disconnect a
Thenable request when the global dies.

The class is called DOMMozPromiseRequestHolder.  Here is an example using
it:

#include "mozilla/dom/DOMMozPromiseRequestHolder.h"

// some method...

nsIGlobalObject* aGlobal = GetParentObject();
NS_ENSURE_TRUE(aGlobal, NS_ERROR_DOM_INVALID_STATE_ERR);

RefPtr> holder =
  new DOMMozPromiseRequestHolder(global);

SomeAsyncWork()->Then(
  global->EventTargetFor(TaskCategory::Other), __func__,
  [holder] {
holder->Complete();
// do resolve stuff
  }, [holder] {
holder->Compelte();
// do rejection stuff
  })->Track(*holder);

This code supports multiple overlapping async operations at once.  It also
works on both window and worker globals.

Disconnecting on workers is particularly important since the worker
shutdown may prevent the promise from dispatching a runnable.  This can in
turn trigger assertions in the Thenable's destructor.  The helper class
correctly disconnects the Thenable during worker shutdown without any
additional runnable dispatch.

This is landing in:

https://bugzilla.mozilla.org/show_bug.cgi?id=1457187

Please let me know if you run into any problems with it.

Thanks.

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


Re: PSA: new helper class for MozPromise in DOM code

2018-04-26 Thread Kris Maglione

On Thu, Apr 26, 2018 at 04:12:09PM -0400, Ben Kelly wrote:

I pushed a new helper class to inbound today to make it easier to work with
MozPromise in DOM code.  Specifically, it allows you to auto-disconnect a
Thenable request when the global dies.


Thank you! I've been worried about these kinds of footguns for a 
while. We could probably use something similar for JS...



   RefPtr> holder =
 new DOMMozPromiseRequestHolder(global);


Note, this can be written a bit more concisely as:

   auto holder = MakeRefPtr>(global);

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

All the hundreds of millions of people who, in their time, believed
the Earth was flat never succeeded in unrounding it by an inch.
--Isaac Asimov

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


Re: PSA: new helper class for MozPromise in DOM code

2018-04-27 Thread Jean-Yves Avenard
Hi

> On 26 Apr 2018, at 10:12 pm, Ben Kelly  wrote:
> 
> Hi all,
> 
> I pushed a new helper class to inbound today to make it easier to work with
> MozPromise in DOM code.  Specifically, it allows you to auto-disconnect a
> Thenable request when the global dies.
> 
> The class is called DOMMozPromiseRequestHolder.  Here is an example using
> it:

is that just a refcounted version of MozPromiseRequestHolder?




smime.p7s
Description: S/MIME cryptographic signature
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Re: PSA: new helper class for MozPromise in DOM code

2018-04-27 Thread Ben Kelly
On Fri, Apr 27, 2018, 3:13 AM Jean-Yves Avenard 
wrote:

> > The class is called DOMMozPromiseRequestHolder.  Here is an example using
> > it:
>
> is that just a refcounted version of MozPromiseRequestHolder?
>

No.  It binds to the global and calls DisconnectIfExists() when the global
invokes DisconnectFromOwner() on it.
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform