From ae3fc51603e842fcc98b435d14f2ccbe0d416a49 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 6 Dec 2018 16:04:02 -0600 Subject: [PATCH] ViewModelBase.Property - observe_current uses delegate to get initial value, so it is always up-to-date --- src/client/spark/core/GlobalExtensions/GlobalExtensions.cs | 6 ++++++ src/client/spark/core/Utility/ViewModelBase.cs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index b72c43513..a5cc7ca5c 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -317,6 +317,12 @@ namespace System.Reactive.Linq public static async Task<(TResult result, bool success)> FirstAsyncTask(this IObservable source, Func predicate, Func selector, TimeSpan timeout) where T : class => await source.Where(predicate).Select(val => (selector(val), true)).Merge(Observable.Return((default(TResult), false)).Delay(timeout)).FirstAsync(); + public static IObservable StartWith(this IObservable source, Func get_value) => + Observable.Create(o => { + o.OnNext(get_value()); + return source.Subscribe(o); + }); + // 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"); diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index d2040d3b1..542ec5d1f 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -192,7 +192,7 @@ namespace mbts.ui.shell public IObservable observe_current(bool start_immediately = false) { var source = this.Select(vals => vals.newval); if (start_immediately) - source = source.StartWith(_value); + source = source.StartWith(() => _value); return source; }