Unverified Commit ea082092 authored by Christian Mürtz's avatar Christian Mürtz Committed by GitHub

Use scheduleTask for adding licenses (#54493)

parent 34895351
...@@ -89,12 +89,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { ...@@ -89,12 +89,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
} }
Stream<LicenseEntry> _addLicenses() async* { Stream<LicenseEntry> _addLicenses() async* {
// We use timers here (rather than scheduleTask from the scheduler binding) // Using _something_ here to break
// because the services layer can't use the scheduler binding (the scheduler
// binding uses the services layer to manage its lifecycle events). Timers
// are what scheduleTask uses under the hood anyway. The only difference is
// that these will just run next, instead of being prioritized relative to
// the other tasks that might be running. Using _something_ here to break
// this into two parts is important because isolates take a while to copy // this into two parts is important because isolates take a while to copy
// data at the moment, and if we receive the data in the same event loop // data at the moment, and if we receive the data in the same event loop
// iteration as we send the data to the next isolate, we are definitely // iteration as we send the data to the next isolate, we are definitely
...@@ -105,14 +100,14 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { ...@@ -105,14 +100,14 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
// https://github.com/dart-lang/sdk/issues/31960 // https://github.com/dart-lang/sdk/issues/31960
// TODO(ianh): Remove this complexity once these bugs are fixed. // TODO(ianh): Remove this complexity once these bugs are fixed.
final Completer<String> rawLicenses = Completer<String>(); final Completer<String> rawLicenses = Completer<String>();
Timer.run(() async { scheduleTask(() async {
rawLicenses.complete(rootBundle.loadString('LICENSE', cache: false)); rawLicenses.complete(rootBundle.loadString('LICENSE', cache: false));
}); }, Priority.animation);
await rawLicenses.future; await rawLicenses.future;
final Completer<List<LicenseEntry>> parsedLicenses = Completer<List<LicenseEntry>>(); final Completer<List<LicenseEntry>> parsedLicenses = Completer<List<LicenseEntry>>();
Timer.run(() async { scheduleTask(() async {
parsedLicenses.complete(compute(_parseLicenses, await rawLicenses.future, debugLabel: 'parseLicenses')); parsedLicenses.complete(compute(_parseLicenses, await rawLicenses.future, debugLabel: 'parseLicenses'));
}); }, Priority.animation);
await parsedLicenses.future; await parsedLicenses.future;
yield* Stream<LicenseEntry>.fromIterable(await parsedLicenses.future); yield* Stream<LicenseEntry>.fromIterable(await parsedLicenses.future);
} }
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/services/binding.dart';
import 'package:flutter_test/flutter_test.dart';
const String license1 = '''
L1Package1
L1Package2
L1Package3
L1Paragraph1
L1Paragraph2
L1Paragraph3''';
const String license2 = '''
L2Package1
L2Package2
L2Package3
L2Paragraph1
L2Paragraph2
L2Paragraph3''';
const String licenses = '''
$license1
--------------------------------------------------------------------------------
$license2
''';
class TestBinding extends BindingBase with SchedulerBinding, ServicesBinding {
@override
BinaryMessenger createBinaryMessenger() {
return super.createBinaryMessenger()
..setMockMessageHandler('flutter/assets', (ByteData message) async {
if (const StringCodec().decodeMessage(message) == 'LICENSE') {
return const StringCodec().encodeMessage(licenses);
}
return null;
});
}
}
void main() {
test('Adds rootBundle LICENSES to LicenseRegistry', () async {
TestBinding(); // The test binding registers a mock handler that returns licenses for the LICENSE key
final List<LicenseEntry> licenses = await LicenseRegistry.licenses.toList();
expect(licenses[0].packages, equals(<String>['L1Package1', 'L1Package2', 'L1Package3']));
expect(licenses[0].paragraphs.map((LicenseParagraph p) => p.text),
equals(<String>['L1Paragraph1', 'L1Paragraph2', 'L1Paragraph3']));
expect(licenses[1].packages, equals(<String>['L2Package1', 'L2Package2', 'L2Package3']));
expect(licenses[1].paragraphs.map((LicenseParagraph p) => p.text),
equals(<String>['L2Paragraph1', 'L2Paragraph2', 'L2Paragraph3']));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment