| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Concurrency; |
| 4 |
using System.Threading; |
| 5 |
|
| 6 |
namespace System.Reactive.Linq |
| 7 |
{ |
| 8 |
public static partial class Observable |
| 9 |
{ |
| 10 |
#region + ObserveOn + |
| 11 |
|
| 12 |
/// <summary> |
| 13 |
/// Wraps the source sequence in order to run its observer callbacks on the specified scheduler. |
| 14 |
/// </summary> |
| 15 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 16 |
/// <param name="source">Source sequence.</param> |
| 17 |
/// <param name="scheduler">Scheduler to notify observers on.</param> |
| 18 |
/// <returns>The source sequence whose observations happen on the specified scheduler.</returns> |
| 19 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception> |
| 20 |
/// <remarks> |
| 21 |
/// This only invokes observer callbacks on a scheduler. In case the subscription and/or unsubscription actions have side-effects |
| 22 |
/// that require to be run on a scheduler, use <see cref="Observable.SubscribeOn{TSource}(IObservable{TSource}, IScheduler)"/>. |
| 23 |
/// </remarks> |
| 24 |
public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, IScheduler scheduler) |
| 25 |
{ |
| 26 |
if (source == null) |
| 27 |
throw new ArgumentNullException("source"); |
| 28 |
if (scheduler == null) |
| 29 |
throw new ArgumentNullException("scheduler"); |
| 30 |
|
| 31 |
return s_impl.ObserveOn<TSource>(source, scheduler); |
| 32 |
} |
| 33 |
|
| 34 |
#if !NO_SYNCCTX |
| 35 |
/// <summary> |
| 36 |
/// Wraps the source sequence in order to run its observer callbacks on the specified synchronization context. |
| 37 |
/// </summary> |
| 38 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 39 |
/// <param name="source">Source sequence.</param> |
| 40 |
/// <param name="context">Synchronization context to notify observers on.</param> |
| 41 |
/// <returns>The source sequence whose observations happen on the specified synchronization context.</returns> |
| 42 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is null.</exception> |
| 43 |
/// <remarks> |
| 44 |
/// This only invokes observer callbacks on a synchronization context. In case the subscription and/or unsubscription actions have side-effects |
| 45 |
/// that require to be run on a synchronization context, use <see cref="Observable.SubscribeOn{TSource}(IObservable{TSource}, SynchronizationContext)"/>. |
| 46 |
/// </remarks> |
| 47 |
public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, SynchronizationContext context) |
| 48 |
{ |
| 49 |
if (source == null) |
| 50 |
throw new ArgumentNullException("source"); |
| 51 |
if (context == null) |
| 52 |
throw new ArgumentNullException("context"); |
| 53 |
|
| 54 |
return s_impl.ObserveOn<TSource>(source, context); |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
#endregion |
| 59 |
|
| 60 |
#region + SubscribeOn + |
| 61 |
|
| 62 |
/// <summary> |
| 63 |
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified scheduler. This operation is not commonly used; |
| 64 |
/// see the remarks section for more information on the distinction between SubscribeOn and ObserveOn. |
| 65 |
/// </summary> |
| 66 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 67 |
/// <param name="source">Source sequence.</param> |
| 68 |
/// <param name="scheduler">Scheduler to perform subscription and unsubscription actions on.</param> |
| 69 |
/// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.</returns> |
| 70 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception> |
| 71 |
/// <remarks> |
| 72 |
/// This only performs the side-effects of subscription and unsubscription on the specified scheduler. In order to invoke observer |
| 73 |
/// callbacks on a scheduler, use <see cref="Observable.ObserveOn{TSource}(IObservable{TSource}, IScheduler)"/>. |
| 74 |
/// </remarks> |
| 75 |
public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, IScheduler scheduler) |
| 76 |
{ |
| 77 |
if (source == null) |
| 78 |
throw new ArgumentNullException("source"); |
| 79 |
if (scheduler == null) |
| 80 |
throw new ArgumentNullException("scheduler"); |
| 81 |
|
| 82 |
return s_impl.SubscribeOn<TSource>(source, scheduler); |
| 83 |
} |
| 84 |
|
| 85 |
#if !NO_SYNCCTX |
| 86 |
/// <summary> |
| 87 |
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified synchronization context. This operation is not commonly used; |
| 88 |
/// see the remarks section for more information on the distinction between SubscribeOn and ObserveOn. |
| 89 |
/// </summary> |
| 90 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 91 |
/// <param name="source">Source sequence.</param> |
| 92 |
/// <param name="context">Synchronization context to perform subscription and unsubscription actions on.</param> |
| 93 |
/// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified synchronization context.</returns> |
| 94 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is null.</exception> |
| 95 |
/// <remarks> |
| 96 |
/// This only performs the side-effects of subscription and unsubscription on the specified synchronization context. In order to invoke observer |
| 97 |
/// callbacks on a synchronization context, use <see cref="Observable.ObserveOn{TSource}(IObservable{TSource}, SynchronizationContext)"/>. |
| 98 |
/// </remarks> |
| 99 |
public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, SynchronizationContext context) |
| 100 |
{ |
| 101 |
if (source == null) |
| 102 |
throw new ArgumentNullException("source"); |
| 103 |
if (context == null) |
| 104 |
throw new ArgumentNullException("context"); |
| 105 |
|
| 106 |
return s_impl.SubscribeOn<TSource>(source, context); |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
#endregion |
| 111 |
|
| 112 |
#region + Synchronize + |
| 113 |
|
| 114 |
/// <summary> |
| 115 |
/// Synchronizes the observable sequence such that observer notifications cannot be delivered concurrently. |
| 116 |
/// This overload is useful to "fix" an observable sequence that exhibits concurrent callbacks on individual observers, which is invalid behavior for the query processor. |
| 117 |
/// </summary> |
| 118 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 119 |
/// <param name="source">Source sequence.</param> |
| 120 |
/// <returns>The source sequence whose outgoing calls to observers are synchronized.</returns> |
| 121 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 122 |
/// <remarks> |
| 123 |
/// It's invalid behavior - according to the observer grammar - for a sequence to exhibit concurrent callbacks on a given observer. |
| 124 |
/// This operator can be used to "fix" a source that doesn't conform to this rule. |
| 125 |
/// </remarks> |
| 126 |
public static IObservable<TSource> Synchronize<TSource>(this IObservable<TSource> source) |
| 127 |
{ |
| 128 |
if (source == null) |
| 129 |
throw new ArgumentNullException("source"); |
| 130 |
|
| 131 |
return s_impl.Synchronize<TSource>(source); |
| 132 |
} |
| 133 |
|
| 134 |
/// <summary> |
| 135 |
/// Synchronizes the observable sequence such that observer notifications cannot be delivered concurrently, using the specified gate object. |
| 136 |
/// This overload is useful when writing n-ary query operators, in order to prevent concurrent callbacks from different sources by synchronizing on a common gate object. |
| 137 |
/// </summary> |
| 138 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 139 |
/// <param name="source">Source sequence.</param> |
| 140 |
/// <param name="gate">Gate object to synchronize each observer call on.</param> |
| 141 |
/// <returns>The source sequence whose outgoing calls to observers are synchronized on the given gate object.</returns> |
| 142 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="gate"/> is null.</exception> |
| 143 |
public static IObservable<TSource> Synchronize<TSource>(this IObservable<TSource> source, object gate) |
| 144 |
{ |
| 145 |
if (source == null) |
| 146 |
throw new ArgumentNullException("source"); |
| 147 |
if (gate == null) |
| 148 |
throw new ArgumentNullException("gate"); |
| 149 |
|
| 150 |
return s_impl.Synchronize<TSource>(source, gate); |
| 151 |
} |
| 152 |
|
| 153 |
#endregion |
| 154 |
} |
| 155 |
} |