TaskObservable.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.CompilerServices;
  5. namespace System.Reactive
  6. {
  7. /// <summary>
  8. /// Extension of the <see cref="IObservable{T}"/> interface compatible with async method return types.
  9. /// </summary>
  10. /// <remarks>
  11. /// This class implements a "task-like" type that can be used as the return type of an asynchronous
  12. /// method in C# 7.0 and beyond. For example:
  13. /// <code>
  14. /// async ITaskObservable&lt;int&gt; RxAsync()
  15. /// {
  16. /// var res = await Observable.Return(21).Delay(TimeSpan.FromSeconds(1));
  17. /// return res * 2;
  18. /// }
  19. /// </code>
  20. /// </remarks>
  21. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  22. [AsyncMethodBuilder(typeof(TaskObservableMethodBuilder<>))]
  23. public interface ITaskObservable<out T> : IObservable<T>
  24. {
  25. // NB: An interface type is preferred to enable the use of covariance.
  26. /// <summary>
  27. /// Gets an awaiter that can be used to await the eventual completion of the observable sequence.
  28. /// </summary>
  29. /// <returns>An awaiter that can be used to await the eventual completion of the observable sequence.</returns>
  30. ITaskObservableAwaiter<T> GetAwaiter();
  31. }
  32. /// <summary>
  33. /// Interface representing an awaiter for an <see cref="ITaskObservable{T}"/>.
  34. /// </summary>
  35. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  36. public interface ITaskObservableAwaiter<out T> : INotifyCompletion
  37. {
  38. /// <summary>
  39. /// Gets a Boolean indicating whether the observable sequence has completed.
  40. /// </summary>
  41. bool IsCompleted { get; }
  42. /// <summary>
  43. /// Gets the result produced by the observable sequence.
  44. /// </summary>
  45. /// <returns>The result produced by the observable sequence.</returns>
  46. T GetResult();
  47. }
  48. }