AlgoDefinitionVM - use AlgoDefinitionData instead of api/protobuf type (CompositeAlgoDefinition/Record)
This commit is contained in:
1 parent
ad9e7660f7
commit
75e0e8e02c
2 files changed
+39
-42
No files matched your search
@@ -1,10 +1,10 @@
|
||||
using bts.api.orderman;
|
||||
using bts.utils;
|
||||
using ProtoBuf;
|
||||
using mbts.spark.utilities;
|
||||
using System;
|
||||
|
||||
namespace mbts.spark {
|
||||
struct AlgoDefinitionData {
|
||||
struct AlgoDefinitionData : IEquatable<AlgoDefinitionData> {
|
||||
public readonly CustomOrderDefinitionImmutable value;
|
||||
public readonly string name;
|
||||
public readonly AlgoPersistedKey key;
|
||||
@@ -13,5 +13,20 @@ namespace mbts.spark {
|
||||
this.name = name;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public bool Equals(AlgoDefinitionData other) =>
|
||||
key == other.key &&
|
||||
name == other.name &&
|
||||
OrderDataHelper.EqualsHelpers.custom_order_def_compareri.Equals(value, other.value);
|
||||
|
||||
public override bool Equals(object obj) => EqualityComparer.equals_obj(this, obj);
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(
|
||||
key.GetHashCode(),
|
||||
EqualityComparer.safe_get_hash_code(name),
|
||||
value.GetHashCode());
|
||||
|
||||
public static bool operator ==(AlgoDefinitionData a, AlgoDefinitionData b) => a.Equals(b);
|
||||
public static bool operator !=(AlgoDefinitionData a, AlgoDefinitionData b) => !a.Equals(b);
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,8 @@ namespace mbts.spark {
|
||||
public const string new_algo_name = "New Algo";
|
||||
readonly OrdersModel _model;
|
||||
readonly ObservableKeyedCollection<AlgoDefinitionTemplateVM, AlgoVMKey> _templates = new ObservableKeyedCollection<AlgoDefinitionTemplateVM, AlgoVMKey>(t => t.vm_key);
|
||||
readonly EditStack<CompositeAlgoDefinitionImmutable> _edit_stack = new EditStack<CompositeAlgoDefinitionImmutable>(OrderDataHelper.EqualsHelpers.algo_def_compareri);
|
||||
readonly VersionContainer<CompositeAlgoDefinitionImmutable> _version;
|
||||
readonly EditStack<AlgoDefinitionData> _edit_stack = new EditStack<AlgoDefinitionData>();
|
||||
readonly VersionContainer<AlgoDefinitionData> _version;
|
||||
readonly Property<bool> _can_edit_structure, _is_expanded, _is_selected;
|
||||
readonly Property<IOrderTypeVM> _algo_root;
|
||||
readonly Property<IOrderTypeVM> _visible_algo_root;
|
||||
@@ -33,7 +33,7 @@ namespace mbts.spark {
|
||||
readonly ReactiveThresholdMonitor<AlgoDefinitionTemplateVM> _any_templates_dirty_monitor = new ReactiveThresholdMonitor<AlgoDefinitionTemplateVM>(t => t.observe_is_dirty());
|
||||
OrderTypeEditingPermissions _editing_permissions = _get_permissions(0);
|
||||
string _name;
|
||||
CompositeAlgoDefinitionImmutable? _committed;
|
||||
AlgoDefinitionData? _committed;
|
||||
AlgoDefinitionMetadata _metadata;
|
||||
TableUpdateBuffer<AlgoTemplateData, AlgoPersistedKey> _template_update_buffer;
|
||||
bool _connected;
|
||||
@@ -73,7 +73,7 @@ namespace mbts.spark {
|
||||
resources.Add(_selected_template.observe_current(true).Subscribe(t => _visible_algo_root.set(t.algo_root)));
|
||||
}
|
||||
_can_edit_structure = backing(nameof(can_edit_structure), templates.ObserveSelf(), _ => templates.Count == 0);
|
||||
_version = new VersionContainer<CompositeAlgoDefinitionImmutable>(() => new CompositeAlgoDefinitionImmutable(_create_algo_def()));
|
||||
_version = new VersionContainer<AlgoDefinitionData>(() => create_algo_def());
|
||||
_committed = _version.value;
|
||||
_edit_stack_push = new DeferrableActionWrapper(() => {
|
||||
if (algo_root != null)
|
||||
@@ -204,7 +204,13 @@ namespace mbts.spark {
|
||||
|
||||
public void undo() => _restore(_edit_stack.try_undo);
|
||||
public void redo() => _restore(_edit_stack.try_redo);
|
||||
public void load_committed() => _load_from(_committed.Value.definition_data());
|
||||
public void load_committed() {
|
||||
using (_edit_stack_push.defer_execution())
|
||||
_load_definition_data(_committed.Value);
|
||||
_version.increment_version();
|
||||
_committed = _version.value;
|
||||
_is_dirty.refresh();
|
||||
}
|
||||
/// <summary>
|
||||
/// Subscribes to the templates source for this definition if it has been persisted (subscription requires a persistant id); does nothing if this definition has not been persisted, or if it is already subscribed (idempotent).
|
||||
/// </summary>
|
||||
@@ -242,12 +248,6 @@ namespace mbts.spark {
|
||||
public AlgoDefinitionVM to_persisted(AlgoDefinitionData def) =>
|
||||
new AlgoDefinitionVM(_model, new AlgoVMKey(def.key), new NamedCustomOrderDefinition(def.value), security_md_source, def.name);
|
||||
|
||||
public CompositeAlgoRecordImmutable create_record() {
|
||||
var mrecord = new CompositeAlgoRecord { AlgoDefinition = _create_algo_def() };
|
||||
mrecord.set_key(vm_key.persisted_id);
|
||||
return new CompositeAlgoRecordImmutable(mrecord);
|
||||
}
|
||||
|
||||
public AlgoDefinitionData create_algo_def() =>
|
||||
new AlgoDefinitionData(new CustomOrderDefinitionImmutable(algo_root.create()), name, vm_key.persisted_id);
|
||||
|
||||
@@ -265,21 +265,10 @@ namespace mbts.spark {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
CompositeAlgoDefinition _create_algo_def() =>
|
||||
new CompositeAlgoDefinition {
|
||||
Algo = algo_root.create(),
|
||||
Name = name
|
||||
};
|
||||
|
||||
void _load_from(AlgoDefinitionData data) {
|
||||
using (_edit_stack_push.defer_execution()) {
|
||||
name = data.name;
|
||||
algo_root = algo_roots.FirstOrDefault(d => d.order_type == data.value.CustomCase, _mk_algo_root(data.value.CustomCase));
|
||||
algo_root.load_from(data.value);
|
||||
}
|
||||
_version.increment_version();
|
||||
_committed = _version.value;
|
||||
_is_dirty.refresh();
|
||||
void _load_definition_data(AlgoDefinitionData data) {
|
||||
name = data.name;
|
||||
algo_root = algo_roots.FirstOrDefault(d => d.order_type == data.value.CustomCase, _mk_algo_root(data.value.CustomCase));
|
||||
algo_root.load_from(data.value);
|
||||
}
|
||||
|
||||
IOrderTypeVM _mk_algo_root(CustomOrderDefinition.CustomOneofCase t) =>
|
||||
@@ -316,24 +305,17 @@ namespace mbts.spark {
|
||||
_can_redo.refresh();
|
||||
}
|
||||
|
||||
void _restore(TryGetValue<CompositeAlgoDefinitionImmutable> try_get_snapshot) {
|
||||
while (try_get_snapshot(out CompositeAlgoDefinitionImmutable next_state)) {
|
||||
void _restore(TryGetValue<AlgoDefinitionData> try_get_snapshot) {
|
||||
while (try_get_snapshot(out AlgoDefinitionData next_state)) {
|
||||
if (!_version.value.Equals(next_state)) {
|
||||
_restore(next_state);
|
||||
using (_edit_stack_push.supress_execution())
|
||||
_load_definition_data(next_state);
|
||||
_version.increment_version();
|
||||
_is_dirty.refresh();
|
||||
break;
|
||||
}
|
||||
}
|
||||
_refresh_undo_redo();
|
||||
}
|
||||
|
||||
void _restore(CompositeAlgoDefinitionImmutable cadi) {
|
||||
using (_edit_stack_push.supress_execution()) {
|
||||
name = cadi.Name;
|
||||
algo_root = algo_roots.FirstOrDefault(a => a.order_type == cadi.Algo.CustomCase);
|
||||
algo_root?.load_from(cadi.Algo);
|
||||
}
|
||||
_version.increment_version();
|
||||
_is_dirty.refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user