Move DWDS initialization into the onLoadEndCallback for the DDC library bundle format (#163338)

The existing code assumed that DWDS needed to initialize some of the JS
state between adding all the scripts to queue and when they are loaded.
A closer examination of the existing AMD module format bootstrapper
shows that we already only initialize DWDS' JS state after all the
scripts are loaded. This is because it waits for the entrypoint to be
loaded before initializing that state, and the entrypoint is only loaded
after all its dependencies are loaded, which includes the SDK and
transitively every file in the app.

Since it's simpler and avoids the double-gating to call main, this
change moves that initialization to after all the scripts are loaded and
aligns with the AMD module bootstrapper.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
This commit is contained in:
Srujan Gaddam 2025-02-14 16:02:09 -08:00 committed by GitHub
parent 92281aae46
commit 87196771eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -523,39 +523,27 @@ String generateDDCLibraryBundleMainModule({
dartDevEmbedder.debugger.registerDevtoolsFormatter(); dartDevEmbedder.debugger.registerDevtoolsFormatter();
// Set up a final script that lets us know when all scripts have been loaded. // Set up a final script that lets us know when all scripts have been loaded.
// Only then can we call the main method.
let onLoadEndSrc = '$onLoadEndBootstrap'; let onLoadEndSrc = '$onLoadEndBootstrap';
window.\$dartLoader.loadConfig.bootstrapScript = { window.\$dartLoader.loadConfig.bootstrapScript = {
src: onLoadEndSrc, src: onLoadEndSrc,
id: onLoadEndSrc, id: onLoadEndSrc,
}; };
window.\$dartLoader.loadConfig.tryLoadBootstrapScript = true; window.\$dartLoader.loadConfig.tryLoadBootstrapScript = true;
let dwdsCalledMain = false; // Should be called by $onLoadEndBootstrap once all the scripts have been
let dartSrcsLoaded = false; // loaded.
let runMainWhenBoth = function() { window.$_onLoadEndCallback = function() {
// Only run once both all the scripts are loaded and DWDS triggers main. let child = {};
if (dwdsCalledMain && dartSrcsLoaded) { child.main = function() {
let sdkOptions = { let sdkOptions = {
nonNullAsserts: $nullAssertions, nonNullAsserts: $nullAssertions,
nativeNonNullAsserts: $nativeNullAssertions, nativeNonNullAsserts: $nativeNullAssertions,
}; };
dartDevEmbedder.runMain(appName, sdkOptions); dartDevEmbedder.runMain(appName, sdkOptions);
} }
/* MAIN_EXTENSION_MARKER */
child.main();
} }
// DWDS expects the main function to be lowercase.
// TODO(srujzs): DWDS should be more robust to not have to require that.
dwdsmain = function() {
dwdsCalledMain = true;
runMainWhenBoth();
}
// Should be called by $onLoadEndBootstrap once all the scripts have been
// loaded.
window.$_onLoadEndCallback = function() {
dartSrcsLoaded = true;
runMainWhenBoth();
}
/* MAIN_EXTENSION_MARKER */
dwdsmain();
})(); })();
'''; ''';
} }