Experimental operators for C# 8 IAsyncEnumerable
s.
Namespace: async_enumerable_dotnet
Factory/Extension methods: AsyncEnumerable
Requires: .NET Core 3 or later
using async_enumerable_dotnet;
using System.Collections.Generic;
var result = AsyncEnumerable.Range(1, 10)
.Filter(v => v % 2 == 0)
.Map(v => v * 2)
.Take(5)
;
var enumerator = result.GetAsyncEnumerator();
try
{
while (await enumerator.MoveNextAsync())
{
Console.WriteLine(enumerator.Current);
}
Console.WriteLine("Done");
}
finally
{
await enumerator.DisposeAsync();
}
Amb
- Relay items of the source that responds first, disposing the othersCreate
- generate values via async pushCombineLatest
- combines the latest items of the source async sequences via a function into resultsConcat
- concatenate multiple async sequencesConcatEager
- run multiple async sequences at once but relay elements in order similar toConcat
Defer
- defer the creation of the actualIAsyncEnumerable
Error
- signal an errorEmpty
- the async sequence ends without any valuesFromArray
- emit the items of an arrayFromEnumerable
- emit the items of anIEnumerable
FromTask
- emit the value returned by an async taskFromObservable
- convert anIObservable
into anIAsyncEnumerable
Interval
- periodically signal an ever increasing numberJust
- emit a single constant valueMerge
- run multiple sources at once and merge their items into a single async sequenceNever
- the async sequence never produces any items and never terminatesRange
- emit a range of numbersSwitch
- switch between inner async sources produced by an outer async sequenceTimer
- emit zero after some time delayUsing
- use a resource for the duration of a generated actualIAsyncEnumerable
Zip
- combine the next values of multiple sources via a function and emit its results
Any
- signals true if any of the source items matched a predicateAll
- signals true if all of the source items matched a predicateBuffer
- collect some number of items into buffer(s) and emit those buffersCollect
- collect items into a custom collection and emit the collection at the endConcatMap
- concatenate in order the inner async sequences mapped from the main sequenceConcatMapEager
- run the async sources at once but relay elements in order similar toConcatMap
ConcatWith
- concatenate in order with another async sequenceCount
- count the number of items in the async sequenceDistinct
- makes sure only distinct elements get relayedDistinctUntilChanged
- relays an element only if it is distinct from the previous itemDebounce
- wait a bit after each item and emit them if no newer item arrived from the sourceDefaultIfEmpty
- return a fallback value if the source async sequence turns out to be emptyDoOnNext
- execute an action when an item becomes availableDoOnDispose
- execute an action when the async sequence gets disposed.ElementAt
- get the element at a specified index or an error/default valueFilter
- prevent items from passing through which don't pass a predicateFirst
- signals the first item of the async sequenceFlatMap
- map the source items intoIAsyncEnumerable
s and merge their values into a single async sequenceGroupBy
- groups the source elements into distinct async groupsIgnoreElements
- ignores items and ends when the source async sequence endsIsEmpty
- signals a single true if the source is emptyLast
- signals the last item of the async sequenceLatest
- runs the source async sequence as fast as it can and samples it with the frequency of the consumerMap
- transform one source value into some other valueMergeWith
- run two async sources at once and merge their items into a single async sequenceOnErrorResumeNext
- if the main source fails, switch to an alternative sourcePrefetch
- run the source async sequence to prefetch items for a slow consumerPublish
- consume an async sequence once while multicasting its items to intermediate consumers for the duration of a function.Reduce
- combine elements with an accumulator and emit the last resultRepeat
- repeatedly consume the entire source async sequence (up to a number of times and/or condition)Replay
- consume an async sequence once, caching some or all of its items and multicasting them to intermediate consumers for the duration of a function.Retry
- retry a failed async sequence (up to a number of times or based on condition)Sample
- periodically take the latest item from the source sequence and emit itScan
- perform rolling aggregation by emitting intermediate resultsSingle
- signals the only item of the async sequence, fails if the sequence has more than one itemSkip
- skip the first specified number of items of the source async sequenceSkipLast
- skip the last number of elementsSkipUntil
- skip until another async sequence signals an item or completesSkipWhile
- skip items while the predicate returns true, start emitting when it turns falseSwitchIfEmpty
- switch to an alternate async sequence if the main sequence turns out to be emptySwitchMap
- switch to a newer mapped-in async sequence, disposing the old one, whenever the source produces an itemTake
- take at most a given number of items and stop the async sequence after thatTakeLast
- take the last given number of items of the source async sequence and emit thoseTakeUntil
- take items from the main source until a secondary async sequence signals an item or completesTakeWhile
- take items while predicate is true and stop when it turns falseTimeout
- signal an error if the next item doesn't arrive within the specified timeToList
- collects all items into a List and signals it as the single result of the async sequenceWithLatestFrom
- combines the elements of the main sequence with the latest value(s) from other sequence(s)
Consume
- consume the async sequence via a awaitable push interface ofIAsyncConsumer
FirstAsync
- get the very first value of the async sequenceForEach
- invoke callbacks for each item and for the terminal signalsLastAsync
- get the very last value of the sequenceSingleAsync
- get the only value of the sequence or signal errorToArrayAsync
- get all items as an arrayToEnumerable
- convert theIAsyncEnumerable
into a blockingIEnumerable
ToListAsync
- get all items in an IListToObservable
- convert theIAsyncEnumerable
into anIObservable
MulticastAsyncEnumerable
- signals events to currently associated IAsyncEnumerator consumers (aka PublishSubject).ReplayAsyncEnumerable
- replays some or all items to its IAsyncEnumerator consumers (aka ReplaySubject).UnicastAsyncEnumerable
- buffers then replay items for an only consumer
TestTaskRunner
- a class that creates tasks (of value, error or cancellation) that signal when a virtual time is moved forward (aka TestScheduler)
Represents a push-like consumer where items, an error and/or completion can be signaled and awaited:
public interface IAsyncConsumer<in T>
{
ValueTask Next(T item);
ValueTask Error(Exception error);
ValueTask Complete();
}
The methods must be awaited and called non-concurrently and non-overlappingly with themselves and each other:
Next* (Error | Complete)?