Skip to content
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

promise cloneinto clarification #19125

Merged
merged 3 commits into from
Aug 12, 2022

Conversation

rebloor
Copy link
Contributor

@rebloor rebloor commented Aug 3, 2022

Summary

Add the documentation suggested by @Rob--W in cloneInto example code fails: Error: Encountered unsupported value type writing stack-scoped structured clone
#15059
to offer an option for cloning promises.

Related issues

Fixes #15059

Metadata

This PR…

  • Adds a new document
  • Rewrites (or significantly expands) a document
  • Fixes a typo, bug, or other error

@rebloor rebloor added the Content:WebExt WebExtensions docs label Aug 3, 2022
@rebloor rebloor requested a review from Rob--W August 3, 2022 23:17
@rebloor rebloor requested a review from a team as a code owner August 3, 2022 23:17
@rebloor rebloor self-assigned this Aug 3, 2022
@rebloor rebloor requested review from willdurand and removed request for a team August 3, 2022 23:17
@github-actions github-actions bot added the Content:Other Any docs not covered by another "Content:" label label Aug 3, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2022

Preview URLs

Flaws

None! 🎉

External URLs

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
Title: Sharing objects with page scripts
on GitHub

No new external URLs

(this comment was updated 2022-08-05 03:26:56.073439)


```js
window.messenger.notify("Message from the page script!");
```

In the special case of a Promise, which is not supported by [structured clone algorithm](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), the desired result can be achieved by using `window.Promise` instead of `Promise`, and then cloning the resolution value with `cloneInto` like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this after the next section and add a header such as "Promise cloning"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rob--W to clarify, you want it moved after the "Constructors from the page context" section?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, move it after that, and prepend a header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let result = { exampleKey: "exampleValue" };
resolve(cloneInto(result, window));
});
// now promise can be passed to the web page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rpl Is this sufficiently clear, or do we need to elaborate on how the value is passed to the webpage?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from my perspective, given that the preamble clearly states that a Promise can't be cloned, it should already make it clear that the resulting promise object from the snippet here should be passes as is to the webpage.

How that should be done seems like too much related to what the extension is doing (e.g. it could be passes as is as part of an event dispatched to the page, returned by a method exported to the webpage or set directly into a global using window.wrappedJSObject.someglobalname = promise and so I'm not sure it is good to point one out here, as it could be misleading.

Personally I'd like it with the current level of detail already provided by the current version of this PR.

If after these changes, this is still reported to us as not clear, then we may consider just create a complete example for the mdn/webextensions-examples repo and just link it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sgtm

@@ -188,12 +188,26 @@ window.wrappedJSObject.messenger = cloneInto(
{cloneFunctions: true});
```

Now page scripts will see a new property on the window, `messenger`, which has a function `notify()`:
Now page scripts sees a new property on the window, `messenger`, which has a function `notify()`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now page scripts sees a new property on the window, `messenger`, which has a function `notify()`:
Now page scripts will see a new property on the window, `messenger`, which has a function `notify()`:

The original text is more correct. This is describing a desired effect, not a fact, so future tense is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback. The best practice for technical writing is to use the present tense e.g. https://developers.google.com/style/tense. It generally makes the content clearer and more direct. For example, here we both say now and will, so does a page script see the new property now or at some undefined point in the future? However, thanks for commenting as it's highlighted that I didn't correctly make the change and I will fix that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, TIL. Well, as long as the grammar is fixed I'll be happy with this ^^

@willdurand willdurand removed their request for review August 4, 2022 11:38
@rebloor rebloor merged commit 7be5afe into mdn:main Aug 12, 2022
@rebloor rebloor deleted the Promise-cloneinto-clarification branch August 12, 2022 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Other Any docs not covered by another "Content:" label Content:WebExt WebExtensions docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cloneInto example code fails: Error: Encountered unsupported value type writing stack-scoped structured clone
4 participants