-
Notifications
You must be signed in to change notification settings - Fork 11
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
Do we really need to wait for the ensembleLoaded event? #85
Comments
I may have figured this one out! If you do a bit of script surgery on the compiled require(["js/ensemble/ensemble"]); ...to this instead (removing the square brackets): require("js/ensemble/ensemble"); ...then the global As far as I can tell, this is because square brackets around the name of the module you're trying to load in a More information here: requirejs/almond#42 (comment) If this is actually the cause, then we need to somehow tweak our build process to produce a version of the standalone compiled |
This is an attempt to fix the issue described in #85.
Commit fed07bb represents my first pass at trying to fix this, and seems to work on my machine. @meldckn, could you confirm it works for you as well? To test the changes:
Your scripts should now be able to access the Note that this commit ends up double- require(["js/ensemble/ensemble"]);
return require("js/ensemble/ensemble");
}()); I don't think this should cause any actual problems, but it's kind of ugly, and it'd be nice to be able to remove the async For more information on how this commit was implemented, using the |
That did it! Woop! (Also, do you think it would be possible to remove RequireJS entirely? Isn't all it's doing concatenating JS files in the order specified by |
Removing RequireJS entirely is almost certainly possible, but nontrivial. Right now all of the Ensemble JS files are using RequireJS to get access to their dependencies and exporting themselves as RequireJS modules using Related to this issue is the problem of stripping out unused or mostly-unused external dependencies from the Ensemble JS files, as discussed in #36. I've started going through and marking which of the functions from these large external libraries we actually use, and in many cases we're not using them for very much – but if we wanted to drop our external dependencies altogether, we'd still have to grab the functions we actually are using and put them in some sort of internal utils file. |
Hmmm got it. Thanks for the thoughtful explanation and new issue! I wonder how modern JS libraries build their standalone files nowadays. Do you think, as we're making updates generally, that we should strive for backwards-compatibility? Like, should we remove the If we're keeping it around, seems like this issue can be closed. 🎉 And we'll remove |
My inclination is to go ahead and remove the In this case specifically, the browser-API-reliant code that creates and dispatches the |
Arguably comparable JS libraries (like p5js, nlp-compromise, Ritajs, Tracery, etc), don't require you to explicitly wait for their library to load before calling any of their functions. It is sufficient to just include their script tags in your main HTML in order from library to code-that-uses-library.
But Ensemble requires the unusual pattern of waiting for a custom event (
ensembleLoaded
) that is triggered at the end of the standaloneensemble.js
file, and then putting anyensemble
function calls in the scope of that event handler's callback. You can also wait for the Window load event (but that waits for every resource, like images if you have an image-heavy game, so the custom event is probably better).Btw, sometimes your webpage will successfully load with just the well-ordered script tags and no
ensembleLoaded
event listener. But I found that it would fail to load in the correct order maybe 50% of the time when you refresh the page many times in a row, throwingUncaught ReferenceError: ensemble is not defined
in the failure case. (This was on Chrome on macOS 10.14.5 with the sampleGame.)Investigate why you need to explicitly wait for
ensemble
to load withensembleLoaded
or window load event, so that we could potentially remove this odd pattern in the future.Max's suggestion: Maybe ensemble somehow defers executing itself until after pageload/another page lifecycle event for some reason?
The text was updated successfully, but these errors were encountered: