Delaying execution of modules in the loader
The loader is designed to enable deferred execution, but there isn't an easy hook to allow this that I know of.
System.define is the only load function that does not ensure execution. The others (System.import, System.load, System.module), run an EnsureEvaluated call to run the scripts.
It could certainly be worth considering a new load function that runs the hook cycles but does not evaluate - System.preload for example. The implementation is trivial as well.
On Tue, 19 Aug 2014, Guy Bedford wrote:
The loader is designed to enable deferred execution, but there isn't an easy hook to allow this that I know of.
System.define is the only load function that does not ensure execution. The others (System.import, System.load, System.module), run an EnsureEvaluated call to run the scripts.
It could certainly be worth considering a new load function that runs the hook cycles but does not evaluate - System.preload for example. The implementation is trivial as well.
Oh, interesting. You mean that just calling LoadModule() doesn't actually trigger any code to execute? That would solve my problem, I just wouldn't call EnsureEvaluated() until I needed the modules.
On Tue, Aug 19, 2014 at 2:55 PM, Guy Bedford <guybedford at gmail.com> wrote:
The loader is designed to enable deferred execution, but there isn't an easy hook to allow this that I know of.
System.define is the only load function that does not ensure execution. The others (System.import, System.load, System.module), run an EnsureEvaluated call to run the scripts.
This doesn't sound right to me (but I could be misunderstanding the spec).
By my reading, "EvaluateLoadedModule" is only called for Loader.prototype.import and Loader.prototype.module.
?
It would be helpful if there was a way that the module execution (after it's been parsed and dependencies have been extracted) could be delayed by the loader.
Suppose a page is loading and has reached a quiescent state, and so the browser thinks "ok, time to preload some scripts". It might start with one script, and then find that it imports another, and would then fetch that one. But it doesn't want to actually execute anything, because the scripts haven't been invoked yet.
Since ES6 does all the heavy lifting of finding the imports in the source code and setting up all the linking, it would be good if the browser could rely on that but just put a stop to the final execution step until such time as the resource is actually needed.
This would also allow for ES6 import syntax to be used inline to declare some dependencies, as in:
<script type=module when-needed> import "jquery"; import "jquery/animations"; import "myapp/logic"; </script>
If the browser could notice that this was an inline script and have the ES6 module system pre-parse it to discover the imports, it could preload them and then when the <script> element's execute() method is invoked it
could just unblock the execution and immediately have the scripts run.