stream method

ActionStream<Chunk, Output> stream(
  1. Input? input, {
  2. Map<String, dynamic>? context,
  3. Stream<Input>? inputStream,
  4. Init? init,
})

Implementation

ActionStream<Chunk, Output> stream(
  Input? input, {
  Map<String, dynamic>? context,
  Stream<Input>? inputStream,
  Init? init,
}) {
  final streamController = StreamController<Chunk>();
  final actionStream = ActionStream<Chunk, Output>(streamController.stream);

  run(
        input,
        context: context,
        inputStream: inputStream,
        init: init,
        onChunk: (chunk) {
          if (!streamController.isClosed) {
            streamController.add(chunk);
          }
        },
      )
      .then((result) {
        actionStream.setResult(result.result);
        if (!streamController.isClosed) {
          streamController.close();
        }
      })
      .catchError((Object e, StackTrace s) {
        actionStream.setError(e, s);
        if (!streamController.isClosed) {
          streamController.addError(e, s);
          streamController.close();
        }
      });

  return actionStream;
}