remove hacky ILazyAlgo interface, replace functionality with subscribe_is_dirty extension
This commit is contained in:
1 parent
00b68a4408
commit
9b6a5ea943
5 files changed
+70
-37
No files matched your search
@@ -131,27 +131,16 @@ namespace mbts.spark {
|
||||
as_demuxer().subscribe_many(_refresh_on_local_changes.execute, nameof(this.name), nameof(abbreviation), nameof(badge_fill)),
|
||||
_edit_pane_vs_model.add_source(VisualStateNames.BadgeSelector.GroupName, custom_badge_color.observe_nullable(true).Select(t => t.HasValue ? VisualStateNames.BadgeSelector.Active : VisualStateNames.BadgeSelector.Inactive)),
|
||||
_edit_pane_vs_model.add_source(VisualStateNames.Abbreviation.GroupName, _abbreviation.observe_current(true).Select(a => string.IsNullOrEmpty(a) ? VisualStateNames.Abbreviation.Unpopulated : VisualStateNames.Abbreviation.Populated)),
|
||||
_algo_root.observe_current(true).SubscribeEach(vm => {
|
||||
if (vm == null)
|
||||
return Disposable.empty;
|
||||
if (vm is ILazyAlgo la) {
|
||||
bool synced_with_loaded = false;
|
||||
return vm.subscribe_definition().Subscribe(_ => {
|
||||
if (la.is_loaded) {
|
||||
if (!synced_with_loaded) {
|
||||
synced_with_loaded = true;
|
||||
_algo_root.observe_current(true).SubscribeEach(vm =>
|
||||
vm == null ? null :
|
||||
Disposable.Create(
|
||||
vm.subscribe_is_dirty().Subscribe(v => {
|
||||
if (!v) {
|
||||
_version.increment_version();
|
||||
_on_committed_changed();
|
||||
} else {
|
||||
_refresh_on_local_changes.execute();
|
||||
}
|
||||
} else {
|
||||
synced_with_loaded = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
return vm.subscribe_definition().Subscribe(_ => _refresh_on_local_changes.execute());
|
||||
}),
|
||||
}),
|
||||
vm.subscribe_definition().Subscribe(_ => _refresh_on_local_changes.execute()))),
|
||||
ObservableAutoWrapper.connect_item_hooks(algo_roots, a => a as IDisposable ?? Disposable.empty, true),
|
||||
ObservableAutoWrapper.connect_item_hooks(templates, t => new CompositeDisposable(_any_templates_dirty_monitor.join(t), t), true),
|
||||
Disposable.Create(() => {
|
||||
@@ -442,8 +431,12 @@ namespace mbts.spark {
|
||||
_committed = _version.value;
|
||||
_is_dirty.refresh();
|
||||
_refresh_undo_redo();
|
||||
RaisePropertyChanged(nameof(committed_value), nameof(current_value));
|
||||
}
|
||||
|
||||
public Snapshot? committed_value => _committed;
|
||||
public Snapshot current_value => _version.value;
|
||||
|
||||
IAlgoVM _mk_algo_root(CustomOrderDefinitionImmutable definition) =>
|
||||
OrderTypeVMFactory.create(new AlgoData(definition), is_expanded: true);
|
||||
|
||||
@@ -546,5 +539,17 @@ namespace mbts.spark {
|
||||
public static bool operator ==(Snapshot a, Snapshot b) => a.Equals(b);
|
||||
public static bool operator !=(Snapshot a, Snapshot b) => !a.Equals(b);
|
||||
}
|
||||
|
||||
struct SnapshotTemplateEqualityComparer : IEqualityComparer<Snapshot> {
|
||||
public bool Equals(Snapshot x, Snapshot y) =>
|
||||
x.abbreviation == y.abbreviation &&
|
||||
x.custom_badge_color == y.custom_badge_color &&
|
||||
x.format == y.format &&
|
||||
x.data.key == y.data.key &&
|
||||
x.data.name == y.data.name &&
|
||||
x.data.value.Equals(y.data.value);
|
||||
|
||||
public int GetHashCode(Snapshot obj) => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,12 +62,6 @@ namespace mbts.spark {
|
||||
IAlgoVM impl { get; }
|
||||
}
|
||||
|
||||
delegate void AlgoLoaded(bool reset_edit_stack);
|
||||
|
||||
interface ILazyAlgo {
|
||||
bool is_loaded { get; }
|
||||
}
|
||||
|
||||
interface IAlgoParamVM {
|
||||
event Action<bool> is_dirty_changed;
|
||||
event PropertyChange<AlgoParamPermissions> permissions_changed;
|
||||
@@ -136,6 +130,25 @@ namespace mbts.spark {
|
||||
public static IEnumerable<IAlgoParamVM> enumerate_all_params(this IAlgoVM vm) =>
|
||||
vm == null ? Enumerable.Empty<IAlgoParamVM>() : vm.algo_params.Concat(vm.child_algo_selectors.SelectMany(s => enumerate_all_params(s.selected_algo)));
|
||||
|
||||
public static IObservable<bool> subscribe_is_dirty(this IAlgoParamVM param_vm) =>
|
||||
Observable.Create<bool>(o => {
|
||||
o.OnNext(param_vm.is_dirty);
|
||||
param_vm.is_dirty_changed += o.OnNext;
|
||||
return Disposable.Create(dispose);
|
||||
void dispose() => param_vm.is_dirty_changed -= o.OnNext;
|
||||
});
|
||||
|
||||
public static IObservable<bool> subscribe_is_dirty(this IAlgoVM vm) =>
|
||||
Observable.Create<bool>(o => {
|
||||
var mon = new ReactiveThresholdMonitor<string>();
|
||||
return Disposable.Create(
|
||||
ObservableAutoWrapper.connect_item_hooks(vm.child_algo_selectors, set_selector_hooks),
|
||||
ObservableAutoWrapper.connect_item_hooks(vm.algo_params, p => mon.join(p.id, p.subscribe_is_dirty())),
|
||||
mon.Subscribe(o));
|
||||
IDisposable set_selector_hooks(IAlgoSelector s) =>
|
||||
s.subscribe_selected_algo().SubscribeEach(_vm => mon.join(s.id, _vm.subscribe_is_dirty()));
|
||||
});
|
||||
|
||||
public static IObservable<AlgoParamPermissions> subscribe_permissions(this IAlgoParamVM param_vm) =>
|
||||
Observable.Create<AlgoParamPermissions>(o => {
|
||||
o.OnNext(param_vm.permissions);
|
||||
|
||||
@@ -504,24 +504,28 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
protected Property<TValue> add_property<TValue>(string prop_name, Func<TMutable, TValue> get_from_def, Action<TMutable, TValue> assign_to_def, TValue init_value = default, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) {
|
||||
protected Property<TValue> add_property<TValue>(string prop_name, Func<TMutable, TValue> get_from_def, Action<TMutable, TValue> assign_to_def, TValue init_value = default, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) =>
|
||||
add_property(prop_name, get_from_def, assign_to_def, out AlgoParamVM<TValue> param, init_value, permissions);
|
||||
|
||||
protected Property<TValue> add_property<TValue>(string prop_name, Func<TMutable, TValue> get_from_def, Action<TMutable, TValue> assign_to_def, out AlgoParamVM<TValue> param, TValue init_value = default, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) {
|
||||
var prop = backing(prop_name, init_value);
|
||||
var param = new AlgoParamVM<TValue>(prop_name, prop_name, init_value) { permissions = permissions };
|
||||
_link_props(param, prop);
|
||||
_algo_params.Add(param);
|
||||
var local_param = new AlgoParamVM<TValue>(prop_name, prop_name, init_value) { permissions = permissions };
|
||||
param = local_param;
|
||||
_link_props(local_param, prop);
|
||||
_algo_params.Add(local_param);
|
||||
_auto_props.Add(new AutoProp(
|
||||
prop_name,
|
||||
def => {
|
||||
if (param.has_value)
|
||||
if (local_param.has_value)
|
||||
assign_to_def(def, prop.Value);
|
||||
},
|
||||
def => assign_to_prop_and_commit(get_from_def(def)),
|
||||
algos => assign_to_prop_and_commit(algos.Select(get_from_def).Distinct().SafeSingleOrDefault()),
|
||||
_link_props(param, prop)));
|
||||
_link_props(local_param, prop)));
|
||||
return prop;
|
||||
void assign_to_prop_and_commit(TValue value) {
|
||||
prop.set(value);
|
||||
param.mark_committed();
|
||||
local_param.mark_committed();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.collections;
|
||||
using bts.spark;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
@@ -13,9 +15,10 @@ using SpreaderDefi = bts.api.orderman.SpreaderOrderDefinitionImmutable;
|
||||
|
||||
namespace mbts.spark {
|
||||
static partial class OrderTypeVMFactory {
|
||||
class SpreaderOrderVM : OrderTypeVMBase<SpreaderDef, SpreaderDefi>, IAlgoVM, ILazyAlgo, IObserver<OrdersModel> {
|
||||
class SpreaderOrderVM : OrderTypeVMBase<SpreaderDef, SpreaderDefi>, IAlgoVM, IObserver<OrdersModel> {
|
||||
readonly IProperty<SpreaderDefi?> _template;
|
||||
readonly StrategyEditorWrapper _strategy_editor_wrapper;
|
||||
readonly AlgoParamVM<ExecutionStrategy> _strategy_param;
|
||||
IDisposable _strategy_sub;
|
||||
|
||||
public SpreaderOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions)
|
||||
@@ -23,9 +26,12 @@ namespace mbts.spark {
|
||||
_template = backing<SpreaderDefi?>(nameof(template)).block_reentrancy();
|
||||
/// <see cref="_strategy_editor_wrapper"/> depends on <see cref="_template"/> (i.e. initialize the latter first).
|
||||
_strategy_editor_wrapper = new StrategyEditorWrapper(this);
|
||||
var strategy_prop = add_property(nameof(SpreaderDef.ExecutionStrategy), mdef => mdef.ExecutionStrategy, (mdef, val) => mdef.ExecutionStrategy = val);
|
||||
var security_prop = add_property(nameof(SpreaderDef.Security), mdef => mdef.Security, (mdef, val) => mdef.Security = val, permissions: AlgoParamPermissions.None);
|
||||
var strategy_prop = add_property(nameof(SpreaderDef.ExecutionStrategy), mdef => mdef.ExecutionStrategy, (mdef, val) => mdef.ExecutionStrategy = val, out _strategy_param);
|
||||
_strategy_param.mark_committed();
|
||||
var security_prop = add_property(nameof(SpreaderDef.Security), mdef => mdef.Security, (mdef, val) => mdef.Security = val, out AlgoParamVM<Security> security_param, permissions: AlgoParamPermissions.None);
|
||||
security_param.mark_committed();
|
||||
_resources.AddRange(
|
||||
security_param.subscribe_value().Subscribe(_ => security_param.mark_committed()),
|
||||
_strategy_editor_wrapper, _template.Subscribe(vals => validate()),
|
||||
_template.Subscribe(ch => security_prop.Value = ch.newval.HasValue && ch.newval.Value.HasSecurity ? ch.newval.Value.Security.CloneToMutable() : null),
|
||||
_template.Subscribe(ch => strategy_prop.Value = ch.newval.HasValue && ch.newval.Value.HasExecutionStrategy ? ch.newval.Value.ExecutionStrategy.CloneToMutable() : null),
|
||||
@@ -57,7 +63,6 @@ namespace mbts.spark {
|
||||
base.is_expanded = value;
|
||||
}
|
||||
}
|
||||
public bool is_loaded { get; private set; }
|
||||
|
||||
protected override void validate() {
|
||||
base.validate();
|
||||
@@ -76,8 +81,8 @@ namespace mbts.spark {
|
||||
.Select(strategy => new SpreaderDefi?(model.factory.spreader_orderi(security.Value, strategy)))
|
||||
.OnDispatcher()
|
||||
.Subscribe(s => {
|
||||
is_loaded = true;
|
||||
_template.Value = s;
|
||||
_strategy_param.mark_committed();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,9 @@ namespace mbts.utils {
|
||||
readonly Func<T, IObservable<bool>> _subscribe_item;
|
||||
uint _threshold;
|
||||
|
||||
public ReactiveThresholdMonitor()
|
||||
: this(null, EqualityComparer<T>.Default) { }
|
||||
|
||||
public ReactiveThresholdMonitor(Func<T, IObservable<bool>> subscribe_item)
|
||||
: this(subscribe_item, EqualityComparer<T>.Default) { }
|
||||
|
||||
@@ -60,7 +63,10 @@ namespace mbts.utils {
|
||||
}
|
||||
|
||||
public IDisposable join(T item) =>
|
||||
new CompositeDisposable(_subscribe_item(item).Subscribe(val => {
|
||||
_subscribe_item == null ? throw new InvalidOperationException("No 'subscribe-item' delegate was specified in the constructor.") : join(item, _subscribe_item(item));
|
||||
|
||||
public IDisposable join(T item, IObservable<bool> source) =>
|
||||
new CompositeDisposable(source.Subscribe(val => {
|
||||
using (_begin_update()) {
|
||||
if (val) _filtered_items.Add(item);
|
||||
else _filtered_items.Remove(item);
|
||||
|
||||
Reference in new issue
Block a user