From ed25a24355a8f533b20ba096b7089f4595f1cc5d Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 7 Jun 2021 12:19:02 -0700 Subject: [PATCH] Fixes handleNavigationMessage in order (flutter/engine#26596) --- .../lib/web_ui/lib/src/engine/window.dart | 70 ++++++++++++------- .../flutter/lib/web_ui/test/window_test.dart | 51 ++++++++++++++ 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/window.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/window.dart index bbaa57224b..14b55540c6 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/window.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/window.dart @@ -4,6 +4,8 @@ part of engine; +typedef _HandleMessageCallBack = Future Function(); + /// When set to true, all platform messages will be printed to the console. const bool /*!*/ _debugPrintPlatformMessages = false; @@ -112,32 +114,50 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow { _customUrlStrategy = null; } - Future handleNavigationMessage(ByteData? data) async { - final MethodCall decoded = JSONMethodCodec().decodeMethodCall(data); - final Map? arguments = decoded.arguments; - switch (decoded.method) { - case 'selectMultiEntryHistory': - await _useMultiEntryBrowserHistory(); - return true; - case 'selectSingleEntryHistory': - await _useSingleEntryBrowserHistory(); - return true; - // the following cases assert that arguments are not null - case 'routeUpdated': // deprecated - assert(arguments != null); - await _useSingleEntryBrowserHistory(); - browserHistory.setRouteName(arguments!['routeName']); - return true; - case 'routeInformationUpdated': - assert(arguments != null); - browserHistory.setRouteName( - arguments!['location'], - state: arguments['state'], - replace: arguments['replace'] ?? false, - ); - return true; + Future _endOfTheLine = Future.value(null); + + Future _waitInTheLine(_HandleMessageCallBack callback) async { + final Future currentPosition = _endOfTheLine; + final Completer completer = Completer(); + _endOfTheLine = completer.future; + await currentPosition; + bool result = false; + try { + result = await callback(); + } finally { + completer.complete(); } - return false; + return result; + } + + Future handleNavigationMessage(ByteData? data) async { + return _waitInTheLine(() async { + final MethodCall decoded = JSONMethodCodec().decodeMethodCall(data); + final Map? arguments = decoded.arguments; + switch (decoded.method) { + case 'selectMultiEntryHistory': + await _useMultiEntryBrowserHistory(); + return true; + case 'selectSingleEntryHistory': + await _useSingleEntryBrowserHistory(); + return true; + // the following cases assert that arguments are not null + case 'routeUpdated': // deprecated + assert(arguments != null); + await _useSingleEntryBrowserHistory(); + browserHistory.setRouteName(arguments!['routeName']); + return true; + case 'routeInformationUpdated': + assert(arguments != null); + browserHistory.setRouteName( + arguments!['location'], + state: arguments['state'], + replace: arguments['replace'] ?? false, + ); + return true; + } + return false; + }); } @override diff --git a/engine/src/flutter/lib/web_ui/test/window_test.dart b/engine/src/flutter/lib/web_ui/test/window_test.dart index 9ba60c4798..687adf4882 100644 --- a/engine/src/flutter/lib/web_ui/test/window_test.dart +++ b/engine/src/flutter/lib/web_ui/test/window_test.dart @@ -133,6 +133,57 @@ void testMain() { }, throwsAssertionError); }); + test('handleNavigationMessage execute request in order.', () async { + // Start with multi entries. + await window.debugInitializeHistory(TestUrlStrategy.fromEntry( + TestHistoryEntry('initial state', null, '/initial'), + ), useSingle: false); + expect(window.browserHistory, isA()); + final List executionOrder = []; + window.handleNavigationMessage( + JSONMethodCodec().encodeMethodCall(MethodCall( + 'selectSingleEntryHistory', + null, + )) + ).then((bool data) { + executionOrder.add('1'); + }); + window.handleNavigationMessage( + JSONMethodCodec().encodeMethodCall(MethodCall( + 'selectMultiEntryHistory', + null, + )) + ).then((bool data) { + executionOrder.add('2'); + }); + window.handleNavigationMessage( + JSONMethodCodec().encodeMethodCall(MethodCall( + 'selectSingleEntryHistory', + null, + )) + ).then((bool data) { + executionOrder.add('3'); + }); + await window.handleNavigationMessage( + JSONMethodCodec().encodeMethodCall(MethodCall( + 'routeInformationUpdated', + { + 'location': '/baz', + 'state': null, + }, // boom + )) + ).then((bool data) { + executionOrder.add('4'); + }); + // The routeInformationUpdated should finish after the browser history + // has been set to single entry. + expect(executionOrder.length, 4); + expect(executionOrder[0], '1'); + expect(executionOrder[1], '2'); + expect(executionOrder[2], '3'); + expect(executionOrder[3], '4'); + }); + test('should not throw when using nav1 and nav2 together', () async { await window.debugInitializeHistory(TestUrlStrategy.fromEntry(