[web] dispatch browser event on flutter/service_worker channel (flutter/engine#21284)

Registering the service worker immediately after the documented has loaded may cause SW initialization to compete with framework initialization. It was recommended to us that we defer the service worker setup until after the framework is done with setup, which should be sometime after the first frame.

To implement this, the binding will dispatch a platform message on startup. This can be listened for in the html document
#66066
This commit is contained in:
Jonah Williams
2020-09-22 07:37:23 -07:00
committed by GitHub
parent f1dce7763f
commit 587bfccae9
2 changed files with 21 additions and 0 deletions

View File

@@ -603,6 +603,11 @@ class EngineWindow extends ui.Window {
}
break;
// Dispatched by the bindings to delay service worker initialization.
case 'flutter/service_worker':
html.window.dispatchEvent(html.Event('flutter-first-frame'));
return;
case 'flutter/textinput':
textEditing.channel.handleTextInput(data, callback);
return;

View File

@@ -255,4 +255,20 @@ void testMain() {
expect(window.locales, isNotEmpty);
expect(localeChangedCount, 1);
});
test('dispatches browser event on flutter/service_worker channel', () async {
final Completer<void> completer = Completer<void>();
html.window.addEventListener('flutter-first-frame', completer.complete);
final Zone innerZone = Zone.current.fork();
innerZone.runGuarded(() {
window.sendPlatformMessage(
'flutter/service_worker',
ByteData(0),
(outputData) { },
);
});
await expectLater(completer.future, completes);
});
}