Re: [whatwg] onclose events for MessagePort

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