diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index 309f5f5e5..788dc45b0 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -307,13 +307,23 @@ namespace System.Reactive.Linq { public static IObservable SubscribeOnDispatcher( this IObservable source, + DispatcherPriority priority = DispatcherPriority.Background) => + source.SubscribeOnDispatcher(Dispatcher.CurrentDispatcher, priority); + + public static IObservable SubscribeOnDispatcher( + this IObservable source, + Dispatcher dispatcher, DispatcherPriority priority = DispatcherPriority.Background) { + if (dispatcher is null) { + throw new ArgumentNullException(nameof(dispatcher)); + } + return Observable.Create(o => { object locker = new(); // init sub to a non-null instance, and let 'sub == null' signal // that the subscription has been disposed IDisposable sub = Disposable.empty; - Dispatcher.CurrentDispatcher.InvokeAsync(subscribe, priority); + dispatcher.InvokeAsync(subscribe, priority); return Disposable.Create(dispose); void subscribe() {