This repository was archived by the owner on Aug 29, 2021. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This resolves #64 ensuring that the execution of synchronous modules, whose execution can only begin after deeper asynchronous dependencies have already completed, remains synchronous with their parent modules.
This ensures that adding an asynchronous dependency to a module does not change the execution of that module which could be a source of bugs during application development - adding a new and unrelated dependency should not change the timing of the modules already in the graph.
Consider the following example:
main.js
where a.js contains:
and identically for b.js.
On execution we get "a", "b", "main" synchronously in order, followed by "a task" then "b task".
Now if we introduce a new dependency to this application which is async:
main.js
with async.js containing:
where async.js contains a top-level await, the entire execution timing changes to instead read - "a", "b", "a task", "b task", "async start", "async end", "main".
The issue here is that introducing a dependency can now alter the interrelated timings of existing modules, which may introduce execution bugs. Typically we don't think of introducing a dependency as something that can drastically change the execution semantics of our existing code! This effect is also viral in that all modules importing this "main" file will also have microtasks now being run on their individual completions too. No module can execute synchronously with main anymore.
The approach taken in this PR is to defer the execution of synchronous dependencies to happen after the async modules have resolved, instead of immediately.
The result is that in the example above we get the following execution - "async start", "async end", "a", "b", "main", where the execution of "a", "b", "main" remains identical in semantics to the execution of "a", "b", "main" before we added the asynchronous dependency. In addition all parent importers of main will continue to execute according to their previous execution timing as well, avoiding the execution timing difference propagating up al modules in the graph. What this means is that introducing an asynchronous dependency will no longer risk drastically changing the execution semantics of our existing code from as it was before we added the asynchronous dependency.
The final approach turned out to be quite simple to implement.Circular references and error handling invariants are maintained in that cycles transition through all states together, and errors stop further execution of synchronous modules, while also throwing into both sync and async cycles as a single strongly connected component transition.