// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. #if !WINDOWS using System.Reactive.Concurrency; using System.Windows; using System.Windows.Threading; namespace System.Reactive.Linq { /// /// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers. /// public static class DispatcherObservable { #region ObserveOn[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 to notify observers on. /// The source sequence whose observations happen on the specified dispatcher. /// or is null. public static IObservable ObserveOn(this IObservable source, Dispatcher dispatcher) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcher == null) throw new ArgumentNullException(nameof(dispatcher)); return ObserveOn_(source, 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 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, Dispatcher dispatcher, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcher == null) throw new ArgumentNullException(nameof(dispatcher)); return ObserveOn_(source, dispatcher, priority); } /// /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher scheduler. /// /// The type of the elements in the source sequence. /// Source sequence. /// Dispatcher scheduler to notify observers on. /// The source sequence whose observations happen on the specified dispatcher scheduler. /// or is null. public static IObservable ObserveOn(this IObservable source, DispatcherScheduler scheduler) { if (source == null) throw new ArgumentNullException(nameof(source)); if (scheduler == null) throw new ArgumentNullException(nameof(scheduler)); return ObserveOn_(source, scheduler.Dispatcher, scheduler.Priority); } /// /// 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, DispatcherObject dispatcherObject) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcherObject == null) throw new ArgumentNullException(nameof(dispatcherObject)); return ObserveOn_(source, dispatcherObject.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, DispatcherObject dispatcherObject, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcherObject == null) throw new ArgumentNullException(nameof(dispatcherObject)); return ObserveOn_(source, dispatcherObject.Dispatcher, priority); } /// /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread. /// /// The type of the elements in the source sequence. /// Source sequence. /// The source sequence whose observations happen on the current thread's dispatcher. /// is null. public static IObservable ObserveOnDispatcher(this IObservable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return ObserveOn_(source, DispatcherScheduler.Current.Dispatcher); } /// /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread. /// /// 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 thread's dispatcher. /// is null. public static IObservable ObserveOnDispatcher(this IObservable source, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); return ObserveOn_(source, DispatcherScheduler.Current.Dispatcher, priority); } private static IObservable ObserveOn_(IObservable source, Dispatcher dispatcher, DispatcherPriority priority) { return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher, priority)); } private static IObservable ObserveOn_(IObservable source, Dispatcher dispatcher) { return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher)); } #endregion #region SubscribeOn[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 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, Dispatcher dispatcher) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcher == null) throw new ArgumentNullException(nameof(dispatcher)); return SubscribeOn_(source, 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 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, Dispatcher dispatcher, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcher == null) throw new ArgumentNullException(nameof(dispatcher)); return SubscribeOn_(source, dispatcher, priority); } /// /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher scheduler. /// /// The type of the elements in the source sequence. /// Source sequence. /// Dispatcher scheduler to perform subscription and unsubscription actions on. /// The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher scheduler. /// 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 scheduler. /// In order to invoke observer callbacks on the specified scheduler, e.g. to render results in a control, use . /// public static IObservable SubscribeOn(this IObservable source, DispatcherScheduler scheduler) { if (source == null) throw new ArgumentNullException(nameof(source)); if (scheduler == null) throw new ArgumentNullException(nameof(scheduler)); return SubscribeOn_(source, scheduler.Dispatcher, scheduler.Priority); } /// /// 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, DispatcherObject dispatcherObject) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcherObject == null) throw new ArgumentNullException(nameof(dispatcherObject)); return SubscribeOn_(source, dispatcherObject.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, DispatcherObject dispatcherObject, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); if (dispatcherObject == null) throw new ArgumentNullException(nameof(dispatcherObject)); return SubscribeOn_(source, dispatcherObject.Dispatcher, priority); } /// /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread. /// /// The type of the elements in the source sequence. /// Source sequence. /// The source sequence whose subscriptions and unsubscriptions happen on the current thread'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 thread. /// In order to invoke observer callbacks on the dispatcher associated with the current thread, e.g. to render results in a control, use . /// public static IObservable SubscribeOnDispatcher(this IObservable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SubscribeOn_(source, DispatcherScheduler.Current.Dispatcher); } /// /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread. /// /// 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 thread'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 thread. /// In order to invoke observer callbacks on the dispatcher associated with the current thread, e.g. to render results in a control, use . /// public static IObservable SubscribeOnDispatcher(this IObservable source, DispatcherPriority priority) { if (source == null) throw new ArgumentNullException(nameof(source)); return SubscribeOn_(source, DispatcherScheduler.Current.Dispatcher, priority); } private static IObservable SubscribeOn_(IObservable source, Dispatcher dispatcher, DispatcherPriority priority) { return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher, priority)); } private static IObservable SubscribeOn_(IObservable source, Dispatcher dispatcher) { return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher)); } #endregion } } #endif