From b7eaad8b142148fe1b69b5954c12c45df52d1888 Mon Sep 17 00:00:00 2001 From: David Iglesias Date: Thu, 26 May 2022 17:50:10 -0700 Subject: [PATCH] [tool][web] Fix flutter.js in Safari 13 (#104761) --- .../src/web/file_generators/flutter_js.dart | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/flutter_tools/lib/src/web/file_generators/flutter_js.dart b/packages/flutter_tools/lib/src/web/file_generators/flutter_js.dart index af8950b1bc..6a5f761087 100644 --- a/packages/flutter_tools/lib/src/web/file_generators/flutter_js.dart +++ b/packages/flutter_tools/lib/src/web/file_generators/flutter_js.dart @@ -27,14 +27,23 @@ _flutter.loader = null; (function() { "use strict"; class FlutterLoader { - // TODO: Move the below methods to "#private" once supported by all the browsers - // we support. In the meantime, we use the "revealing module" pattern. + /** + * Creates a FlutterLoader, and initializes its instance methods. + */ + constructor() { + // TODO: Move the below methods to "#private" once supported by all the browsers + // we support. In the meantime, we use the "revealing module" pattern. - // Watchdog to prevent injecting the main entrypoint multiple times. - _scriptLoaded = null; + // Watchdog to prevent injecting the main entrypoint multiple times. + this._scriptLoaded = null; - // Resolver for the pending promise returned by loadEntrypoint. - _didCreateEngineInitializerResolve = null; + // Resolver for the pending promise returned by loadEntrypoint. + this._didCreateEngineInitializerResolve = null; + + // Called by Flutter web. + // Bound to `this` now, so "this" is preserved across JS <-> Flutter jumps. + this.didCreateEngineInitializer = this._didCreateEngineInitializer.bind(this); + } /** * Initializes the main.dart.js with/without serviceWorker. @@ -51,19 +60,19 @@ _flutter.loader = null; } /** - * Resolves the promise created by loadEntrypoint. Called by Flutter. - * Needs to be weirdly bound like it is, so "this" is preserved across - * the JS <-> Flutter jumps. + * Resolves the promise created by loadEntrypoint. + * Called by Flutter through the public `didCreateEngineInitializer` method, + * which is bound to the correct instance of the FlutterLoader on the page. * @param {*} engineInitializer */ - didCreateEngineInitializer = (function(engineInitializer) { + _didCreateEngineInitializer(engineInitializer) { if (typeof this._didCreateEngineInitializerResolve != "function") { console.warn("Do not call didCreateEngineInitializer by hand. Start with loadEntrypoint instead."); } this._didCreateEngineInitializerResolve(engineInitializer); - // Remove this method after it's done, so Flutter Web can hot restart. + // Remove the public method after it's done, so Flutter Web can hot restart. delete this.didCreateEngineInitializer; - }).bind(this); + } _loadEntrypoint(entrypointUrl) { if (!this._scriptLoaded) { @@ -71,7 +80,11 @@ _flutter.loader = null; let scriptTag = document.createElement("script"); scriptTag.src = entrypointUrl; scriptTag.type = "application/javascript"; - this._didCreateEngineInitializerResolve = resolve; // Cache the resolve, so it can be called from Flutter. + // Cache the resolve, so it can be called from Flutter. + // Note: Flutter hot restart doesn't re-create this promise, so this + // can only be called once. Instead, we need to model this as a stream + // of `engineCreated` events coming from Flutter that are handled by JS. + this._didCreateEngineInitializerResolve = resolve; scriptTag.addEventListener("error", reject); document.body.append(scriptTag); });