Пример #1
0
        public void OnCompleted(Action continuation)
        {
            var cancel = default(IDisposable);

            if (_token.CanBeCanceled)
            {
                cancel = _token.Register(() =>
                {
                    Interlocked.Exchange(ref continuation, null)?.Invoke();
                });
            }

            try
            {
                _task.OnCompleted(() =>
                {
                    void Invoke()
                    {
                        cancel?.Dispose();

                        Interlocked.Exchange(ref continuation, null)?.Invoke();
                    }

                    if (_scheduler != null)
                    {
                        var t = _scheduler.ExecuteAsync(ct =>
                        {
                            Invoke();

                            return(Task.CompletedTask);
                        }, _token);
                    }
                    else
                    {
                        Invoke();
                    }
                });
            }
            catch
            {
                cancel?.Dispose();
                throw;
            }
        }
Пример #2
0
            public void OnCompleted(Action continuation)
            {
                var t = _scheduler.ExecuteAsync(ct =>
                {
                    try
                    {
                        continuation();
                    }
                    catch (Exception ex)
                    {
                        _error = ExceptionDispatchInfo.Capture(ex);
                    }
                    finally
                    {
                        _done = true;
                    }

                    return(Task.CompletedTask);
                }, _token);
            }
Пример #3
0
        public static IAsyncObservable <TSource> ToAsyncObservable <TSource>(this IObservable <TSource> source, IAsyncScheduler subscribeScheduler, IAsyncScheduler disposeScheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (subscribeScheduler == null)
            {
                throw new ArgumentNullException(nameof(subscribeScheduler));
            }
            if (disposeScheduler == null)
            {
                throw new ArgumentNullException(nameof(disposeScheduler));
            }

            return(Create <TSource>(async observer =>
            {
                var d = new CompositeAsyncDisposable();

                var subscribeTask = await subscribeScheduler.ScheduleAsync(async ct =>
                {
                    ct.ThrowIfCancellationRequested();

                    var disposable = source.Subscribe(AsyncObserver.ToObserver(observer));

                    var disposeTask = AsyncDisposable.Create(() => disposeScheduler.ExecuteAsync(_ =>
                    {
                        disposable.Dispose();
                        return Task.CompletedTask;
                    }));

                    await d.AddAsync(disposeTask).RendezVous(subscribeScheduler);
                }).ConfigureAwait(false);

                await d.AddAsync(subscribeTask).ConfigureAwait(false);

                return d;
            }));
        }