| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
using System; |
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Linq; |
| 5 |
using System.Threading.Tasks; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace System.Linq |
| 9 |
{ |
| 10 |
public static partial class AsyncEnumerable |
| 11 |
{ |
| 12 |
public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumerable<TSource> source) |
| 13 |
{ |
| 14 |
if (source == null) |
| 15 |
throw new ArgumentNullException("source"); |
| 16 |
|
| 17 |
return Create(() => |
| 18 |
{ |
| 19 |
var e = source.GetEnumerator(); |
| 20 |
|
| 21 |
return Create( |
| 22 |
ct => Task.Factory.StartNew(() => |
| 23 |
{ |
| 24 |
var res = default(bool); |
| 25 |
try |
| 26 |
{ |
| 27 |
res = e.MoveNext(); |
| 28 |
} |
| 29 |
finally |
| 30 |
{ |
| 31 |
if (!res) |
| 32 |
e.Dispose(); |
| 33 |
} |
| 34 |
return res; |
| 35 |
}, ct), |
| 36 |
() => e.Current, |
| 37 |
() => e.Dispose() |
| 38 |
); |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source) |
| 43 |
{ |
| 44 |
if (source == null) |
| 45 |
throw new ArgumentNullException("source"); |
| 46 |
|
| 47 |
return ToEnumerable_(source); |
| 48 |
} |
| 49 |
|
| 50 |
private static IEnumerable<TSource> ToEnumerable_<TSource>(IAsyncEnumerable<TSource> source) |
| 51 |
{ |
| 52 |
using (var e = source.GetEnumerator()) |
| 53 |
{ |
| 54 |
while (true) |
| 55 |
{ |
| 56 |
var t = e.MoveNext(CancellationToken.None); |
| 57 |
t.Wait(); |
| 58 |
if (!t.Result) |
| 59 |
break; |
| 60 |
var c = e.Current; |
| 61 |
yield return c; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#if !NO_RXINTERFACES |
| 67 |
public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source) |
| 68 |
{ |
| 69 |
if (source == null) |
| 70 |
throw new ArgumentNullException("source"); |
| 71 |
|
| 72 |
return Create(() => |
| 73 |
{ |
| 74 |
var observer = new ToAsyncEnumerableObserver<TSource>(); |
| 75 |
observer.Queue = new Queue<Either<TSource, Exception, bool>>(); |
| 76 |
|
| 77 |
var subscription = source.Subscribe(observer); |
| 78 |
|
| 79 |
return Create( |
| 80 |
(ct, tcs) => |
| 81 |
{ |
| 82 |
lock (observer.Queue) |
| 83 |
{ |
| 84 |
if (observer.Queue.Count > 0) |
| 85 |
{ |
| 86 |
var n = observer.Queue.Dequeue(); |
| 87 |
n.Switch( |
| 88 |
x => |
| 89 |
{ |
| 90 |
observer.Current = x; |
| 91 |
tcs.TrySetResult(true); |
| 92 |
}, |
| 93 |
ex => |
| 94 |
{ |
| 95 |
tcs.TrySetException(ex); |
| 96 |
}, |
| 97 |
_ => |
| 98 |
{ |
| 99 |
tcs.TrySetResult(false); |
| 100 |
} |
| 101 |
); |
| 102 |
} |
| 103 |
else |
| 104 |
observer.TaskCompletionSource = tcs; |
| 105 |
} |
| 106 |
|
| 107 |
return tcs.Task; |
| 108 |
}, |
| 109 |
() => observer.Current, |
| 110 |
() => |
| 111 |
{ |
| 112 |
subscription.Dispose(); |
| 113 |
// Should we cancel in-flight operations somehow? |
| 114 |
}); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
class ToAsyncEnumerableObserver<T> : IObserver<T> |
| 119 |
{ |
| 120 |
public Queue<Either<T, Exception, bool>> Queue { get; set; } |
| 121 |
public T Current { get; set; } |
| 122 |
public TaskCompletionSource<bool> TaskCompletionSource { get; set; } |
| 123 |
|
| 124 |
public void OnCompleted() |
| 125 |
{ |
| 126 |
lock (Queue) |
| 127 |
{ |
| 128 |
if (TaskCompletionSource == null) |
| 129 |
Queue.Enqueue(new Either<T, Exception, bool>.Choice3(true)); |
| 130 |
else |
| 131 |
{ |
| 132 |
TaskCompletionSource.SetResult(false); |
| 133 |
TaskCompletionSource = null; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void OnError(Exception error) |
| 139 |
{ |
| 140 |
lock (Queue) |
| 141 |
{ |
| 142 |
if (TaskCompletionSource == null) |
| 143 |
Queue.Enqueue(new Either<T, Exception, bool>.Choice2(error)); |
| 144 |
else |
| 145 |
{ |
| 146 |
TaskCompletionSource.SetException(error); |
| 147 |
TaskCompletionSource = null; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public void OnNext(T value) |
| 153 |
{ |
| 154 |
lock (Queue) |
| 155 |
{ |
| 156 |
if (TaskCompletionSource == null) |
| 157 |
Queue.Enqueue(new Either<T, Exception, bool>.Choice1(value)); |
| 158 |
else |
| 159 |
{ |
| 160 |
Current = value; |
| 161 |
TaskCompletionSource.SetResult(true); |
| 162 |
TaskCompletionSource = null; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
abstract class Either<T, U, V> |
| 169 |
{ |
| 170 |
public abstract void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3); |
| 171 |
|
| 172 |
public class Choice1 : Either<T, U, V> |
| 173 |
{ |
| 174 |
public Choice1(T value) { Value = value; } |
| 175 |
public T Value { get; private set; } |
| 176 |
|
| 177 |
public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3) |
| 178 |
{ |
| 179 |
choice1(Value); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public class Choice2 : Either<T, U, V> |
| 184 |
{ |
| 185 |
public Choice2(U value) { Value = value; } |
| 186 |
public U Value { get; private set; } |
| 187 |
|
| 188 |
public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3) |
| 189 |
{ |
| 190 |
choice2(Value); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public class Choice3 : Either<T, U, V> |
| 195 |
{ |
| 196 |
public Choice3(V value) { Value = value; } |
| 197 |
public V Value { get; private set; } |
| 198 |
|
| 199 |
public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3) |
| 200 |
{ |
| 201 |
choice3(Value); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
public static IObservable<TSource> ToObservable<TSource>(this IAsyncEnumerable<TSource> source) |
| 207 |
{ |
| 208 |
if (source == null) |
| 209 |
throw new ArgumentNullException("source"); |
| 210 |
|
| 211 |
return new ToObservableObservable<TSource>(source); |
| 212 |
} |
| 213 |
|
| 214 |
class ToObservableObservable<T> : IObservable<T> |
| 215 |
{ |
| 216 |
private readonly IAsyncEnumerable<T> source; |
| 217 |
|
| 218 |
public ToObservableObservable(IAsyncEnumerable<T> source) |
| 219 |
{ |
| 220 |
this.source = source; |
| 221 |
} |
| 222 |
|
| 223 |
public IDisposable Subscribe(IObserver<T> observer) |
| 224 |
{ |
| 225 |
var ctd = new CancellationTokenDisposable(); |
| 226 |
var e = source.GetEnumerator(); |
| 227 |
|
| 228 |
var f = default(Action); |
| 229 |
f = () => e.MoveNext(ctd.Token).ContinueWith(t => |
| 230 |
{ |
| 231 |
if (t.IsFaulted) |
| 232 |
{ |
| 233 |
observer.OnError(t.Exception); |
| 234 |
e.Dispose(); |
| 235 |
} |
| 236 |
else if (t.IsCanceled) |
| 237 |
{ |
| 238 |
e.Dispose(); |
| 239 |
} |
| 240 |
else if (t.IsCompleted) |
| 241 |
{ |
| 242 |
if (t.Result) |
| 243 |
{ |
| 244 |
observer.OnNext(e.Current); |
| 245 |
f(); |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
observer.OnCompleted(); |
| 250 |
e.Dispose(); |
| 251 |
} |
| 252 |
} |
| 253 |
}, ctd.Token); |
| 254 |
|
| 255 |
f(); |
| 256 |
|
| 257 |
return new CompositeDisposable(ctd, e); |
| 258 |
} |
| 259 |
} |
| 260 |
#endif |
| 261 |
} |
| 262 |
} |