From 34d7742abb00aba077e865ef48ffb783578503c7 Mon Sep 17 00:00:00 2001 From: ferhatb Date: Wed, 22 Apr 2020 14:29:44 -0700 Subject: [PATCH 1/2] Batch systemFontChange messages --- lib/web_ui/lib/src/engine/util.dart | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/web_ui/lib/src/engine/util.dart b/lib/web_ui/lib/src/engine/util.dart index 5063d71df9469..2979838112854 100644 --- a/lib/web_ui/lib/src/engine/util.dart +++ b/lib/web_ui/lib/src/engine/util.dart @@ -461,11 +461,23 @@ void applyWebkitClipFix(html.Element containerElement) { final ByteData _fontChangeMessage = JSONMessageCodec().encodeMessage({'type': 'fontsChange'}); +// Font load callbacks will typically arrive in sequence, we want to prevent +// sendFontChangeMessage of causing multiple synchronous rebuilds. +// This flag ensures we properly schedule a single call to framework. +bool _fontChangeScheduled = false; + FutureOr sendFontChangeMessage() async { if (window._onPlatformMessage != null) - window.invokeOnPlatformMessage( - 'flutter/system', - _fontChangeMessage, - (_) {}, - ); + if (!_fontChangeScheduled) { + _fontChangeScheduled = true; + // Batch updates into next animationframe. + html.window.requestAnimationFrame((num _) { + _fontChangeScheduled = false; + window.invokeOnPlatformMessage( + 'flutter/system', + _fontChangeMessage, + (_) {}, + ); + }); + } } From ce9dedd2bbbc7b61e3ad0cfcba79ff07bc1b59c2 Mon Sep 17 00:00:00 2001 From: ferhatb Date: Thu, 23 Apr 2020 16:10:42 -0700 Subject: [PATCH 2/2] Update test for async --- lib/web_ui/test/text/font_loading_test.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/web_ui/test/text/font_loading_test.dart b/lib/web_ui/test/text/font_loading_test.dart index 441212c47dcf8..3ec78a53aa871 100644 --- a/lib/web_ui/test/text/font_loading_test.dart +++ b/lib/web_ui/test/text/font_loading_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. // @dart = 2.6 +import 'dart:async'; import 'dart:convert'; import 'dart:html' as html; import 'dart:typed_data'; @@ -85,6 +86,9 @@ Future main() async { responseType: 'arraybuffer'); await ui.loadFontFromList(Uint8List.view(response.response), fontFamily: 'Blehm'); + final Completer completer = Completer(); + html.window.requestAnimationFrame( (_) { completer.complete(true); } ); + await(completer.future); window.onPlatformMessage = oldHandler; expect(actualName, 'flutter/system'); expect(message, '{"type":"fontsChange"}');