Switch.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Reactive.Disposables;
  5. using System.Threading;
  6. namespace System.Reactive.Linq
  7. {
  8. public partial class AsyncObservable
  9. {
  10. public static IAsyncObservable<TSource> Switch<TSource>(this IAsyncObservable<IAsyncObservable<TSource>> source)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. return Create<IAsyncObservable<TSource>, TSource>(
  15. source,
  16. async static (source, observer) =>
  17. {
  18. var (sink, cancel) = AsyncObserver.Switch(observer);
  19. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  20. return StableCompositeAsyncDisposable.Create(subscription, cancel);
  21. });
  22. }
  23. }
  24. public partial class AsyncObserver
  25. {
  26. public static (IAsyncObserver<IAsyncObservable<TSource>>, IAsyncDisposable) Switch<TSource>(IAsyncObserver<TSource> observer)
  27. {
  28. if (observer == null)
  29. throw new ArgumentNullException(nameof(observer));
  30. var gate = new AsyncGate();
  31. var isStopped = false;
  32. var hasLatest = false;
  33. var latest = 0UL;
  34. var disposable = new SerialAsyncDisposable();
  35. return
  36. (
  37. Create<IAsyncObservable<TSource>>(
  38. async xs =>
  39. {
  40. ulong id;
  41. using (await gate.LockAsync().ConfigureAwait(false))
  42. {
  43. hasLatest = true;
  44. id = unchecked(++latest);
  45. }
  46. var innerObserver = Create<TSource>(
  47. async x =>
  48. {
  49. using (await gate.LockAsync().ConfigureAwait(false))
  50. {
  51. if (latest == id)
  52. {
  53. await observer.OnNextAsync(x).ConfigureAwait(false);
  54. }
  55. }
  56. },
  57. async ex =>
  58. {
  59. using (await gate.LockAsync().ConfigureAwait(false))
  60. {
  61. if (latest == id)
  62. {
  63. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  64. }
  65. }
  66. },
  67. async () =>
  68. {
  69. using (await gate.LockAsync().ConfigureAwait(false))
  70. {
  71. if (latest == id)
  72. {
  73. hasLatest = false;
  74. if (isStopped)
  75. {
  76. await observer.OnCompletedAsync().ConfigureAwait(false);
  77. }
  78. }
  79. }
  80. }
  81. );
  82. var inner = await xs.SubscribeSafeAsync(innerObserver).ConfigureAwait(false);
  83. await disposable.AssignAsync(inner).ConfigureAwait(false);
  84. },
  85. async ex =>
  86. {
  87. using (await gate.LockAsync().ConfigureAwait(false))
  88. {
  89. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  90. }
  91. },
  92. async () =>
  93. {
  94. using (await gate.LockAsync().ConfigureAwait(false))
  95. {
  96. isStopped = true;
  97. if (!hasLatest)
  98. {
  99. await observer.OnCompletedAsync().ConfigureAwait(false);
  100. }
  101. }
  102. }
  103. ),
  104. disposable
  105. );
  106. }
  107. }
  108. }