diff --git a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs index 9fa69c408..8e831b958 100644 --- a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs +++ b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs @@ -22,7 +22,7 @@ public VolSurfaceWindow( VolSurfaceWindowFactory factory, IBus bus, string vs_name, VolSurfaceWindowSettings startup_data) : - base("Vol Surface", bus, factory, Icons.vol_surface) { + base(vs_name, bus, factory, Icons.vol_surface) { _startup_data = startup_data ?? throw new ArgumentNullException(nameof(startup_data)); this.vs_name = vs_name; _dispose = Disposable.Create(() => Disposable.dispose(ref _vm)); diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index cd4afa871..f731a39b3 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -284,32 +284,50 @@ namespace System.Collections.Specialized { namespace System.Reactive.Linq { static class SparkObservableExtensions { - // This is placed here to intentionally make ObserveOnDispatcher ambiguous with the one provided by Rx - public static IObservable ObserveOnDispatcher(this IObservable source) { - throw new InvalidOperationException("This method can starve the UI of input messages. Please use OnDispatcher() instead"); - } + public static IObservable OnDispatcher( + this IObservable source, + DispatcherPriority priority = DispatcherPriority.Background) => + source.OnDispatcher(Dispatcher.CurrentDispatcher, priority); public static IObservable OnDispatcher( this IObservable source, + Dispatcher dispatcher, DispatcherPriority priority = DispatcherPriority.Background) { - var dispatcher = Dispatcher.CurrentDispatcher; + if (dispatcher is null) { + throw new ArgumentNullException(nameof(dispatcher)); + } + return Observable.Create(o => { return source.Subscribe(on_next); - async void on_next(T value) { - await dispatcher.check_invoke_async(() => o.OnNext(value), priority); + void on_next(T value) { + if (dispatcher.CheckAccess()) { + o.OnNext(value); + return; + } + dispatcher.InvokeAsync(() => o.OnNext(value), priority); } }); } 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() {