fix: Use calling thread's Dispatcher in SubscribeOnDispatcher

This commit is contained in:
John Colvin committed 2022-02-15 12:56:58 -05:00
1 parent e020cca9d2
commit 09aeef03a6
1 file changed
+11 -1
@@ -307,13 +307,23 @@ namespace System.Reactive.Linq {
public static IObservable<T> SubscribeOnDispatcher<T>(
this IObservable<T> source,
DispatcherPriority priority = DispatcherPriority.Background) =>
source.SubscribeOnDispatcher(Dispatcher.CurrentDispatcher, priority);
public static IObservable<T> SubscribeOnDispatcher<T>(
this IObservable<T> source,
Dispatcher dispatcher,
DispatcherPriority priority = DispatcherPriority.Background) {
if (dispatcher is null) {
throw new ArgumentNullException(nameof(dispatcher));
}
return Observable.Create<T>(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() {