From cade0e90ca529869788573ba2b48ac9cee3e367b Mon Sep 17 00:00:00 2001 From: Ferhat Date: Fri, 24 Apr 2020 14:10:22 -0700 Subject: [PATCH] [web] Batch systemFontChange messages (#17885) * Batch systemFontChange messages * Update test for async --- lib/web_ui/lib/src/engine/util.dart | 22 ++++++++++++++++----- lib/web_ui/test/text/font_loading_test.dart | 4 ++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/web_ui/lib/src/engine/util.dart b/lib/web_ui/lib/src/engine/util.dart index 5063d71df..297983811 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, + (_) {}, + ); + }); + } } diff --git a/lib/web_ui/test/text/font_loading_test.dart b/lib/web_ui/test/text/font_loading_test.dart index 441212c47..3ec78a53a 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"}'); -- GitLab