Skip to content

WorkerDOM.upgrade should wait for Worker init. #1047

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

Merged
merged 5 commits into from
Apr 20, 2021
Merged

WorkerDOM.upgrade should wait for Worker init. #1047

merged 5 commits into from
Apr 20, 2021

Conversation

samouri
Copy link
Member

@samouri samouri commented Apr 20, 2021

summary
Regular web workers w/ blob URLs are initialized synchronously, whereas the new IframeWorker introduced recently is initialized async. In order to ensure all sent messages end up received we have two options:

  1. Buffer all received messages within IframeWorker and send once it has been completely initialized.
  2. Force the WorkerDOM.upgrade to only resolve once the worker is ready.

I've opted to do both in this PR

@samouri samouri marked this pull request as ready for review April 20, 2021 15:52
@samouri samouri self-assigned this Apr 20, 2021
Copy link
Collaborator

@rcebulko rcebulko left a comment

Choose a reason for hiding this comment

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

Left a couple Qs but generally looks good to me

* @returns {Promise<void>}
*/
ready() {
return (this.worker as IframeWorker).readyPromise || Promise.resolve();
Copy link
Collaborator

Choose a reason for hiding this comment

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

In what circumstance would readyPromise be falsey if it's being initialized in the constructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

Worker will never have readyPromise (as well as WorkerImpl which is used in tests), whereas IframeWorker will.

@@ -65,11 +71,14 @@ class IframeWorker {
fetch(this.url.toString())
.then((res) => res.text())
.then((code) => {
if ((event.data as MessageFromIframe).type == 'iframe-ready') {
const data = event.data as MessageFromIframe;
if (data.type == 'iframe-ready') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to use a string literal instead of MESSAGE_TYPES.ready etc here? Wondering specifically if the latter might get compiled/minified better

Copy link
Member Author

@samouri samouri Apr 20, 2021

Choose a reason for hiding this comment

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

TBH I'm fine with it either way. As of now, we don't have the struct for it here. There's less of a need in TS when it is properly typed, whereas in JS its helpful to avoid typos

Copy link
Collaborator

@rcebulko rcebulko Apr 20, 2021

Choose a reason for hiding this comment

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

Is there typechecking on string equality comparisons like this? I don't think there would be?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! The error looks like this:

This condition will always return 'false' since the types ... have no overlap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants