// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
#if WINDOWS
using System.Reactive.Concurrency;
using Windows.UI.Core;
#if HAS_OS_XAML
using Windows.UI.Xaml;
#endif
namespace System.Reactive.Linq
{
///
/// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers.
///
[CLSCompliant(false)]
public static class CoreDispatcherObservable
{
#region ObserveOn[CoreDispatcher]
///
/// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Dispatcher whose associated message loop is used to notify observers on.
/// The source sequence whose observations happen on the specified dispatcher.
/// or is null.
public static IObservable ObserveOn(this IObservable source, CoreDispatcher dispatcher)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher));
}
///
/// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Dispatcher whose associated message loop is used to notify observers on.
/// Priority to schedule work items at.
/// The source sequence whose observations happen on the specified dispatcher.
/// or is null.
public static IObservable ObserveOn(this IObservable source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher, priority));
}
#if HAS_OS_XAML
///
/// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Object to get the dispatcher from.
/// The source sequence whose observations happen on the specified object's dispatcher.
/// or is null.
public static IObservable ObserveOn(this IObservable source, DependencyObject dependencyObject)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dependencyObject == null)
{
throw new ArgumentNullException(nameof(dependencyObject));
}
return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
}
///
/// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Object to get the dispatcher from.
/// Priority to schedule work items at.
/// The source sequence whose observations happen on the specified object's dispatcher.
/// or is null.
public static IObservable ObserveOn(this IObservable source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dependencyObject == null)
{
throw new ArgumentNullException(nameof(dependencyObject));
}
return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
}
#endif
///
/// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// The source sequence whose observations happen on the current window's dispatcher.
/// is null.
public static IObservable ObserveOnCoreDispatcher(this IObservable source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return Synchronization.ObserveOn(source, CoreDispatcherScheduler.Current);
}
///
/// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Priority to schedule work items at.
/// The source sequence whose observations happen on the current window's dispatcher.
/// is null.
public static IObservable ObserveOnDispatcher(this IObservable source, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
}
#endregion
#region SubscribeOn[CoreDispatcher]
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.
/// The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.
/// or is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
/// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use .
///
public static IObservable SubscribeOn(this IObservable source, CoreDispatcher dispatcher)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher));
}
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.
/// Priority to schedule work items at.
/// The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.
/// or is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
/// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use .
///
public static IObservable SubscribeOn(this IObservable source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher, priority));
}
#if HAS_OS_XAML
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Object to get the dispatcher from.
/// The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.
/// or is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
/// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use .
///
public static IObservable SubscribeOn(this IObservable source, DependencyObject dependencyObject)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dependencyObject == null)
{
throw new ArgumentNullException(nameof(dependencyObject));
}
return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
}
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Object to get the dispatcher from.
/// Priority to schedule work items at.
/// The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.
/// or is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
/// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use .
///
public static IObservable SubscribeOn(this IObservable source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (dependencyObject == null)
{
throw new ArgumentNullException(nameof(dependencyObject));
}
return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
}
#endif
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// The source sequence whose subscriptions and unsubscriptions happen on the current window's dispatcher.
/// is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current window.
/// In order to invoke observer callbacks on the dispatcher associated with the current window, e.g. to render results in a control, use .
///
public static IObservable SubscribeOnCoreDispatcher(this IObservable source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return Synchronization.SubscribeOn(source, CoreDispatcherScheduler.Current);
}
///
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
///
/// The type of the elements in the source sequence.
/// Source sequence.
/// Priority to schedule work items at.
/// The source sequence whose subscriptions and unsubscriptions happen on the current window's dispatcher.
/// is null.
///
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current window.
/// In order to invoke observer callbacks on the dispatcher associated with the current window, e.g. to render results in a control, use .
///
public static IObservable SubscribeOnDispatcher(this IObservable source, CoreDispatcherPriority priority)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
}
#endregion
}
}
#endif