-
Notifications
You must be signed in to change notification settings - Fork 155
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
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.
Left a couple Qs but generally looks good to me
* @returns {Promise<void>} | ||
*/ | ||
ready() { | ||
return (this.worker as IframeWorker).readyPromise || Promise.resolve(); |
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.
In what circumstance would readyPromise
be falsey if it's being initialized in the constructor?
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.
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') { |
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.
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
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.
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
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.
Is there typechecking on string equality comparisons like this? I don't think there would be?
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.
Yep! The error looks like this:
This condition will always return 'false' since the types ... have no overlap
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:IframeWorker
and send once it has been completely initialized.WorkerDOM.upgrade
to only resolve once the worker is ready.I've opted to do both in this PR