From 09aeef03a64e3cd105427009dedb40704b3d52c8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 15 Feb 2022 12:52:33 -0500 Subject: [PATCH] fix: Use calling thread's Dispatcher in SubscribeOnDispatcher --- .../spark/core/GlobalExtensions/GlobalExtensions.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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() {