| 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.Reactive.Concurrency; |
| 5 |
using System.Reactive.Disposables; |
| 6 |
|
| 7 |
namespace System.Reactive.Linq |
| 8 |
{ |
| 9 |
#if !NO_PERF |
| 10 |
using ObservableImpl; |
| 11 |
#endif |
| 12 |
|
| 13 |
internal partial class QueryLanguage |
| 14 |
{ |
| 15 |
#region + Subscribe + |
| 16 |
|
| 17 |
public virtual IDisposable Subscribe<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer) |
| 18 |
{ |
| 19 |
return Subscribe_<TSource>(source, observer, SchedulerDefaults.Iteration); |
| 20 |
} |
| 21 |
|
| 22 |
public virtual IDisposable Subscribe<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler) |
| 23 |
{ |
| 24 |
return Subscribe_<TSource>(source, observer, scheduler); |
| 25 |
} |
| 26 |
|
| 27 |
private static IDisposable Subscribe_<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler) |
| 28 |
{ |
| 29 |
#if !NO_PERF |
| 30 |
// |
| 31 |
// [OK] Use of unsafe Subscribe: we're calling into a known producer implementation. |
| 32 |
// |
| 33 |
return new ToObservable<TSource>(source, scheduler).Subscribe/*Unsafe*/(observer); |
| 34 |
#else |
| 35 |
var e = source.GetEnumerator(); |
| 36 |
var flag = new BooleanDisposable(); |
| 37 |
|
| 38 |
scheduler.Schedule(self => |
| 39 |
{ |
| 40 |
var hasNext = false; |
| 41 |
var ex = default(Exception); |
| 42 |
var current = default(TSource); |
| 43 |
|
| 44 |
if (flag.IsDisposed) |
| 45 |
{ |
| 46 |
e.Dispose(); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
try |
| 51 |
{ |
| 52 |
hasNext = e.MoveNext(); |
| 53 |
if (hasNext) |
| 54 |
current = e.Current; |
| 55 |
} |
| 56 |
catch (Exception exception) |
| 57 |
{ |
| 58 |
ex = exception; |
| 59 |
} |
| 60 |
|
| 61 |
if (!hasNext || ex != null) |
| 62 |
{ |
| 63 |
e.Dispose(); |
| 64 |
} |
| 65 |
|
| 66 |
if (ex != null) |
| 67 |
{ |
| 68 |
observer.OnError(ex); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (!hasNext) |
| 73 |
{ |
| 74 |
observer.OnCompleted(); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
observer.OnNext(current); |
| 79 |
self(); |
| 80 |
}); |
| 81 |
|
| 82 |
return flag; |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
#endregion |
| 87 |
|
| 88 |
#region + ToEnumerable + |
| 89 |
|
| 90 |
public virtual IEnumerable<TSource> ToEnumerable<TSource>(IObservable<TSource> source) |
| 91 |
{ |
| 92 |
return new AnonymousEnumerable<TSource>(() => source.GetEnumerator()); |
| 93 |
} |
| 94 |
|
| 95 |
#endregion |
| 96 |
|
| 97 |
#region ToEvent |
| 98 |
|
| 99 |
public virtual IEventSource<Unit> ToEvent(IObservable<Unit> source) |
| 100 |
{ |
| 101 |
return new EventSource<Unit>(source, (h, _) => h(Unit.Default)); |
| 102 |
} |
| 103 |
|
| 104 |
public virtual IEventSource<TSource> ToEvent<TSource>(IObservable<TSource> source) |
| 105 |
{ |
| 106 |
return new EventSource<TSource>(source, (h, value) => h(value)); |
| 107 |
} |
| 108 |
|
| 109 |
#endregion |
| 110 |
|
| 111 |
#region ToEventPattern |
| 112 |
|
| 113 |
public virtual IEventPatternSource<TEventArgs> ToEventPattern<TEventArgs>(IObservable<EventPattern<TEventArgs>> source) |
| 114 |
#if !NO_EVENTARGS_CONSTRAINT |
| 115 |
where TEventArgs : EventArgs |
| 116 |
#endif |
| 117 |
{ |
| 118 |
return new EventPatternSource<TEventArgs>( |
| 119 |
#if !NO_VARIANCE |
| 120 |
source, |
| 121 |
#else |
| 122 |
source.Select(x => (EventPattern<object, TEventArgs>)x), |
| 123 |
#endif |
| 124 |
(h, evt) => h(evt.Sender, evt.EventArgs) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
#endregion |
| 129 |
|
| 130 |
#region + ToObservable + |
| 131 |
|
| 132 |
public virtual IObservable<TSource> ToObservable<TSource>(IEnumerable<TSource> source) |
| 133 |
{ |
| 134 |
#if !NO_PERF |
| 135 |
return new ToObservable<TSource>(source, SchedulerDefaults.Iteration); |
| 136 |
#else |
| 137 |
return ToObservable_(source, SchedulerDefaults.Iteration); |
| 138 |
#endif |
| 139 |
} |
| 140 |
|
| 141 |
public virtual IObservable<TSource> ToObservable<TSource>(IEnumerable<TSource> source, IScheduler scheduler) |
| 142 |
{ |
| 143 |
#if !NO_PERF |
| 144 |
return new ToObservable<TSource>(source, scheduler); |
| 145 |
#else |
| 146 |
return ToObservable_(source, scheduler); |
| 147 |
#endif |
| 148 |
} |
| 149 |
|
| 150 |
#if NO_PERF |
| 151 |
private static IObservable<TSource> ToObservable_<TSource>(IEnumerable<TSource> source, IScheduler scheduler) |
| 152 |
{ |
| 153 |
return new AnonymousObservable<TSource>(observer => source.Subscribe(observer, scheduler)); |
| 154 |
} |
| 155 |
#endif |
| 156 |
|
| 157 |
#endregion |
| 158 |
} |
| 159 |
} |