Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cade0e9

Browse files
authored
[web] Batch systemFontChange messages (#17885)
* Batch systemFontChange messages * Update test for async
1 parent 4bcfae8 commit cade0e9

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/web_ui/lib/src/engine/util.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,23 @@ void applyWebkitClipFix(html.Element containerElement) {
461461

462462
final ByteData _fontChangeMessage = JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'fontsChange'});
463463

464+
// Font load callbacks will typically arrive in sequence, we want to prevent
465+
// sendFontChangeMessage of causing multiple synchronous rebuilds.
466+
// This flag ensures we properly schedule a single call to framework.
467+
bool _fontChangeScheduled = false;
468+
464469
FutureOr<void> sendFontChangeMessage() async {
465470
if (window._onPlatformMessage != null)
466-
window.invokeOnPlatformMessage(
467-
'flutter/system',
468-
_fontChangeMessage,
469-
(_) {},
470-
);
471+
if (!_fontChangeScheduled) {
472+
_fontChangeScheduled = true;
473+
// Batch updates into next animationframe.
474+
html.window.requestAnimationFrame((num _) {
475+
_fontChangeScheduled = false;
476+
window.invokeOnPlatformMessage(
477+
'flutter/system',
478+
_fontChangeMessage,
479+
(_) {},
480+
);
481+
});
482+
}
471483
}

lib/web_ui/test/text/font_loading_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
// @dart = 2.6
6+
import 'dart:async';
67
import 'dart:convert';
78
import 'dart:html' as html;
89
import 'dart:typed_data';
@@ -85,6 +86,9 @@ Future<void> main() async {
8586
responseType: 'arraybuffer');
8687
await ui.loadFontFromList(Uint8List.view(response.response),
8788
fontFamily: 'Blehm');
89+
final Completer<void> completer = Completer();
90+
html.window.requestAnimationFrame( (_) { completer.complete(true); } );
91+
await(completer.future);
8892
window.onPlatformMessage = oldHandler;
8993
expect(actualName, 'flutter/system');
9094
expect(message, '{"type":"fontsChange"}');

0 commit comments

Comments
 (0)