123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Threading.Tasks;
- namespace System.Reactive.Linq
- {
- public partial class AsyncObservable
- {
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, IObserver<TSource> observer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- return Create(
- source,
- observer,
- static (source, observer, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, observer)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- return Create(
- source,
- onNext,
- static (source, onNext, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<Exception> onError)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- return Create(
- source,
- onError,
- static (source, onError, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onError)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Create(
- source,
- onCompleted,
- static (source, onCompleted, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onCompleted)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Create(
- source,
- (onNext, onError, onCompleted),
- static (source, state, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, state.onNext, state.onError, state.onCompleted)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, IAsyncObserver<TSource> observer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- return Create(
- source,
- observer,
- static (source, observer, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, observer)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask> onNext)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- return Create(
- source,
- onNext,
- static (source, onNext, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<Exception, ValueTask> onError)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- return Create(
- source,
- onError,
- static (source, onError, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onError)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<ValueTask> onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Create(
- source,
- onCompleted,
- static (source, onCompleted, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onCompleted)));
- }
- public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask> onNext, Func<Exception, ValueTask> onError, Func<ValueTask> onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Create(
- source,
- (onNext, onError, onCompleted),
- static (source, state, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, state.onNext, state.onError, state.onCompleted)));
- }
- }
- public partial class AsyncObserver
- {
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, IAsyncObserver<TSource> witness)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (witness == null)
- throw new ArgumentNullException(nameof(witness));
- return Create<TSource>(
- async x =>
- {
- try
- {
- await witness.OnNextAsync(x).ConfigureAwait(false);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnNextAsync(x).ConfigureAwait(false);
- },
- async error =>
- {
- try
- {
- await witness.OnErrorAsync(error).ConfigureAwait(false);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnErrorAsync(error).ConfigureAwait(false);
- },
- async () =>
- {
- try
- {
- await witness.OnCompletedAsync().ConfigureAwait(false);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask> onNext)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- return Do(observer, Create(onNext));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<Exception, ValueTask> onError)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- return Do(observer, Create<TSource>(_ => default, onError, () => default));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<ValueTask> onCompleted)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Do(observer, Create<TSource>(_ => default, _ => default, onCompleted));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask> onNext, Func<Exception, ValueTask> onError, Func<ValueTask> onCompleted)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Do(observer, Create(onNext, onError, onCompleted));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, IObserver<TSource> witness)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (witness == null)
- throw new ArgumentNullException(nameof(witness));
- return Create<TSource>(
- async x =>
- {
- try
- {
- witness.OnNext(x);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnNextAsync(x).ConfigureAwait(false);
- },
- async error =>
- {
- try
- {
- witness.OnError(error);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnErrorAsync(error).ConfigureAwait(false);
- },
- async () =>
- {
- try
- {
- witness.OnCompleted();
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Action<TSource> onNext)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- return Do(observer, x => { onNext(x); return default; });
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Action<Exception> onError)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- return Do(observer, Create<TSource>(_ => default, ex => { onError(ex); return default; }, () => default));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Action onCompleted)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Do(observer, Create<TSource>(_ => default, _ => default, () => { onCompleted(); return default; }));
- }
- public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return Do(observer, x => { onNext(x); return default; }, ex => { onError(ex); return default; }, () => { onCompleted(); return default; });
- }
- }
- }
|