cancelable_stream 1.0.0 copy "cancelable_stream: ^1.0.0" to clipboard
cancelable_stream: ^1.0.0 copied to clipboard

A stream wrapper that supports explicit cancellation of the upstream source.

cancelable_stream #

A Dart stream wrapper that allows streams to be canceled, unsubscribing from the upstream source and completing downstream listeners.

Getting started #

The package works with any Dart project. Make sure your pubspec.yaml includes:

dependencies:
  cancelable_stream: ^1.0.0

Usage #

A simple example of using CancelableStream:

import 'package:cancelable_stream/cancelable_stream.dart';

void main() async {
  // A stream that emits numbers at one-second intervals
  createStream() async* {
    for (var value in [1, 2, 3, 4, 5]) {
      await Future.delayed(Duration(seconds: 1));
      print('Emit $value');
      yield value;
    }
    print('Producer done');
  }

  // Wrap the stream so it can be canceled
  final stream = createStream().cancelable();

  // Cancel the stream after 2.5 seconds
  Future.delayed(Duration(milliseconds: 2500), stream.cancel);

  // Listen to the events
  await for (var value in stream) {
    print('Consume $value');
  }
  print('Subscriber done');
}

The output will be:

Emit 1
Consume 1
Emit 2
Consume 2
Emit 3   // Print occurs before yield; the value is never delivered
Subscriber done

Additional information #

Contributions are welcome. If you find bugs, have feature requests, or want to improve the package, please open an issue or submit a pull request on the GitHub repository.

0
likes
160
points
31
downloads

Publisher

unverified uploader

Weekly Downloads

A stream wrapper that supports explicit cancellation of the upstream source.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on cancelable_stream