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<DOMMozPromiseRequestHolder<GenericPromise>> holder =
      new DOMMozPromiseRequestHolder<GenericPromise>(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

Reply via email to