From ede5f51fa285dcb702adddc34525b23e05d3c59c Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 7 Mar 2023 10:36:10 -0600 Subject: [PATCH] refactor(shorebird_cli): use Isolate.run (#32) --- .../lib/src/commands/run_command.dart | 9 ++-- packages/shorebird_cli/lib/src/compute.dart | 41 ------------------- .../shorebird_cli/test/src/compute_test.dart | 17 -------- 3 files changed, 4 insertions(+), 63 deletions(-) delete mode 100644 packages/shorebird_cli/lib/src/compute.dart delete mode 100644 packages/shorebird_cli/test/src/compute_test.dart diff --git a/packages/shorebird_cli/lib/src/commands/run_command.dart b/packages/shorebird_cli/lib/src/commands/run_command.dart index ba633faa..099d58e5 100644 --- a/packages/shorebird_cli/lib/src/commands/run_command.dart +++ b/packages/shorebird_cli/lib/src/commands/run_command.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'dart:io'; +import 'dart:isolate'; import 'package:archive/archive_io.dart'; import 'package:args/command_runner.dart'; @@ -7,7 +8,6 @@ import 'package:mason_logger/mason_logger.dart'; import 'package:path/path.dart' as p; import 'package:shorebird_cli/src/auth/auth.dart'; import 'package:shorebird_cli/src/command_runner.dart'; -import 'package:shorebird_cli/src/compute.dart'; import 'package:shorebird_code_push_api_client/shorebird_code_push_api_client.dart'; typedef StartProcess = Future Function( @@ -157,12 +157,11 @@ Future _extractShorebirdEngine( targetDirectory.createSync(recursive: true); - await compute( - (path) async { - final inputStream = InputFileStream(path); + await Isolate.run( + () async { + final inputStream = InputFileStream(archivePath); final archive = ZipDecoder().decodeBuffer(inputStream); extractArchiveToDisk(archive, targetPath); }, - archivePath, ); } diff --git a/packages/shorebird_cli/lib/src/compute.dart b/packages/shorebird_cli/lib/src/compute.dart deleted file mode 100644 index 8395c02a..00000000 --- a/packages/shorebird_cli/lib/src/compute.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'dart:async'; -import 'dart:isolate'; - -/// Perform [computation] with [input] in an [Isolate]. -Future compute(FutureOr Function(M) computation, M input) async { - final resultPort = ReceivePort(); - final errorPort = ReceivePort(); - - await Isolate.spawn<_IsolateConfig>>( - _spawn, - _IsolateConfig>(computation, input, resultPort.sendPort), - onError: errorPort.sendPort, - ); - - final result = Completer(); - errorPort.listen((dynamic errorData) { - final data = errorData as List; - final exception = Exception(data[0]); - final stack = StackTrace.fromString(data[1] as String); - result.completeError(exception, stack); - }); - resultPort.listen((dynamic resultData) => result.complete(resultData as R)); - await result.future; - resultPort.close(); - errorPort.close(); - return result.future; -} - -class _IsolateConfig { - const _IsolateConfig(this.callback, this.message, this.resultPort); - - final R Function(M message) callback; - final M message; - final SendPort resultPort; - - FutureOr compute() => callback(message); -} - -Future _spawn(_IsolateConfig> configuration) async { - Isolate.exit(configuration.resultPort, await configuration.compute()); -} diff --git a/packages/shorebird_cli/test/src/compute_test.dart b/packages/shorebird_cli/test/src/compute_test.dart deleted file mode 100644 index 458e555f..00000000 --- a/packages/shorebird_cli/test/src/compute_test.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:shorebird_cli/src/compute.dart'; -import 'package:test/test.dart'; - -int test1(int value) => value + 1; -int test2(int value) => throw Exception(); -Future test1Async(int value) async => value + 1; -Future test2Async(int value) async => throw Exception(); - -void main() { - test('compute()', () async { - expect(await compute(test1, 0), 1); - expect(compute(test2, 0), throwsException); - - expect(await compute(test1Async, 0), 1); - expect(compute(test2Async, 0), throwsException); - }); -}