| 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 |
|
| 6 |
namespace System.Reactive.Linq |
| 7 |
{ |
| 8 |
public static partial class Observable |
| 9 |
{ |
| 10 |
#region + Subscribe + |
| 11 |
|
| 12 |
/// <summary> |
| 13 |
/// Subscribes an observer to an enumerable sequence. |
| 14 |
/// </summary> |
| 15 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 16 |
/// <param name="source">Enumerable sequence to subscribe to.</param> |
| 17 |
/// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param> |
| 18 |
/// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns> |
| 19 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception> |
| 20 |
public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer) |
| 21 |
{ |
| 22 |
if (source == null) |
| 23 |
throw new ArgumentNullException("source"); |
| 24 |
if (observer == null) |
| 25 |
throw new ArgumentNullException("observer"); |
| 26 |
|
| 27 |
return s_impl.Subscribe<TSource>(source, observer); |
| 28 |
} |
| 29 |
|
| 30 |
/// <summary> |
| 31 |
/// Subscribes an observer to an enumerable sequence, using the specified scheduler to run the enumeration loop. |
| 32 |
/// </summary> |
| 33 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 34 |
/// <param name="source">Enumerable sequence to subscribe to.</param> |
| 35 |
/// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param> |
| 36 |
/// <param name="scheduler">Scheduler to perform the enumeration on.</param> |
| 37 |
/// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns> |
| 38 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> or <paramref name="scheduler"/> is null.</exception> |
| 39 |
public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler) |
| 40 |
{ |
| 41 |
if (source == null) |
| 42 |
throw new ArgumentNullException("source"); |
| 43 |
if (observer == null) |
| 44 |
throw new ArgumentNullException("observer"); |
| 45 |
if (scheduler == null) |
| 46 |
throw new ArgumentNullException("scheduler"); |
| 47 |
|
| 48 |
return s_impl.Subscribe<TSource>(source, observer, scheduler); |
| 49 |
} |
| 50 |
|
| 51 |
#endregion |
| 52 |
|
| 53 |
#region + ToEnumerable + |
| 54 |
|
| 55 |
/// <summary> |
| 56 |
/// Converts an observable sequence to an enumerable sequence. |
| 57 |
/// </summary> |
| 58 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 59 |
/// <param name="source">An observable sequence to convert to an enumerable sequence.</param> |
| 60 |
/// <returns>The enumerable sequence containing the elements in the observable sequence.</returns> |
| 61 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 62 |
public static IEnumerable<TSource> ToEnumerable<TSource>(this IObservable<TSource> source) |
| 63 |
{ |
| 64 |
if (source == null) |
| 65 |
throw new ArgumentNullException("source"); |
| 66 |
|
| 67 |
return s_impl.ToEnumerable<TSource>(source); |
| 68 |
} |
| 69 |
|
| 70 |
#endregion |
| 71 |
|
| 72 |
#region ToEvent |
| 73 |
|
| 74 |
/// <summary> |
| 75 |
/// Exposes an observable sequence as an object with an Action-based .NET event. |
| 76 |
/// </summary> |
| 77 |
/// <param name="source">Observable source sequence.</param> |
| 78 |
/// <returns>The event source object.</returns> |
| 79 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 80 |
public static IEventSource<Unit> ToEvent(this IObservable<Unit> source) |
| 81 |
{ |
| 82 |
if (source == null) |
| 83 |
throw new ArgumentNullException("source"); |
| 84 |
|
| 85 |
return s_impl.ToEvent(source); |
| 86 |
} |
| 87 |
|
| 88 |
/// <summary> |
| 89 |
/// Exposes an observable sequence as an object with an Action<TSource>-based .NET event. |
| 90 |
/// </summary> |
| 91 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 92 |
/// <param name="source">Observable source sequence.</param> |
| 93 |
/// <returns>The event source object.</returns> |
| 94 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 95 |
public static IEventSource<TSource> ToEvent<TSource>(this IObservable<TSource> source) |
| 96 |
{ |
| 97 |
if (source == null) |
| 98 |
throw new ArgumentNullException("source"); |
| 99 |
|
| 100 |
return s_impl.ToEvent<TSource>(source); |
| 101 |
} |
| 102 |
|
| 103 |
#endregion |
| 104 |
|
| 105 |
#region ToEventPattern |
| 106 |
|
| 107 |
/// <summary> |
| 108 |
/// Exposes an observable sequence as an object with a .NET event, conforming to the standard .NET event pattern. |
| 109 |
/// </summary> |
| 110 |
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam> |
| 111 |
/// <param name="source">Observable source sequence.</param> |
| 112 |
/// <returns>The event source object.</returns> |
| 113 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 114 |
public static IEventPatternSource<TEventArgs> ToEventPattern<TEventArgs>(this IObservable<EventPattern<TEventArgs>> source) |
| 115 |
#if !NO_EVENTARGS_CONSTRAINT |
| 116 |
where TEventArgs : EventArgs |
| 117 |
#endif |
| 118 |
{ |
| 119 |
if (source == null) |
| 120 |
throw new ArgumentNullException("source"); |
| 121 |
|
| 122 |
return s_impl.ToEventPattern<TEventArgs>(source); |
| 123 |
} |
| 124 |
|
| 125 |
#endregion |
| 126 |
|
| 127 |
#region + ToObservable + |
| 128 |
|
| 129 |
/// <summary> |
| 130 |
/// Converts an enumerable sequence to an observable sequence. |
| 131 |
/// </summary> |
| 132 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 133 |
/// <param name="source">Enumerable sequence to convert to an observable sequence.</param> |
| 134 |
/// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns> |
| 135 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 136 |
public static IObservable<TSource> ToObservable<TSource>(this IEnumerable<TSource> source) |
| 137 |
{ |
| 138 |
if (source == null) |
| 139 |
throw new ArgumentNullException("source"); |
| 140 |
|
| 141 |
return s_impl.ToObservable<TSource>(source); |
| 142 |
} |
| 143 |
|
| 144 |
/// <summary> |
| 145 |
/// Converts an enumerable sequence to an observable sequence, using the specified scheduler to run the enumeration loop. |
| 146 |
/// </summary> |
| 147 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 148 |
/// <param name="source">Enumerable sequence to convert to an observable sequence.</param> |
| 149 |
/// <param name="scheduler">Scheduler to run the enumeration of the input sequence on.</param> |
| 150 |
/// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns> |
| 151 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception> |
| 152 |
public static IObservable<TSource> ToObservable<TSource>(this IEnumerable<TSource> source, IScheduler scheduler) |
| 153 |
{ |
| 154 |
if (source == null) |
| 155 |
throw new ArgumentNullException("source"); |
| 156 |
if (scheduler == null) |
| 157 |
throw new ArgumentNullException("scheduler"); |
| 158 |
|
| 159 |
return s_impl.ToObservable<TSource>(source, scheduler); |
| 160 |
} |
| 161 |
|
| 162 |
#endregion |
| 163 |
} |
| 164 |
} |