| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Linq; |
| 5 |
using System.Reactive.Concurrency; |
| 6 |
using System.Reactive.Disposables; |
| 7 |
using System.Reactive.Subjects; |
| 8 |
|
| 9 |
namespace System.Reactive.Linq |
| 10 |
{ |
| 11 |
internal class QueryLanguageEx : IQueryLanguageEx |
| 12 |
{ |
| 13 |
#region Create |
| 14 |
|
| 15 |
public virtual IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>> iteratorMethod) |
| 16 |
{ |
| 17 |
return new AnonymousObservable<TResult>(observer => |
| 18 |
iteratorMethod(observer).Concat().Subscribe(_ => { }, observer.OnError, observer.OnCompleted)); |
| 19 |
} |
| 20 |
|
| 21 |
public virtual IObservable<Unit> Create(Func<IEnumerable<IObservable<object>>> iteratorMethod) |
| 22 |
{ |
| 23 |
return new AnonymousObservable<Unit>(observer => |
| 24 |
iteratorMethod().Concat().Subscribe(_ => { }, observer.OnError, observer.OnCompleted)); |
| 25 |
} |
| 26 |
|
| 27 |
#endregion |
| 28 |
|
| 29 |
#region Expand |
| 30 |
|
| 31 |
public virtual IObservable<TSource> Expand<TSource>(IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector, IScheduler scheduler) |
| 32 |
{ |
| 33 |
return new AnonymousObservable<TSource>(observer => |
| 34 |
{ |
| 35 |
var outGate = new object(); |
| 36 |
var q = new Queue<IObservable<TSource>>(); |
| 37 |
var m = new SerialDisposable(); |
| 38 |
var d = new CompositeDisposable { m }; |
| 39 |
var activeCount = 0; |
| 40 |
var isAcquired = false; |
| 41 |
|
| 42 |
var ensureActive = default(Action); |
| 43 |
|
| 44 |
ensureActive = () => |
| 45 |
{ |
| 46 |
var isOwner = false; |
| 47 |
|
| 48 |
lock (q) |
| 49 |
{ |
| 50 |
if (q.Count > 0) |
| 51 |
{ |
| 52 |
isOwner = !isAcquired; |
| 53 |
isAcquired = true; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (isOwner) |
| 58 |
{ |
| 59 |
m.Disposable = scheduler.Schedule(self => |
| 60 |
{ |
| 61 |
var work = default(IObservable<TSource>); |
| 62 |
|
| 63 |
lock (q) |
| 64 |
{ |
| 65 |
if (q.Count > 0) |
| 66 |
work = q.Dequeue(); |
| 67 |
else |
| 68 |
{ |
| 69 |
isAcquired = false; |
| 70 |
return; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
var m1 = new SingleAssignmentDisposable(); |
| 75 |
d.Add(m1); |
| 76 |
m1.Disposable = work.Subscribe( |
| 77 |
x => |
| 78 |
{ |
| 79 |
lock (outGate) |
| 80 |
observer.OnNext(x); |
| 81 |
|
| 82 |
var result = default(IObservable<TSource>); |
| 83 |
try |
| 84 |
{ |
| 85 |
result = selector(x); |
| 86 |
} |
| 87 |
catch (Exception exception) |
| 88 |
{ |
| 89 |
lock (outGate) |
| 90 |
observer.OnError(exception); |
| 91 |
} |
| 92 |
|
| 93 |
lock (q) |
| 94 |
{ |
| 95 |
q.Enqueue(result); |
| 96 |
activeCount++; |
| 97 |
} |
| 98 |
|
| 99 |
ensureActive(); |
| 100 |
}, |
| 101 |
exception => |
| 102 |
{ |
| 103 |
lock (outGate) |
| 104 |
observer.OnError(exception); |
| 105 |
}, |
| 106 |
() => |
| 107 |
{ |
| 108 |
d.Remove(m1); |
| 109 |
|
| 110 |
var done = false; |
| 111 |
lock (q) |
| 112 |
{ |
| 113 |
activeCount--; |
| 114 |
if (activeCount == 0) |
| 115 |
done = true; |
| 116 |
} |
| 117 |
if (done) |
| 118 |
lock (outGate) |
| 119 |
observer.OnCompleted(); |
| 120 |
}); |
| 121 |
self(); |
| 122 |
}); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
lock (q) |
| 127 |
{ |
| 128 |
q.Enqueue(source); |
| 129 |
activeCount++; |
| 130 |
} |
| 131 |
ensureActive(); |
| 132 |
|
| 133 |
return d; |
| 134 |
}); |
| 135 |
} |
| 136 |
|
| 137 |
public virtual IObservable<TSource> Expand<TSource>(IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector) |
| 138 |
{ |
| 139 |
return source.Expand(selector, SchedulerDefaults.Iteration); |
| 140 |
} |
| 141 |
|
| 142 |
#endregion |
| 143 |
|
| 144 |
#region ForkJoin |
| 145 |
|
| 146 |
public virtual IObservable<TResult> ForkJoin<TFirst, TSecond, TResult>(IObservable<TFirst> first, IObservable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector) |
| 147 |
{ |
| 148 |
return Combine<TFirst, TSecond, TResult>(first, second, (observer, leftSubscription, rightSubscription) => |
| 149 |
{ |
| 150 |
var leftStopped = false; |
| 151 |
var rightStopped = false; |
| 152 |
var hasLeft = false; |
| 153 |
var hasRight = false; |
| 154 |
var lastLeft = default(TFirst); |
| 155 |
var lastRight = default(TSecond); |
| 156 |
|
| 157 |
return new BinaryObserver<TFirst, TSecond>( |
| 158 |
left => |
| 159 |
{ |
| 160 |
switch (left.Kind) |
| 161 |
{ |
| 162 |
case NotificationKind.OnNext: |
| 163 |
hasLeft = true; |
| 164 |
lastLeft = left.Value; |
| 165 |
break; |
| 166 |
case NotificationKind.OnError: |
| 167 |
rightSubscription.Dispose(); |
| 168 |
observer.OnError(left.Exception); |
| 169 |
break; |
| 170 |
case NotificationKind.OnCompleted: |
| 171 |
leftStopped = true; |
| 172 |
if (rightStopped) |
| 173 |
{ |
| 174 |
if (!hasLeft) |
| 175 |
observer.OnCompleted(); |
| 176 |
else if (!hasRight) |
| 177 |
observer.OnCompleted(); |
| 178 |
else |
| 179 |
{ |
| 180 |
TResult result; |
| 181 |
try |
| 182 |
{ |
| 183 |
result = resultSelector(lastLeft, lastRight); |
| 184 |
} |
| 185 |
catch (Exception exception) |
| 186 |
{ |
| 187 |
observer.OnError(exception); |
| 188 |
return; |
| 189 |
} |
| 190 |
observer.OnNext(result); |
| 191 |
observer.OnCompleted(); |
| 192 |
} |
| 193 |
} |
| 194 |
break; |
| 195 |
} |
| 196 |
}, |
| 197 |
right => |
| 198 |
{ |
| 199 |
switch (right.Kind) |
| 200 |
{ |
| 201 |
case NotificationKind.OnNext: |
| 202 |
hasRight = true; |
| 203 |
lastRight = right.Value; |
| 204 |
break; |
| 205 |
case NotificationKind.OnError: |
| 206 |
leftSubscription.Dispose(); |
| 207 |
observer.OnError(right.Exception); |
| 208 |
break; |
| 209 |
case NotificationKind.OnCompleted: |
| 210 |
rightStopped = true; |
| 211 |
if (leftStopped) |
| 212 |
{ |
| 213 |
if (!hasLeft) |
| 214 |
observer.OnCompleted(); |
| 215 |
else if (!hasRight) |
| 216 |
observer.OnCompleted(); |
| 217 |
else |
| 218 |
{ |
| 219 |
TResult result; |
| 220 |
try |
| 221 |
{ |
| 222 |
result = resultSelector(lastLeft, lastRight); |
| 223 |
} |
| 224 |
catch (Exception exception) |
| 225 |
{ |
| 226 |
observer.OnError(exception); |
| 227 |
return; |
| 228 |
} |
| 229 |
observer.OnNext(result); |
| 230 |
observer.OnCompleted(); |
| 231 |
} |
| 232 |
} |
| 233 |
break; |
| 234 |
} |
| 235 |
}); |
| 236 |
}); |
| 237 |
} |
| 238 |
|
| 239 |
public virtual IObservable<TSource[]> ForkJoin<TSource>(params IObservable<TSource>[] sources) |
| 240 |
{ |
| 241 |
return sources.ForkJoin(); |
| 242 |
} |
| 243 |
|
| 244 |
public virtual IObservable<TSource[]> ForkJoin<TSource>(IEnumerable<IObservable<TSource>> sources) |
| 245 |
{ |
| 246 |
return new AnonymousObservable<TSource[]>(subscriber => |
| 247 |
{ |
| 248 |
var allSources = sources.ToArray(); |
| 249 |
var count = allSources.Length; |
| 250 |
|
| 251 |
if (count == 0) |
| 252 |
{ |
| 253 |
subscriber.OnCompleted(); |
| 254 |
return Disposable.Empty; |
| 255 |
} |
| 256 |
|
| 257 |
var group = new CompositeDisposable(allSources.Length); |
| 258 |
var gate = new object(); |
| 259 |
|
| 260 |
var finished = false; |
| 261 |
var hasResults = new bool[count]; |
| 262 |
var hasCompleted = new bool[count]; |
| 263 |
var results = new List<TSource>(count); |
| 264 |
|
| 265 |
lock (gate) |
| 266 |
{ |
| 267 |
for (var index = 0; index < count; index++) |
| 268 |
{ |
| 269 |
var currentIndex = index; |
| 270 |
var source = allSources[index]; |
| 271 |
results.Add(default(TSource)); |
| 272 |
group.Add(source.Subscribe( |
| 273 |
value => |
| 274 |
{ |
| 275 |
lock (gate) |
| 276 |
{ |
| 277 |
if (!finished) |
| 278 |
{ |
| 279 |
hasResults[currentIndex] = true; |
| 280 |
results[currentIndex] = value; |
| 281 |
} |
| 282 |
} |
| 283 |
}, |
| 284 |
error => |
| 285 |
{ |
| 286 |
lock (gate) |
| 287 |
{ |
| 288 |
finished = true; |
| 289 |
subscriber.OnError(error); |
| 290 |
group.Dispose(); |
| 291 |
} |
| 292 |
}, |
| 293 |
() => |
| 294 |
{ |
| 295 |
lock (gate) |
| 296 |
{ |
| 297 |
if (!finished) |
| 298 |
{ |
| 299 |
if (!hasResults[currentIndex]) |
| 300 |
{ |
| 301 |
subscriber.OnCompleted(); |
| 302 |
return; |
| 303 |
} |
| 304 |
hasCompleted[currentIndex] = true; |
| 305 |
foreach (var completed in hasCompleted) |
| 306 |
{ |
| 307 |
if (!completed) |
| 308 |
return; |
| 309 |
} |
| 310 |
finished = true; |
| 311 |
subscriber.OnNext(results.ToArray()); |
| 312 |
subscriber.OnCompleted(); |
| 313 |
} |
| 314 |
} |
| 315 |
})); |
| 316 |
} |
| 317 |
} |
| 318 |
return group; |
| 319 |
}); |
| 320 |
} |
| 321 |
|
| 322 |
#endregion |
| 323 |
|
| 324 |
#region Let |
| 325 |
|
| 326 |
public virtual IObservable<TResult> Let<TSource, TResult>(IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> function) |
| 327 |
{ |
| 328 |
return function(source); |
| 329 |
} |
| 330 |
|
| 331 |
#endregion |
| 332 |
|
| 333 |
#region ManySelect |
| 334 |
|
| 335 |
public virtual IObservable<TResult> ManySelect<TSource, TResult>(IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector) |
| 336 |
{ |
| 337 |
return ManySelect(source, selector, DefaultScheduler.Instance); |
| 338 |
} |
| 339 |
|
| 340 |
public virtual IObservable<TResult> ManySelect<TSource, TResult>(IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector, IScheduler scheduler) |
| 341 |
{ |
| 342 |
return Observable.Defer<TResult>(() => |
| 343 |
{ |
| 344 |
var chain = default(ChainObservable<TSource>); |
| 345 |
|
| 346 |
return source |
| 347 |
.Select( |
| 348 |
x => |
| 349 |
{ |
| 350 |
var curr = new ChainObservable<TSource>(x); |
| 351 |
|
| 352 |
if (chain != null) |
| 353 |
chain.OnNext(curr); |
| 354 |
chain = curr; |
| 355 |
|
| 356 |
return (IObservable<TSource>)curr; |
| 357 |
}) |
| 358 |
.Do( |
| 359 |
_ => { }, |
| 360 |
exception => |
| 361 |
{ |
| 362 |
if (chain != null) |
| 363 |
chain.OnError(exception); |
| 364 |
}, |
| 365 |
() => |
| 366 |
{ |
| 367 |
if (chain != null) |
| 368 |
chain.OnCompleted(); |
| 369 |
}) |
| 370 |
.ObserveOn(scheduler) |
| 371 |
.Select(selector); |
| 372 |
}); |
| 373 |
} |
| 374 |
|
| 375 |
class ChainObservable<T> : ISubject<IObservable<T>, T> |
| 376 |
{ |
| 377 |
T head; |
| 378 |
AsyncSubject<IObservable<T>> tail = new AsyncSubject<IObservable<T>>(); |
| 379 |
|
| 380 |
public ChainObservable(T head) |
| 381 |
{ |
| 382 |
this.head = head; |
| 383 |
} |
| 384 |
|
| 385 |
public IDisposable Subscribe(IObserver<T> observer) |
| 386 |
{ |
| 387 |
var g = new CompositeDisposable(); |
| 388 |
g.Add(CurrentThreadScheduler.Instance.Schedule(() => |
| 389 |
{ |
| 390 |
observer.OnNext(head); |
| 391 |
g.Add(tail.Merge().Subscribe(observer)); |
| 392 |
})); |
| 393 |
return g; |
| 394 |
} |
| 395 |
|
| 396 |
public void OnCompleted() |
| 397 |
{ |
| 398 |
OnNext(Observable.Empty<T>()); |
| 399 |
} |
| 400 |
|
| 401 |
public void OnError(Exception error) |
| 402 |
{ |
| 403 |
OnNext(Observable.Throw<T>(error)); |
| 404 |
} |
| 405 |
|
| 406 |
public void OnNext(IObservable<T> value) |
| 407 |
{ |
| 408 |
tail.OnNext(value); |
| 409 |
tail.OnCompleted(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
#endregion |
| 414 |
|
| 415 |
#region ToListObservable |
| 416 |
|
| 417 |
public virtual ListObservable<TSource> ToListObservable<TSource>(IObservable<TSource> source) |
| 418 |
{ |
| 419 |
return new ListObservable<TSource>(source); |
| 420 |
} |
| 421 |
|
| 422 |
#endregion |
| 423 |
|
| 424 |
#region |> Helpers <| |
| 425 |
|
| 426 |
private static IObservable<TResult> Combine<TLeft, TRight, TResult>(IObservable<TLeft> leftSource, IObservable<TRight> rightSource, Func<IObserver<TResult>, IDisposable, IDisposable, IObserver<Either<Notification<TLeft>, Notification<TRight>>>> combinerSelector) |
| 427 |
{ |
| 428 |
return new AnonymousObservable<TResult>(observer => |
| 429 |
{ |
| 430 |
var leftSubscription = new SingleAssignmentDisposable(); |
| 431 |
var rightSubscription = new SingleAssignmentDisposable(); |
| 432 |
|
| 433 |
var combiner = combinerSelector(observer, leftSubscription, rightSubscription); |
| 434 |
var gate = new object(); |
| 435 |
|
| 436 |
leftSubscription.Disposable = leftSource.Materialize().Select(x => Either<Notification<TLeft>, Notification<TRight>>.CreateLeft(x)).Synchronize(gate).Subscribe(combiner); |
| 437 |
rightSubscription.Disposable = rightSource.Materialize().Select(x => Either<Notification<TLeft>, Notification<TRight>>.CreateRight(x)).Synchronize(gate).Subscribe(combiner); |
| 438 |
|
| 439 |
return new CompositeDisposable(leftSubscription, rightSubscription); |
| 440 |
}); |
| 441 |
} |
| 442 |
|
| 443 |
#endregion |
| 444 |
} |
| 445 |
} |