`ListenableAsyncEnumerable` is a C# implementation of [Dart](https://dart.dev)s `Stream` infrastructure. It adds functionality to `IAsyncEnumerable` instances to be able to listen to the data emitted by them.
$ dotnet add package ListenableAsyncEnumerableListenableAsyncEnumerable is a C# implementation of Darts Stream infrastructure.
For now, only basic functionality is covered.
To obtain an IListenableAsyncEnumerable<T>, call the ToListenable extension method on any IAsyncEnumerable<T>:
IAsyncEnumerable<string> enumerable = GetMyAsyncEnumerable();
IListenableAsyncEnumerable<string> listenable = enumerable.ToListenable();
Call the Listen method on an IListenableAsyncEnumerable<T> to obtain an IAsyncEnumerableSubscription<T>:
using IAsyncEnumerableSubscription<string> subscription = listenable.Listen(
onData: value => HandleMyValue(value),
onError: exception => HandleException(exception),
onDone: () => HandleRiverDone()
);
await foreach (var value in listenable) {
// Do something with the value
}
// Cancel the subscription if you don't need it anymore
// It will also get cancelled when Dispose is called
subscription.Cancel();
Calling Listen will not enumerate the enumerable.
The callbacks passed to the Listen method only get invoked once the enumerable actually gets enumerated.
Contributions are welcome. Open an issue or a PR if you want.
This project is licensed under the MIT license.