// 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.Reactive.Linq; using System.Reactive.Linq.ObservableImpl; using System.Reactive.Subjects; using System.Threading; using System.Threading.Tasks; namespace System.Reactive.Threading.Tasks { /// /// Provides a set of static methods for converting tasks to observable sequences. /// public static class TaskObservableExtensions { private sealed class SlowTaskObservable : IObservable { private readonly Task _task; private readonly IScheduler _scheduler; public SlowTaskObservable(Task task, IScheduler scheduler) { _task = task; _scheduler = scheduler; } public IDisposable Subscribe(IObserver observer) { if (observer == null) { throw new ArgumentNullException(nameof(observer)); } var cts = new CancellationDisposable(); var options = GetTaskContinuationOptions(_scheduler); if (_scheduler == null) _task.ContinueWith((t, subjectObject) => t.EmitTaskResult((IObserver)subjectObject), observer, cts.Token, options, TaskScheduler.Current); else { _task.ContinueWithState( (task, tuple) => tuple.@this._scheduler.ScheduleAction( (task, tuple.observer), tuple2 => tuple2.task.EmitTaskResult(tuple2.observer)), (@this: this, observer), cts.Token, options); } return cts; } } private sealed class SlowTaskObservable : IObservable { private readonly Task _task; private readonly IScheduler _scheduler; public SlowTaskObservable(Task task, IScheduler scheduler) { _task = task; _scheduler = scheduler; } public IDisposable Subscribe(IObserver observer) { if (observer == null) { throw new ArgumentNullException(nameof(observer)); } var cts = new CancellationDisposable(); var options = GetTaskContinuationOptions(_scheduler); if (_scheduler == null) _task.ContinueWith((t, subjectObject) => t.EmitTaskResult((IObserver)subjectObject), observer, cts.Token, options, TaskScheduler.Current); else { _task.ContinueWithState( (task, tuple) => tuple.@this._scheduler.ScheduleAction( (task, tuple.observer), tuple2 => tuple2.task.EmitTaskResult(tuple2.observer)), (@this: this, observer), cts.Token, options); } return cts; } } /// /// Returns an observable sequence that signals when the task completes. /// /// Task to convert to an observable sequence. /// An observable sequence that produces a unit value when the task completes, or propagates the exception produced by the task. /// is null. /// If the specified task object supports cancellation, consider using instead. public static IObservable ToObservable(this Task task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } return ToObservableImpl(task, scheduler: null); } /// /// Returns an observable sequence that signals when the task completes. /// /// Task to convert to an observable sequence. /// Scheduler on which to notify observers about completion, cancellation or failure. /// An observable sequence that produces a unit value when the task completes, or propagates the exception produced by the task. /// is null or is null. /// If the specified task object supports cancellation, consider using instead. public static IObservable ToObservable(this Task task, IScheduler scheduler) { if (task == null) { throw new ArgumentNullException(nameof(task)); } if (scheduler == null) { throw new ArgumentNullException(nameof(scheduler)); } return ToObservableImpl(task, scheduler); } private static IObservable ToObservableImpl(Task task, IScheduler scheduler) { if (task.IsCompleted) { scheduler = scheduler ?? ImmediateScheduler.Instance; switch (task.Status) { case TaskStatus.Faulted: return new Throw(task.Exception.InnerException, scheduler); case TaskStatus.Canceled: return new Throw(new TaskCanceledException(task), scheduler); } return new Return(Unit.Default, scheduler); } return new SlowTaskObservable(task, scheduler); } private static void EmitTaskResult(this Task task, IObserver subject) { switch (task.Status) { case TaskStatus.RanToCompletion: subject.OnNext(Unit.Default); subject.OnCompleted(); break; case TaskStatus.Faulted: subject.OnError(task.Exception.InnerException); break; case TaskStatus.Canceled: subject.OnError(new TaskCanceledException(task)); break; } } internal static IDisposable Subscribe(this Task task, IObserver observer) { if (task.IsCompleted) { task.EmitTaskResult(observer); return Disposable.Empty; } var cts = new CancellationDisposable(); task.ContinueWith( (t, observerObject) => t.EmitTaskResult((IObserver)observerObject), observer, cts.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); return cts; } /// /// Returns an observable sequence that propagates the result of the task. /// /// The type of the result produced by the task. /// Task to convert to an observable sequence. /// An observable sequence that produces the task's result, or propagates the exception produced by the task. /// is null. /// If the specified task object supports cancellation, consider using instead. public static IObservable ToObservable(this Task task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } return ToObservableImpl(task, scheduler: null); } /// /// Returns an observable sequence that propagates the result of the task. /// /// The type of the result produced by the task. /// Task to convert to an observable sequence. /// Scheduler on which to notify observers about completion, cancellation or failure. /// An observable sequence that produces the task's result, or propagates the exception produced by the task. /// is null or is null. /// If the specified task object supports cancellation, consider using instead. public static IObservable ToObservable(this Task task, IScheduler scheduler) { if (task == null) { throw new ArgumentNullException(nameof(task)); } if (scheduler == null) { throw new ArgumentNullException(nameof(scheduler)); } return ToObservableImpl(task, scheduler); } private static IObservable ToObservableImpl(Task task, IScheduler scheduler) { if (task.IsCompleted) { scheduler = scheduler ?? ImmediateScheduler.Instance; switch (task.Status) { case TaskStatus.Faulted: return new Throw(task.Exception.InnerException, scheduler); case TaskStatus.Canceled: return new Throw(new TaskCanceledException(task), scheduler); } return new Return(task.Result, scheduler); } return new SlowTaskObservable(task, scheduler); } private static void EmitTaskResult(this Task task, IObserver subject) { switch (task.Status) { case TaskStatus.RanToCompletion: subject.OnNext(task.Result); subject.OnCompleted(); break; case TaskStatus.Faulted: subject.OnError(task.Exception.InnerException); break; case TaskStatus.Canceled: subject.OnError(new TaskCanceledException(task)); break; } } private static TaskContinuationOptions GetTaskContinuationOptions(IScheduler scheduler) { var options = TaskContinuationOptions.None; if (scheduler != null) { // // We explicitly don't special-case the immediate scheduler here. If the user asks for a // synchronous completion, we'll try our best. However, there's no guarantee due to the // internal stack probing in the TPL, which may cause asynchronous completion on a thread // pool thread in order to avoid stack overflows. Therefore we can only attempt to be more // efficient in the case where the user specified a scheduler, hence we know that the // continuation will trigger a scheduling operation. In case of the immediate scheduler, // it really becomes "immediate scheduling" wherever the TPL decided to run the continuation, // i.e. not necessarily where the task was completed from. // options |= TaskContinuationOptions.ExecuteSynchronously; } return options; } internal static IDisposable Subscribe(this Task task, IObserver observer) { if (task.IsCompleted) { task.EmitTaskResult(observer); return Disposable.Empty; } var cts = new CancellationDisposable(); task.ContinueWith( (t, observerObject) => t.EmitTaskResult((IObserver)observerObject), observer, cts.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); return cts; } /// /// Returns a task that will receive the last value or the exception produced by the observable sequence. /// /// The type of the elements in the source sequence. /// Observable sequence to convert to a task. /// A task that will receive the last element or the exception produced by the observable sequence. /// is null. public static Task ToTask(this IObservable observable) { if (observable == null) { throw new ArgumentNullException(nameof(observable)); } return observable.ToTask(new CancellationToken(), state: null); } /// /// Returns a task that will receive the last value or the exception produced by the observable sequence. /// /// The type of the elements in the source sequence. /// Observable sequence to convert to a task. /// The state to use as the underlying task's AsyncState. /// A task that will receive the last element or the exception produced by the observable sequence. /// is null. public static Task ToTask(this IObservable observable, object state) { if (observable == null) { throw new ArgumentNullException(nameof(observable)); } return observable.ToTask(new CancellationToken(), state); } /// /// Returns a task that will receive the last value or the exception produced by the observable sequence. /// /// The type of the elements in the source sequence. /// Observable sequence to convert to a task. /// Cancellation token that can be used to cancel the task, causing unsubscription from the observable sequence. /// A task that will receive the last element or the exception produced by the observable sequence. /// is null. public static Task ToTask(this IObservable observable, CancellationToken cancellationToken) { if (observable == null) { throw new ArgumentNullException(nameof(observable)); } return observable.ToTask(cancellationToken, state: null); } private sealed class ToTaskObserver : SafeObserver { private readonly CancellationToken _ct; private readonly TaskCompletionSource _tcs; private readonly CancellationTokenRegistration _ctr; private bool _hasValue; private TResult _lastValue; public ToTaskObserver(TaskCompletionSource tcs, CancellationToken ct) { _ct = ct; _tcs = tcs; if (ct.CanBeCanceled) { _ctr = ct.Register(@this => ((ToTaskObserver)@this).Cancel(), this); } } public override void OnNext(TResult value) { _hasValue = true; _lastValue = value; } public override void OnError(Exception error) { _tcs.TrySetException(error); _ctr.Dispose(); // no null-check needed (struct) Dispose(); } public override void OnCompleted() { if (_hasValue) { _tcs.TrySetResult(_lastValue); } else { _tcs.TrySetException(new InvalidOperationException(Strings_Linq.NO_ELEMENTS)); } _ctr.Dispose(); // no null-check needed (struct) Dispose(); } private void Cancel() { Dispose(); #if HAS_TPL46 _tcs.TrySetCanceled(_ct); #else _tcs.TrySetCanceled(); #endif } } /// /// Returns a task that will receive the last value or the exception produced by the observable sequence. /// /// The type of the elements in the source sequence. /// Observable sequence to convert to a task. /// Cancellation token that can be used to cancel the task, causing unsubscription from the observable sequence. /// The state to use as the underlying task's . /// A task that will receive the last element or the exception produced by the observable sequence. /// is null. public static Task ToTask(this IObservable observable, CancellationToken cancellationToken, object state) { if (observable == null) { throw new ArgumentNullException(nameof(observable)); } var tcs = new TaskCompletionSource(state); var taskCompletionObserver = new ToTaskObserver(tcs, cancellationToken); // // Subtle race condition: if the source completes before we reach the line below, the SingleAssigmentDisposable // will already have been disposed. Upon assignment, the disposable resource being set will be disposed on the // spot, which may throw an exception. // try { // // [OK] Use of unsafe Subscribe: we're catching the exception here to set the TaskCompletionSource. // // Notice we could use a safe subscription to route errors through OnError, but we still need the // exception handling logic here for the reason explained above. We cannot afford to throw here // and as a result never set the TaskCompletionSource, so we tunnel everything through here. // taskCompletionObserver.SetResource(observable.Subscribe/*Unsafe*/(taskCompletionObserver)); } catch (Exception ex) { tcs.TrySetException(ex); } return tcs.Task; } } }