public static async ValueTask SubscribeAsync <T>(this IAsyncObservable <T> source, Action <T> onNext = null, Action <Exception> onError = null, Action onCompleted = null, CancellationToken token = default) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var observer = new AnonymousAsyncObserver <T> .Sync(onNext); try { await source.SubscribeAsync(observer, token); onCompleted?.Invoke(); } catch (Exception ex) when(onError != null) { onError(ex); } }
public static async ValueTask SubscribeAsync <T>(this IAsyncObservable <T> source, Func <T, ValueTask> onNext, Func <Exception, ValueTask> onError = null, Func <ValueTask> onCompleted = null, CancellationToken token = default) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var observer = new AnonymousAsyncObserver <T> .Async(onNext); try { await source.SubscribeAsync(observer, token); if (onCompleted != null) { await onCompleted(); } } catch (Exception ex) when(onError != null) { await onError(ex); } }