| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the Apache 2.0 License.// See the LICENSE file in the project root for more information. using System.Reactive.Concurrency;using System.Reactive.Disposables;using System.Threading;namespace System.Reactive.Linq{    partial class AsyncObservable    {        public static IAsyncObservable<TSource> Throttle<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (dueTime < TimeSpan.Zero)                throw new ArgumentOutOfRangeException(nameof(dueTime));            return Create<TSource>(async observer =>            {                var d = new CompositeAsyncDisposable();                var (sink, throttler) = AsyncObserver.Throttle(observer, dueTime);                await d.AddAsync(throttler).ConfigureAwait(false);                var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);                await d.AddAsync(sourceSubscription).ConfigureAwait(false);                return d;            });        }        public static IAsyncObservable<TSource> Throttle<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncScheduler scheduler)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (dueTime < TimeSpan.Zero)                throw new ArgumentOutOfRangeException(nameof(dueTime));            if (scheduler == null)                throw new ArgumentNullException(nameof(scheduler));            return Create<TSource>(async observer =>            {                var d = new CompositeAsyncDisposable();                var (sink, throttler) = AsyncObserver.Throttle(observer, dueTime, scheduler);                await d.AddAsync(throttler).ConfigureAwait(false);                var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);                await d.AddAsync(sourceSubscription).ConfigureAwait(false);                return d;            });        }        public static IAsyncObservable<TSource> Throttle<TSource, TThrottle>(this IAsyncObservable<TSource> source, Func<TSource, IAsyncObservable<TThrottle>> throttleSelector)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (throttleSelector == null)                throw new ArgumentNullException(nameof(throttleSelector));            return Create<TSource>(async observer =>            {                var d = new CompositeAsyncDisposable();                var (sink, throttler) = AsyncObserver.Throttle(observer, throttleSelector);                await d.AddAsync(throttler).ConfigureAwait(false);                var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);                await d.AddAsync(sourceSubscription).ConfigureAwait(false);                return d;            });        }    }    partial class AsyncObserver    {        public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource>(IAsyncObserver<TSource> observer, TimeSpan dueTime) => Throttle(observer, dueTime, TaskPoolAsyncScheduler.Default);        public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource>(IAsyncObserver<TSource> observer, TimeSpan dueTime, IAsyncScheduler scheduler)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (dueTime < TimeSpan.Zero)                throw new ArgumentOutOfRangeException(nameof(dueTime));            if (scheduler == null)                throw new ArgumentNullException(nameof(scheduler));            var gate = new AsyncLock();            var timer = new SerialAsyncDisposable();            var hasValue = false;            var value = default(TSource);            var id = 0UL;            return                (                    Create<TSource>(                        async x =>                        {                            var myId = default(ulong);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                hasValue = true;                                value = x;                                myId = ++id;                            }                            var d = new SingleAssignmentAsyncDisposable();                            await timer.AssignAsync(d).ConfigureAwait(false);                            var t = await scheduler.ScheduleAsync(async ct =>                            {                                if (!ct.IsCancellationRequested)                                {                                    using (await gate.LockAsync().ConfigureAwait(false))                                    {                                        if (hasValue && id == myId)                                        {                                            await observer.OnNextAsync(value).ConfigureAwait(false);                                        }                                        hasValue = false;                                    }                                }                            }).ConfigureAwait(false);                            await d.AssignAsync(t).ConfigureAwait(false);                        },                        async ex =>                        {                            await timer.DisposeAsync().ConfigureAwait(false);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                await observer.OnErrorAsync(ex).ConfigureAwait(false);                                hasValue = false;                                id++;                            }                        },                        async () =>                        {                            await timer.DisposeAsync().ConfigureAwait(false);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                if (hasValue)                                {                                    await observer.OnNextAsync(value).ConfigureAwait(false);                                }                                await observer.OnCompletedAsync().ConfigureAwait(false);                                hasValue = false;                                id++;                            }                        }                    ),                    timer                );        }        public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource, TThrottle>(IAsyncObserver<TSource> observer, Func<TSource, IAsyncObservable<TThrottle>> throttleSelector)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (throttleSelector == null)                throw new ArgumentNullException(nameof(throttleSelector));            var gate = new AsyncLock();            var throttler = new SerialAsyncDisposable();            var hasValue = false;            var value = default(TSource);            var id = 0UL;            return                (                    Create<TSource>(                        async x =>                        {                            var throttleSource = default(IAsyncObservable<TThrottle>);                            try                            {                                throttleSource = throttleSelector(x); // REVIEW: Do we need an async variant?                            }                            catch (Exception ex)                            {                                using (await gate.LockAsync().ConfigureAwait(false))                                {                                    await observer.OnErrorAsync(ex).ConfigureAwait(false);                                }                                return;                            }                            var myId = default(ulong);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                hasValue = true;                                value = x;                                myId = ++id;                            }                            var d = new SingleAssignmentAsyncDisposable();                            await throttler.AssignAsync(d).ConfigureAwait(false);                            var throttleObserver = Create<TThrottle>(                                    async y =>                                    {                                        using (await gate.LockAsync().ConfigureAwait(false))                                        {                                            if (hasValue && myId == id)                                            {                                                await observer.OnNextAsync(x).ConfigureAwait(false);                                            }                                            hasValue = false;                                            await d.DisposeAsync().ConfigureAwait(false);                                        }                                    },                                    async ex =>                                    {                                        using (await gate.LockAsync().ConfigureAwait(false))                                        {                                            await observer.OnErrorAsync(ex).ConfigureAwait(false);                                        }                                    },                                    async () =>                                    {                                        using (await gate.LockAsync().ConfigureAwait(false))                                        {                                            if (hasValue && myId == id)                                            {                                                await observer.OnNextAsync(x).ConfigureAwait(false);                                            }                                            hasValue = false;                                            await d.DisposeAsync().ConfigureAwait(false);                                        }                                    }                                );                            var t = await throttleSource.SubscribeSafeAsync(throttleObserver).ConfigureAwait(false);                            await d.AssignAsync(t).ConfigureAwait(false);                        },                        async ex =>                        {                            await throttler.DisposeAsync().ConfigureAwait(false);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                await observer.OnErrorAsync(ex).ConfigureAwait(false);                                hasValue = false;                                id++;                            }                        },                        async () =>                        {                            await throttler.DisposeAsync().ConfigureAwait(false);                            using (await gate.LockAsync().ConfigureAwait(false))                            {                                if (hasValue)                                {                                    await observer.OnNextAsync(value).ConfigureAwait(false);                                }                                await observer.OnCompletedAsync().ConfigureAwait(false);                                hasValue = false;                                id++;                            }                        }                    ),                    throttler                );        }    }}
 |