-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose structuredClone #3414
Expose structuredClone #3414
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some additional nits from me. Thanks for taking this on. If you can convince the powers that be at Chrome (and add some tests) we can move forward with this. (Potentially after adding transferables.)
Other than the comment, the spec text LGTM. However, this will need tests (and of course, browser support).
WPT PR here: web-platform-tests/wpt#9218 |
I think this section is misplaced. It should be part of https://html.spec.whatwg.org/multipage/structured-data.html#safe-passing-of-structured-data, probably a new sub-section at the end. For the dev edition, the contents of this new subsection (not including its heading) should be the only content of the "Safe passing of structured data" section. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting the ball rolling on this, Surma! 👍🏻
@domenic I hope I understood your feedback right. PTAL |
As a web developer / JS API user I don't care so much about how the object gets cloned (whether the "structural clone algorithm" is used or a different way of deep cloning). The name of the method doesn't have to reflect the choice of algorithm used by the implementation. Could the method be called clone() instead of structuredClone() ? or eg deepClone() or completeClone() or completeCopy() - |
I disagree. The name should to convey what kind of clone the result is. The method A name like |
I see, thanks for your reply. Perhaps we do need a method deepClone() or completeCopy()? |
Yeah, we have talked about that. But cloning functions is not trivial. Please continue discussing that matter in #793 :) |
To reiterate, my main problem with this proposal is that it doesn't expose the full underlying primitive, but rather a subset, and it's unclear how to extend it to expose it fully. The primitive we have is one that takes a value and a transfer list, and returns a value and a transfer list. And the transfer list can be independent from the value. This API just drops the return transfer list on the floor. |
Thanks for re-iterating, I didn’t get that before. That’s fair, I guess. I don’t see a problem with changing the returned value to |
I'm not sure that primitive is terribly important. I think it's more for handling cases where for whatever reason you don't put the transferred values inside the cloned data, and for spec convenience in not making you dig through to find those values, but IMO such cases are not an important part of the primitive... |
I agree, but I’m also not sure if we are thinking of everything that could be needed in the future. I’ll leave that decision up to you all, I don’t have very strong feelings either way, although in terms of developer ergonomics and arguably the current 99% use-case, returning just the clone seems more desirable. |
FWIW, +1 for just returning the deserialized values. It'd be really easy for a caller to get if they wanted, be serializing |
The original PR for tests got side-tracked and the actual PR for adding tests for |
Just wondering: I am not sure what the result of the conversation so far is. Have we reached some sort of consensus weather |
I don't know about consensus, but I feel it should definitely be there, and my read of @annevk was that he didn't feel too strongly against it... What we're missing in terms of implementer interest is outlined in https://whatwg.org/working-mode#additions. So far I don't see any browser engineers saying "we would like to implement this soon" on this thread. |
I suspect Firefox would adopt this pretty quickly, but it'd be good if someone could go over #793 (comment) and work through @wanderview's concern. |
source
Outdated
<dt><var>result</var> = self . <code subdfn data-x="dom-structuredClone">structuredClone</code>( <var>value</var> | ||
[, { <code data-x="dom-PostMessageOptions-transfer">transfer</code> } ] )</dt> | ||
<dd> | ||
<p>Takes the input value and returns a deep copy by performing the structured clone algorithm. <span>Transferable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, maybe the answer here is that we update the note that starts with
This specification used to define a "structured clone" algorithm
to instead say that is the colloquial name and also the name of the API (xref)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
transferring the underlying data instead of copying it. Collectively this | ||
serialization/deserialization process is known as "structured cloning", although most APIs perform | ||
separate serialization and deserialization steps. (With the notable exception being the <code | ||
data-x="dom-structuredClone">structuredClone()</code> function.)</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data-x="dom-structuredClone">structuredClone()</code> function.)</p> | |
data-x="dom-structuredClone">structuredClone()</code> method.)</p> |
Hooray! 6+ years after the original feature request, and 3.5 years after the original PR, this is finally merged! I've updated the OP with implementation bug links for all browsers. |
🎉 I’ll make time to update the related MDN docs |
Automatic update from web-platform-tests Add tests for structuredClone Follows whatwg/html#3414. -- wpt-commits: 7cfc0be8d4d4c3ae374e1f8444a2309c0eefc563 wpt-pr: 11069
Automatic update from web-platform-tests Add tests for structuredClone Follows whatwg/html#3414. -- wpt-commits: 7cfc0be8d4d4c3ae374e1f8444a2309c0eefc563 wpt-pr: 11069
`StructuredDeserialize` expects the first argument to be a record containing the [[Type]] slot, as would be returned by `StructuredSerialize` and `StructuredSerializeForStorage`. `StructuredSerializeWithTransfer` returns instead a record with [[Serialized]] and [[TransferDataHolders]] slots, which must be deserialized using the `StructuredDeserializeWithTransfer` operation. It seems like this was an oversight in whatwg#3414.
whatwg/html#3414 introduced the `structuredClone()` API and renamed the `PostMessageOptions` dictionary to `StructuredSerializeOptions`. When implementing this change in Chromium (https://chromium-review.googlesource.com/c/chromium/src/+/3245334), it was decided that, since `structuredClone()` has no use for the `includeUserActivation` parameter, it was better to keep the `StructuredSerializeOptions` as per the spec and incorporate that parameter in a derived interface keeping the old name `PostMessageOptions`. This change updates the proposed IDL of the explainer to reflect these changes.
This is in connection to #793, to make the discussion a bit more tangible.
This is my first time touching the HTML spec and also my first time writing a spec section from scratch. I probably didn’t do everything right, so please be thorough in your review.
(See WHATWG Working Mode: Changes for more details.)
/common-dom-interfaces.html ( diff )
/index.html ( diff )
/structured-data.html ( diff )
/web-messaging.html ( diff )
/webappapis.html ( diff )
/window-object.html ( diff )
/workers.html ( diff )