-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
add a .then() method to the module-creating function in MODULARIZE #5085
Conversation
…llowing it to be used like a promise, and providing a uniform API for both sync and async compilation
Looks good to me! I ran into same problem when adding wasm mode to ogv.js; this looks like a handier solution than the manual use of onRuntimeInitialized I initially used, though I'll have to retool some of my initialization code away from the 'init function looks like a constructor' pattern to 'init function looks like a Promise' pattern. |
(Would it make sense to also have a .catch() that calls if we never initialize due to an error?) |
If all the rest of our startup were Promises that had |
Tested also on box2d.js, in the wasm branch there. Looks good there too. |
src/postamble.js
Outdated
func(Module); | ||
}); | ||
if (old) old(); |
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.
Shouldn't old
be called before func
?
I mean, in Module().then(f1).then(f2)
, I want f1
to always be called before f2
.
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.
Makes sense, I reordered it, thanks.
Working towards a .catch() is preferable because:
|
Agreed, yeah, we should work towards a Merging this for now as it's an improvement in the right direction, and already used by ammo.js and box2d.js successfully. |
This lets it be used as a promise, and works in both sync and async mode (we need async for wasm).
In modularize mode, the entire output is a function
Module()
(or another name, set by EXPORT_NAME). This is useful for libraries, so they don't influence the global scope. Til now most libraries, like box2d.js and ammo.js, assumed synchronous startup, which was made possible by not having a mem init file (which was convenient anyhow as for libraries having extra files is less good). But for wasm it looks like we can't depend on sync compilation.This PR adds a
then()
method, usable asBasically like a promise. The callback is called when the module is all compiled and ready to be used. It has the module as a parameter.
This is also perhaps a nicer API than the other methods for finding out when it is possible to call compiled code (e.g., onRuntimeInitialized), so in the long term, maybe we'd want to recommend people use modularize more, and this with it.
The wasm branch of ammo.js uses this. Looks like it works ok.