Skip to content

Commit 8719677

Browse files
authored
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.
1 parent 92281aa commit 8719677

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

packages/flutter_tools/lib/src/web/bootstrap.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -523,39 +523,27 @@ String generateDDCLibraryBundleMainModule({
523523
dartDevEmbedder.debugger.registerDevtoolsFormatter();
524524
525525
// Set up a final script that lets us know when all scripts have been loaded.
526+
// Only then can we call the main method.
526527
let onLoadEndSrc = '$onLoadEndBootstrap';
527528
window.\$dartLoader.loadConfig.bootstrapScript = {
528529
src: onLoadEndSrc,
529530
id: onLoadEndSrc,
530531
};
531532
window.\$dartLoader.loadConfig.tryLoadBootstrapScript = true;
532-
let dwdsCalledMain = false;
533-
let dartSrcsLoaded = false;
534-
let runMainWhenBoth = function() {
535-
// Only run once both all the scripts are loaded and DWDS triggers main.
536-
if (dwdsCalledMain && dartSrcsLoaded) {
533+
// Should be called by $onLoadEndBootstrap once all the scripts have been
534+
// loaded.
535+
window.$_onLoadEndCallback = function() {
536+
let child = {};
537+
child.main = function() {
537538
let sdkOptions = {
538539
nonNullAsserts: $nullAssertions,
539540
nativeNonNullAsserts: $nativeNullAssertions,
540541
};
541542
dartDevEmbedder.runMain(appName, sdkOptions);
542543
}
544+
/* MAIN_EXTENSION_MARKER */
545+
child.main();
543546
}
544-
// DWDS expects the main function to be lowercase.
545-
// TODO(srujzs): DWDS should be more robust to not have to require that.
546-
dwdsmain = function() {
547-
dwdsCalledMain = true;
548-
runMainWhenBoth();
549-
}
550-
// Should be called by $onLoadEndBootstrap once all the scripts have been
551-
// loaded.
552-
window.$_onLoadEndCallback = function() {
553-
dartSrcsLoaded = true;
554-
runMainWhenBoth();
555-
}
556-
557-
/* MAIN_EXTENSION_MARKER */
558-
dwdsmain();
559547
})();
560548
''';
561549
}

0 commit comments

Comments
 (0)