load method

Future<void> load(
  1. String family,
  2. List<Uint8List> files
)

Implementation

Future<void> load(String family, List<Uint8List> files) async {
  final known = _families.putIfAbsent(family, () => []);
  final pending = <Future<void>>[];
  final fresh = <Uint8List>[];
  for (final bytes in files) {
    final existing = known
        .where((entry) => listEquals(entry.bytes, bytes))
        .firstOrNull;
    if (existing != null) {
      // A registration that finished is not waited for again: its future
      // may belong to a zone that no longer runs (a test before this one,
      // the render server's last request), and would never answer here.
      if (!existing.registered) pending.add(existing.loaded);
    } else if (!fresh.any((other) => listEquals(other, bytes))) {
      fresh.add(bytes);
    }
  }
  if (fresh.isNotEmpty) {
    late final Future<void> done;
    done = Future<void>.sync(() => register(family, fresh)).catchError((
      Object error,
      StackTrace stack,
    ) {
      known.removeWhere((entry) => identical(entry.loaded, done));
      Error.throwWithStackTrace(error, stack);
    });
    final entries = [
      for (final bytes in fresh)
        _RegisteredFont(Uint8List.fromList(bytes), done),
    ];
    known.addAll(entries);
    unawaited(done.then((_) {
      for (final entry in entries) {
        entry.registered = true;
      }
    }, onError: (Object _) {}));
    pending.add(done);
  }
  await Future.wait(pending);
}