Unverified Commit 1fe677a3 authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

update _isolates_io.dart for better nnbd migration (#59666)

parent 90fb2e80
...@@ -18,6 +18,7 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St ...@@ -18,6 +18,7 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St
final Flow flow = Flow.begin(); final Flow flow = Flow.begin();
Timeline.startSync('$debugLabel: start', flow: flow); Timeline.startSync('$debugLabel: start', flow: flow);
final ReceivePort resultPort = ReceivePort(); final ReceivePort resultPort = ReceivePort();
final ReceivePort exitPort = ReceivePort();
final ReceivePort errorPort = ReceivePort(); final ReceivePort errorPort = ReceivePort();
Timeline.finishSync(); Timeline.finishSync();
final Isolate isolate = await Isolate.spawn<_IsolateConfiguration<Q, FutureOr<R>>>( final Isolate isolate = await Isolate.spawn<_IsolateConfiguration<Q, FutureOr<R>>>(
...@@ -30,7 +31,7 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St ...@@ -30,7 +31,7 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St
flow.id, flow.id,
), ),
errorsAreFatal: true, errorsAreFatal: true,
onExit: resultPort.sendPort, onExit: exitPort.sendPort,
onError: errorPort.sendPort, onError: errorPort.sendPort,
); );
final Completer<R> result = Completer<R>(); final Completer<R> result = Completer<R>();
...@@ -45,8 +46,13 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St ...@@ -45,8 +46,13 @@ Future<R> compute<Q, R>(isolates.ComputeCallback<Q, R> callback, Q message, { St
result.completeError(exception, stack); result.completeError(exception, stack);
} }
}); });
exitPort.listen((dynamic exitData) {
if (!result.isCompleted) {
result.completeError(Exception('Isolate exited without result or error.'));
}
});
resultPort.listen((dynamic resultData) { resultPort.listen((dynamic resultData) {
assert(resultData == null || resultData is R); assert(resultData is R);
if (!result.isCompleted) if (!result.isCompleted)
result.complete(resultData as R); result.complete(resultData as R);
}); });
...@@ -78,12 +84,11 @@ class _IsolateConfiguration<Q, R> { ...@@ -78,12 +84,11 @@ class _IsolateConfiguration<Q, R> {
} }
Future<void> _spawn<Q, R>(_IsolateConfiguration<Q, FutureOr<R>> configuration) async { Future<void> _spawn<Q, R>(_IsolateConfiguration<Q, FutureOr<R>> configuration) async {
R result; final R result = await Timeline.timeSync(
await Timeline.timeSync(
configuration.debugLabel, configuration.debugLabel,
() async { () async {
final FutureOr<R> applicationResult = await configuration.apply(); final FutureOr<R> applicationResult = await configuration.apply();
result = await applicationResult; return await applicationResult;
}, },
flow: Flow.step(configuration.flowId), flow: Flow.step(configuration.flowId),
); );
......
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