123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Threading;
- using Subscribes = Cysharp.Threading.Tasks.Linq.Subscribe;
- namespace Cysharp.Threading.Tasks.Linq
- {
- public static partial class UniTaskAsyncEnumerable
- {
- // OnNext
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> action)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTaskVoid> action)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> action, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTaskVoid> action, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- Subscribes.SubscribeCore(source, action, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- // OnNext, OnError
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> onNext, Action<Exception> onError)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> onNext, Action<Exception> onError, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- Subscribes.SubscribeCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Action<Exception> onError)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Action<Exception> onError, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Action<Exception> onError)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Action<Exception> onError, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onError, nameof(onError));
- Subscribes.SubscribeAwaitCore(source, onNext, onError, Subscribes.NopCompleted, cancellationToken).Forget();
- }
- // OnNext, OnCompleted
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget();
- return cts;
- }
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> onNext, Action onCompleted)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget();
- return cts;
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget();
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> onNext, Action onCompleted, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- Subscribes.SubscribeCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Action onCompleted)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Action onCompleted, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget();
- }
- public static IDisposable SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Action onCompleted)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cts.Token).Forget();
- return cts;
- }
- public static void SubscribeAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Action onCompleted, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(onNext, nameof(onNext));
- Error.ThrowArgumentNullException(onCompleted, nameof(onCompleted));
- Subscribes.SubscribeAwaitCore(source, onNext, Subscribes.NopError, onCompleted, cancellationToken).Forget();
- }
- // IObserver
- public static IDisposable Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IObserver<TSource> observer)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(observer, nameof(observer));
- var cts = new CancellationTokenDisposable();
- Subscribes.SubscribeCore(source, observer, cts.Token).Forget();
- return cts;
- }
- public static void Subscribe<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IObserver<TSource> observer, CancellationToken cancellationToken)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(observer, nameof(observer));
- Subscribes.SubscribeCore(source, observer, cancellationToken).Forget();
- }
- }
- internal sealed class CancellationTokenDisposable : IDisposable
- {
- readonly CancellationTokenSource cts = new CancellationTokenSource();
- public CancellationToken Token => cts.Token;
- public void Dispose()
- {
- if (!cts.IsCancellationRequested)
- {
- cts.Cancel();
- }
- }
- }
- internal static class Subscribe
- {
- public static readonly Action<Exception> NopError = _ => { };
- public static readonly Action NopCompleted = () => { };
- public static async UniTaskVoid SubscribeCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- onNext(e.Current);
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- onCompleted();
- }
- catch (Exception ex)
- {
- if (onError == NopError)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- return;
- }
- if (ex is OperationCanceledException) return;
- onError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTaskVoid SubscribeCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTaskVoid> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- onNext(e.Current).Forget();
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- onCompleted();
- }
- catch (Exception ex)
- {
- if (onError == NopError)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- return;
- }
- if (ex is OperationCanceledException) return;
- onError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTaskVoid SubscribeCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTaskVoid> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- onNext(e.Current, cancellationToken).Forget();
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- onCompleted();
- }
- catch (Exception ex)
- {
- if (onError == NopError)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- return;
- }
- if (ex is OperationCanceledException) return;
- onError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTaskVoid SubscribeCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, IObserver<TSource> observer, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- observer.OnNext(e.Current);
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- observer.OnCompleted();
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException) return;
- observer.OnError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTaskVoid SubscribeAwaitCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- await onNext(e.Current);
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- onCompleted();
- }
- catch (Exception ex)
- {
- if (onError == NopError)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- return;
- }
- if (ex is OperationCanceledException) return;
- onError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTaskVoid SubscribeAwaitCore<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> onNext, Action<Exception> onError, Action onCompleted, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- try
- {
- await onNext(e.Current, cancellationToken);
- }
- catch (Exception ex)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- }
- }
- onCompleted();
- }
- catch (Exception ex)
- {
- if (onError == NopError)
- {
- UniTaskScheduler.PublishUnobservedTaskException(ex);
- return;
- }
- if (ex is OperationCanceledException) return;
- onError(ex);
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- }
- }
|