runInComputeIsolateStream<Q, P> method

Stream<P> runInComputeIsolateStream<Q, P>(
  1. StreamComputeTask<Q, P> task,
  2. Q argument
)

dedicated for compute operations that need streaming progress updates

Example:

Stream<int> progress = IsolateManager.instance
    .runInComputeIsolateStream<String, int>(
  (path, emit) async {
    emit(0);
    // ...do work in chunks and emit updates...
    emit(100);
  },
  '/tmp/file.bin',
);

await for (final value in progress) {
  print('progress: $value%');
}

Implementation

Stream<P> runInComputeIsolateStream<Q, P>(
  StreamComputeTask<Q, P> task,
  Q argument,
) async* {
  await ready;

  final sendPort = _computePool.getNextSendPort();
  final port = ReceivePort();
  sendPort.send(['stream', task, argument, port.sendPort]);

  await for (final message in port) {
    if (message is Map && message['type'] == 'progress') {
      yield message['data'] as P;
    } else if (message is Map && message['type'] == 'done') {
      port.close();
      break;
    } else if (message is Map && message['error'] != null) {
      port.close();
      throw Exception(message['error']);
    }
  }
}