W3C home > Mailing lists > Public > whatwg@whatwg.org > October 2013

Re: [whatwg] onclose events for MessagePort

From: Jonas Sicking <jonas@sicking.cc>
Date: Tue, 22 Oct 2013 10:32:21 -0700
Message-ID: <CA+c2ei-4dnKZ2g0UTfSKiSSDtR2YgM8D_CGjUCb9Kw=QWCZ2=g@mail.gmail.com>
To: Ehsan Akhgari <ehsan@mozilla.com>
Cc: WHAT Working Group <whatwg@lists.whatwg.org>, Boris Zbarsky <bzbarsky@mit.edu>, Ian Hickson <ian@hixie.ch>, Andrew Wilson <atwilson@google.com>, Gene Lian <clian@mozilla.com>
On Tue, Oct 22, 2013 at 9:31 AM, Ehsan Akhgari <ehsan@mozilla.com> wrote:
>> interface MessagePort {
>>   ...
>>   Promise pin();
>>   void unpin(optional any value);
>> };
>>
>> Rather than firing channeldropped we reject any promise returned from
>> pin(). Once the caller receives an expected answer he/she calls unpin()
>> which resolves the promise using whatever value is passed in and so the port
>> becomes GCable again.
>>
>> When pin() is called again after the unpin call we create a new promise
>> which again prevents the port from getting GCed.
>
> This sounds good to me, if the usage of Promise is OK in HTML these days!
>>
>> We could even expose a failAndUnpin function which rejects the promise.
>> This could be useful to enable the page to implement timeouts etc.
>
> Hmm, I'm not sure if I understand this.  Can you please elaborate?

As the API stands in the proposal above you could write code like:

port.postMessage({ doStuff: "using-this-data" });
port.onmessage = e => { port.unpin(e.data); };
port.pin().then(d => doAsync(d)).then(...);

Which is great. However if you want to implement a timeout such that
it is treated as an error if data isn't returned within 5 seconds,
that is harder to do. How to do it is left as an exercise to the
reader :)

However it definitely could be done. So I think the above API is
certainly good enough. The failAndUnpin (or better named
unpinAndReject) method is just a convenience method.

/ Jonas
Received on Tuesday, 22 October 2013 17:33:18 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:12 UTC