W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2015

Re: Exposing structured clone as an API?

From: Maciej Stachowiak <mjs@apple.com>
Date: Thu, 23 Apr 2015 15:38:13 -0700
Cc: Ted Mielczarek <ted@mozilla.com>, public-webapps <public-webapps@w3.org>, kg@luminance.org
Message-id: <5A40FD0A-74C5-44F6-B5CC-177A62508E92@apple.com>
To: Martin Thomson <martin.thomson@gmail.com>

> On Apr 23, 2015, at 3:27 PM, Martin Thomson <martin.thomson@gmail.com> wrote:
> 
> On 23 April 2015 at 15:02, Ted Mielczarek <ted@mozilla.com> wrote:
>> Has anyone ever proposed exposing the structured clone algorithm directly as an API?
> 
> If you didn't just do so, I will :)
> 
>> 1. https://twitter.com/TedMielczarek/status/591315580277391360
> 
> Looking at your jsfiddle, here's a way to turn that into something useful.
> 
> +Object.prototype.clone = Object.prototype.clone || function() {
> - function clone(x) {
>    return new Promise(function (resolve, reject) {
>        window.addEventListener('message', function(e) {
>            resolve(e.data);
>        });
> +        window.postMessage(this, "*");
> -        window.postMessage(x, "*");
>    });
> }
> 
> But are we are in the wrong place to have that discussion?

Code nitpick: it probably should remove the event listener from within the handler, or calling this function repeatedly will leak memory. Also it will get slower every time.

Actually, now that I think about it, this isn’t usable at all if you are using postMessage for anything else, since you could accidentally capture non-cloning-related messages.

I guess these are potentially arguments to expose cloning directly.

Regards,
Maciej
Received on Thursday, 23 April 2015 22:38:42 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:27:31 UTC