OrderBook - refactor 'property-update-throttle' code to use CommonValueMonitor
This commit is contained in:
1 parent
5d0023a80b
commit
4e85bf4520
2 files changed
+271
-113
No files matched your search
@@ -83,21 +83,18 @@ namespace mbts.spark {
|
||||
toggle_fills_slave_grid_is_visible_cmd = new DelegateCommand(() => fills_slave_grid_is_visible = !fills_slave_grid_is_visible);
|
||||
grid_settings_manager = _create_grid_settings_manager(owner_title, bus, notification_service, model, rows, core_columns);
|
||||
IDisposable attach_vm_hooks = null;
|
||||
_selected_algo_mon.common_algo_changed += on_common_selected_algo_changed;
|
||||
_selected_algo_mon.common_sec_md_changed += on_common_selected_sec_md_changed;
|
||||
_selected_algo_mon.common_price_changed += on_common_price_changed;
|
||||
_selected_algo_mon.common_size_changed += on_common_size_changed;
|
||||
_selected_algo_mon.all_active_changed += on_all_selected_orders_active_changed;
|
||||
begin_attaching += on_begin_attaching;
|
||||
end_attaching += on_end_attaching;
|
||||
concat_disposable(
|
||||
_selected_algo_mon.subscribe_common_algo().Subscribe(on_common_selected_algo_changed),
|
||||
_selected_algo_mon.subscribe_common_sec_md().Subscribe(on_common_selected_sec_md_changed),
|
||||
_selected_algo_mon.subscribe_price().Subscribe(on_common_price_changed),
|
||||
_selected_algo_mon.subscribe_size().Subscribe(on_common_size_changed),
|
||||
_selected_algo_mon.subscribe_all_active().Subscribe(on_all_selected_orders_active_changed),
|
||||
model.on_order_tuples.OnDispatcher().Subscribe(_row_collection.update),
|
||||
grid_settings_manager,
|
||||
order_mod_vm.algo_vm.connect_data(Observable.Return(model)),
|
||||
order_mod_vm.algo_vm.connect_data(Observable.FromEvent<PropertyChange<SecurityImmutable?>, SecurityImmutable?>(
|
||||
cb => (oldval, newval) => cb(newval),
|
||||
cb => _selected_algo_mon.common_spreader_sec_changed += cb,
|
||||
cb => _selected_algo_mon.common_spreader_sec_changed -= cb)),
|
||||
order_mod_vm.algo_vm.connect_data(_selected_algo_mon.subscribe_common_spreader_sec()),
|
||||
order_mod_vm.algo_vm,
|
||||
bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(_selected_algo_mon.on_security_md_map),
|
||||
attach_vm,
|
||||
@@ -111,11 +108,6 @@ namespace mbts.spark {
|
||||
Disposable.Create(() => {
|
||||
_columns.filter_changed -= on_filter_changed;
|
||||
_row_collection.CollectionChanged -= _row_collection_CollectionChanged;
|
||||
_selected_algo_mon.common_algo_changed -= on_common_selected_algo_changed;
|
||||
_selected_algo_mon.common_sec_md_changed -= on_common_selected_sec_md_changed;
|
||||
_selected_algo_mon.common_price_changed += on_common_price_changed;
|
||||
_selected_algo_mon.common_size_changed += on_common_size_changed;
|
||||
_selected_algo_mon.all_active_changed -= on_all_selected_orders_active_changed;
|
||||
begin_attaching -= on_begin_attaching;
|
||||
end_attaching -= on_end_attaching;
|
||||
Disposable.dispose(ref attach_vm_hooks);
|
||||
@@ -134,7 +126,7 @@ namespace mbts.spark {
|
||||
return ft => keys.Contains(ft.fill.OrderKey);
|
||||
}
|
||||
void on_filter_changed() => _row_collection.filter = _columns.get_filter();
|
||||
void on_common_selected_algo_changed(CustomOrderDefinitionImmutable? oldval, CustomOrderDefinitionImmutable? newval) {
|
||||
void on_common_selected_algo_changed(CustomOrderDefinitionImmutable? newval) {
|
||||
if (newval.HasValue) {
|
||||
order_mod_vm.algo_vm.load_def(newval.Value);
|
||||
} else {
|
||||
@@ -142,11 +134,11 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
void on_common_selected_sec_md_changed(SecurityMD? oldval, SecurityMD? newval) => order_mod_vm.security_md = newval;
|
||||
void on_common_price_changed(double oldval, double newval) => order_mod_vm.price = newval;
|
||||
void on_common_size_changed(uint? oldval, uint? newval) => order_mod_vm.size_calculator.value = newval;
|
||||
void on_common_selected_sec_md_changed(SecurityMD? newval) => order_mod_vm.security_md = newval;
|
||||
void on_common_price_changed(double newval) => order_mod_vm.price = newval;
|
||||
void on_common_size_changed(uint? newval) => order_mod_vm.size_calculator.value = newval;
|
||||
|
||||
void on_all_selected_orders_active_changed(bool oldval, bool newval) {
|
||||
void on_all_selected_orders_active_changed(bool newval) {
|
||||
order_mod_vm.algo_vm.editing_permissions_mask = newval ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None;
|
||||
modify_selected_algos_async_cmd.RaiseCanExecuteChanged();
|
||||
send_attached_orders_async_cmd.RaiseCanExecuteChanged();
|
||||
@@ -161,17 +153,8 @@ namespace mbts.spark {
|
||||
await Task.WhenAll(order_tups.Select(t => _model.send_order(t.order.Def.with(attach_target_order_key: t.order.Key, custom_def: algo), price, size)));
|
||||
}
|
||||
|
||||
void on_begin_attaching() {
|
||||
Disposable.dispose(ref attach_vm_hooks);
|
||||
attach_vm.security_md = _selected_algo_mon.common_sec_md;
|
||||
_selected_algo_mon.common_sec_md_changed += on_security_md_changed;
|
||||
attach_vm_hooks = Disposable.Create(() => _selected_algo_mon.common_sec_md_changed -= on_security_md_changed);
|
||||
void on_security_md_changed(SecurityMD? oldval, SecurityMD? newval) =>
|
||||
attach_vm.security_md = newval;
|
||||
}
|
||||
|
||||
void on_end_attaching() =>
|
||||
Disposable.dispose(ref attach_vm_hooks);
|
||||
void on_begin_attaching() => Disposable.replace(ref attach_vm_hooks, _selected_algo_mon.subscribe_common_sec_md().Subscribe(smd => attach_vm.security_md = smd));
|
||||
void on_end_attaching() => Disposable.dispose(ref attach_vm_hooks);
|
||||
}
|
||||
|
||||
private void _row_collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
@@ -254,22 +237,20 @@ namespace mbts.spark {
|
||||
|
||||
public void row_selection_changed(SelectionChangedEventArgs e) {
|
||||
try {
|
||||
using (_selected_algo_mon.update()) {
|
||||
if (e.RemovedItems != null) {
|
||||
foreach (var row_vm in e.RemovedItems.Cast<OrderBookRowVM>()) {
|
||||
row_vm.is_selected = false;
|
||||
_selected_algo_mon.on_row_unselected(row_vm);
|
||||
}
|
||||
if (e.RemovedItems != null) {
|
||||
foreach (var row_vm in e.RemovedItems.Cast<OrderBookRowVM>()) {
|
||||
row_vm.is_selected = false;
|
||||
_selected_algo_mon.on_row_unselected(row_vm);
|
||||
}
|
||||
if (e.AddedItems != null) {
|
||||
foreach (var row_vm in e.AddedItems.Cast<OrderBookRowVM>()) {
|
||||
row_vm.is_selected = true;
|
||||
_selected_algo_mon.on_row_selected(row_vm);
|
||||
}
|
||||
}
|
||||
RowSelectionHelper.apply_changes(selected_rows, e);
|
||||
selected_order_changed?.Invoke(current_order);
|
||||
}
|
||||
if (e.AddedItems != null) {
|
||||
foreach (var row_vm in e.AddedItems.Cast<OrderBookRowVM>()) {
|
||||
row_vm.is_selected = true;
|
||||
_selected_algo_mon.on_row_selected(row_vm);
|
||||
}
|
||||
}
|
||||
RowSelectionHelper.apply_changes(selected_rows, e);
|
||||
selected_order_changed?.Invoke(current_order);
|
||||
} catch(Exception ex) {
|
||||
// [John]I observed a strange null-reference exception in this callback when the launcher was shutting down (see https://bluetradesys.atlassian.net/browse/SPARK-2089)
|
||||
Log.Ui.error($"[{nameof(OrderBookVM)}] Error handling selection change: {ex}");
|
||||
@@ -551,104 +532,119 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Monitors selected rows, and determines whether or not they are all active-orders, with a common algo structure.
|
||||
/// When all selected rows satisfy this condition, <see cref="common_algo"/> will be non-null, and its value may be used to populate algo-editor panel on the right side of the window.
|
||||
/// In terms of user-interaction, this means the user may edit the algo params for multiple orders at once if they are all active and share a common algo structure.
|
||||
/// Monitors selected rows, and determines whether or not they are all active-orders, with common parameters.
|
||||
/// </summary>
|
||||
class SelectedAlgosMonitor {
|
||||
static readonly TimeSpan _throttle = TimeSpan.FromSeconds(0.1);
|
||||
readonly PropertyUpdateThrottle<CustomOrderDefinitionImmutable?> _common_algo = new PropertyUpdateThrottle<CustomOrderDefinitionImmutable?>(_throttle);
|
||||
readonly PropertyUpdateThrottle<SecurityImmutable?> _common_spreader_sec = new PropertyUpdateThrottle<SecurityImmutable?>(_throttle);
|
||||
readonly PropertyUpdateThrottle<SecurityImmutable?> _common_sec = new PropertyUpdateThrottle<SecurityImmutable?>(_throttle);
|
||||
readonly PropertyUpdateThrottle<SecurityMD?> _common_sec_md = new PropertyUpdateThrottle<SecurityMD?>(_throttle);
|
||||
readonly PropertyUpdateThrottle<double> _common_price = new PropertyUpdateThrottle<double>(_throttle);
|
||||
readonly PropertyUpdateThrottle<uint?> _common_size = new PropertyUpdateThrottle<uint?>(_throttle);
|
||||
readonly PropertyUpdateThrottle<bool> _all_active = new PropertyUpdateThrottle<bool>(_throttle);
|
||||
readonly SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable> _algo_structure_index = new SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable>();
|
||||
readonly SetMap<double, uint> _price_order_map = new SetMap<double, uint>();
|
||||
readonly SetMap<uint, uint> _size_order_map = new SetMap<uint, uint>();
|
||||
readonly SetMap<SecurityImmutable?, uint> _sec_order_map = new SetMap<SecurityImmutable?, uint>();
|
||||
readonly CommonValueMonitor<AlgoSecurityKey> _algo_mon = new CommonValueMonitor<AlgoSecurityKey>();
|
||||
readonly CommonValueMonitor<SecurityImmutable?> _spreader_sec_mon = new CommonValueMonitor<SecurityImmutable?>();
|
||||
readonly CommonValueMonitor<SecurityImmutable?> _sec_mon = new CommonValueMonitor<SecurityImmutable?>();
|
||||
readonly CommonValueMonitor<double> _price_mon = new CommonValueMonitor<double>(double.NaN);
|
||||
readonly CommonValueMonitor<uint> _size_mon = new CommonValueMonitor<uint>();
|
||||
readonly CommonValueMonitor<AccountTuple> _account_mon = new CommonValueMonitor<AccountTuple>();
|
||||
readonly CommonValueMonitor<bool> _active_mon = new CommonValueMonitor<bool>();
|
||||
readonly CommonValueMonitorMultiplexor<OrderSecurityAlgoMeta> _group_mon = new CommonValueMonitorMultiplexor<OrderSecurityAlgoMeta>();
|
||||
readonly DynamicOrderHelper _dyn_helper;
|
||||
IReadOnlyDictionary<uint, SecurityMD> _security_md_map = DictionaryHelper.empty<uint, SecurityMD>();
|
||||
int _inactive_order_count;
|
||||
|
||||
public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) =>
|
||||
public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) {
|
||||
_dyn_helper = dyn_helper;
|
||||
_group_mon.add_member_mon(_algo_mon, _algo_key);
|
||||
_group_mon.add_member_mon(_spreader_sec_mon, _get_spreader_sec, s => s.HasValue);
|
||||
_group_mon.add_member_mon(_sec_mon, osam => osam.security, s => s.HasValue);
|
||||
_group_mon.add_member_mon(_price_mon, osam => osam.order.price());
|
||||
_group_mon.add_member_mon(_size_mon, osam => osam.order.quantity());
|
||||
_group_mon.add_member_mon(_account_mon, osam => new AccountTuple(osam.order.account_id(), osam.order.account()));
|
||||
_group_mon.add_member_mon(_active_mon, osam => osam.order.is_active());
|
||||
}
|
||||
|
||||
public event PropertyChange<CustomOrderDefinitionImmutable?> common_algo_changed { add => _common_algo.changed += value; remove => _common_algo.changed -= value; }
|
||||
public event PropertyChange<SecurityImmutable?> common_sec_changed { add => _common_sec.changed += value; remove => _common_sec.changed -= value; }
|
||||
public event PropertyChange<SecurityMD?> common_sec_md_changed { add => _common_sec_md.changed += value; remove => _common_sec_md.changed -= value; }
|
||||
public event PropertyChange<SecurityImmutable?> common_spreader_sec_changed { add => _common_spreader_sec.changed += value; remove => _common_spreader_sec.changed -= value; }
|
||||
public event PropertyChange<double> common_price_changed { add => _common_price.changed += value; remove => _common_price.changed -= value; }
|
||||
public event PropertyChange<uint?> common_size_changed { add => _common_size.changed += value; remove => _common_size.changed -= value; }
|
||||
public event PropertyChange<bool> all_active_changed { add => _all_active.changed += value; remove => _all_active.changed -= value; }
|
||||
event Action _security_md_map_changed;
|
||||
|
||||
public CustomOrderDefinitionImmutable? common_algo => _algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null;
|
||||
public SecurityImmutable? common_sec => _sec_order_map.TryGetSingle(out IGroupingSet<SecurityImmutable?, uint> gs) ? gs.Key : null;
|
||||
public IObservable<CustomOrderDefinitionImmutable?> subscribe_common_algo() => _algo_mon.SubscribeThrottledNullable(_throttle).Select(ask => ask.HasValue ? ask.Value.algo: (CustomOrderDefinitionImmutable?)null);
|
||||
public IObservable<SecurityImmutable?> subscribe_common_spreader_sec() => _spreader_sec_mon.SubscribeThrottled(_throttle);
|
||||
public IObservable<SecurityImmutable?> subscribe_common_sec() => _sec_mon.SubscribeThrottled(_throttle);
|
||||
public IObservable<SecurityMD?> subscribe_common_sec_md() =>
|
||||
Observable.Create<SecurityMD?>(o => {
|
||||
SecurityMD? last_value = null;
|
||||
_sec_mon.common_value_changed += on_common_sec_changed;
|
||||
_security_md_map_changed += refresh;
|
||||
return Disposable.Create(() => {
|
||||
_sec_mon.common_value_changed -= on_common_sec_changed;
|
||||
_security_md_map_changed -= refresh;
|
||||
});
|
||||
void on_common_sec_changed(SecurityImmutable? _) => refresh();
|
||||
void refresh() {
|
||||
var curr = common_sec_md;
|
||||
if (curr != last_value)
|
||||
o.OnNext(last_value = curr);
|
||||
}
|
||||
});
|
||||
public IObservable<double> subscribe_price() => _price_mon.SubscribeThrottled(_throttle);
|
||||
public IObservable<uint?> subscribe_size() => _size_mon.SubscribeThrottledNullable(_throttle);
|
||||
public IObservable<AccountTuple?> subscribe_account() => _account_mon.SubscribeThrottledNullable(_throttle);
|
||||
public IObservable<bool> subscribe_all_active() => _active_mon.SubscribeThrottled(_throttle);
|
||||
|
||||
public CustomOrderDefinitionImmutable? common_algo => _algo_mon.common_value_nullable()?.algo;
|
||||
public SecurityImmutable? common_sec => _sec_mon.common_value;
|
||||
public SecurityMD? common_sec_md => common_sec is SecurityImmutable sec && _security_md_map.TryGetValue(sec.Id, out SecurityMD smd) ? smd : (SecurityMD?)null;
|
||||
public SecurityImmutable? common_spreader_sec => _algo_structure_index.Count == 1 ? _algo_structure_index.First().Key.spreader_sec : null;
|
||||
public double common_price => _price_order_map.TryGetSingle(out IGroupingSet<double, uint> gs) ? gs.Key : double.NaN;
|
||||
public uint? common_size => _size_order_map.TryGetSingle(out IGroupingSet<uint, uint> gs) ? gs.Key : (uint?)null;
|
||||
public bool all_active => _inactive_order_count == 0 && _algo_structure_index.Count > 0;
|
||||
public SecurityImmutable? common_spreader_sec => _spreader_sec_mon.common_value;
|
||||
public double common_price => _price_mon.try_get_common_value(out double p) ? p : double.NaN;
|
||||
public uint? common_size => _size_mon.common_value_nullable();
|
||||
public AccountTuple? common_account => _account_mon.common_value_nullable();
|
||||
public bool all_active => _active_mon.common_value;
|
||||
|
||||
public void on_row_selected(OrderBookRowVM row) {
|
||||
_select_algo(row.order_tuple);
|
||||
_group_mon.add(row.order_tuple);
|
||||
row.updated += _on_row_updated;
|
||||
}
|
||||
|
||||
public void on_row_unselected(OrderBookRowVM row) {
|
||||
_unselect_algo(row.order_tuple);
|
||||
_group_mon.remove(row.order_tuple);
|
||||
row.updated -= _on_row_updated;
|
||||
}
|
||||
|
||||
public void on_security_md_map(IReadOnlyDictionary<uint, SecurityMD> security_md_map) {
|
||||
using (update())
|
||||
_security_md_map = security_md_map;
|
||||
}
|
||||
|
||||
public IDisposable update() {
|
||||
var old_algo = common_algo;
|
||||
bool old_all_active = all_active;
|
||||
return Disposable.Create(() => {
|
||||
_common_algo.value = common_algo;
|
||||
_common_sec.value = common_sec;
|
||||
_common_sec_md.value = common_sec_md;
|
||||
_common_spreader_sec.value = common_spreader_sec;
|
||||
_common_price.value = common_price;
|
||||
_common_size.value = common_size;
|
||||
_all_active.value = all_active;
|
||||
});
|
||||
_security_md_map = security_md_map;
|
||||
_security_md_map_changed?.Invoke();
|
||||
}
|
||||
|
||||
void _on_row_updated((OrderSecurityAlgoMeta oldval, OrderSecurityAlgoMeta newval) ch) {
|
||||
using (update()) {
|
||||
_unselect_algo(ch.oldval);
|
||||
_select_algo(ch.newval);
|
||||
}
|
||||
}
|
||||
|
||||
void _select_algo(OrderSecurityAlgoMeta osam) {
|
||||
_algo_structure_index.add((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order);
|
||||
_sec_order_map.add(osam.security, osam.order.Key);
|
||||
_price_order_map.add(osam.order.price(), osam.order.Key);
|
||||
_size_order_map.add(osam.order.quantity(), osam.order.Key);
|
||||
if (!osam.order.is_active())
|
||||
_inactive_order_count++;
|
||||
}
|
||||
|
||||
void _unselect_algo(OrderSecurityAlgoMeta osam) {
|
||||
_algo_structure_index.remove((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order);
|
||||
_sec_order_map.remove(osam.security, osam.order.Key);
|
||||
_price_order_map.remove(osam.order.price(), osam.order.Key);
|
||||
_size_order_map.remove(osam.order.quantity(), osam.order.Key);
|
||||
if (!osam.order.is_active())
|
||||
_inactive_order_count--;
|
||||
_group_mon.remove(ch.oldval);
|
||||
_group_mon.add(ch.newval);
|
||||
}
|
||||
|
||||
static SecurityImmutable? _get_spreader_sec(OrderSecurityAlgoMeta osam) =>
|
||||
OrderDataHelper.enumerate_algo_hierarchy(osam.order.Def.CustomDef.CloneToMutable())
|
||||
.Any(algo => algo.CustomCase == CustomOrderDefinition.CustomOneofCase.Spreader) ?
|
||||
osam.security : null;
|
||||
|
||||
AlgoSecurityKey _algo_key(OrderSecurityAlgoMeta osam) =>
|
||||
new AlgoSecurityKey(osam.order.Def.CustomDef, _get_spreader_sec(osam), AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index));
|
||||
|
||||
SecurityMD? _get_sec_md(OrderSecurityAlgoMeta osam) =>
|
||||
osam.security.HasValue && _security_md_map.TryGetValue(osam.security.Value.Id, out SecurityMD smd) ? smd : (SecurityMD?)null;
|
||||
|
||||
struct AlgoSecurityKey {
|
||||
public readonly CustomOrderDefinitionImmutable algo;
|
||||
public readonly SecurityImmutable? security;
|
||||
public readonly AlgoStructureKey key;
|
||||
|
||||
public AlgoSecurityKey(CustomOrderDefinitionImmutable algo, SecurityImmutable? security, AlgoStructureKey key) {
|
||||
this.algo = algo;
|
||||
this.security = security;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public bool Equals(AlgoSecurityKey other) =>
|
||||
security == other.security &&
|
||||
key == other.key;
|
||||
|
||||
public override bool Equals(object obj) => obj is AlgoSecurityKey other && Equals(other);
|
||||
public override int GetHashCode() => key.GetHashCode();
|
||||
|
||||
public static bool operator ==(AlgoSecurityKey a, AlgoSecurityKey b) => a.Equals(b);
|
||||
public static bool operator !=(AlgoSecurityKey a, AlgoSecurityKey b) => !a.Equals(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
using mbts.collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bts.utils {
|
||||
class CommonValueMonitor<T> {
|
||||
readonly RefCountMap<T> _values;
|
||||
T _default_value;
|
||||
|
||||
public CommonValueMonitor()
|
||||
: this(default, EqualityComparer<T>.Default) { }
|
||||
|
||||
public CommonValueMonitor(T default_value)
|
||||
: this(default_value, EqualityComparer<T>.Default) { }
|
||||
|
||||
public CommonValueMonitor(T default_value, IEqualityComparer<T> comparer) {
|
||||
_default_value = default_value;
|
||||
_values = new RefCountMap<T>(comparer);
|
||||
}
|
||||
|
||||
public event Action<T> common_value_changed;
|
||||
|
||||
public ICollection<T> distinct_values => _values.keys;
|
||||
public T common_value => _values.keys.SafeSingleOrDefault();
|
||||
|
||||
public void add(T value) {
|
||||
if (_values.increment_adds_key(value)) {
|
||||
switch (_values.Count) {
|
||||
case 1: common_value_changed?.Invoke(value); break;
|
||||
case 2: common_value_changed?.Invoke(_default_value); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(T value) {
|
||||
if (_values.decrement_removes_key(value)) {
|
||||
switch (_values.Count) {
|
||||
case 0: common_value_changed?.Invoke(_default_value); break;
|
||||
case 1: common_value_changed?.Invoke(_values.keys.First()); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CommonValueMonitorMultiplexor<TSource> {
|
||||
int _member_id_counter;
|
||||
readonly Dictionary<int, (Action<TSource> add, Action<TSource> remove)> _members = new Dictionary<int, (Action<TSource> add, Action<TSource> remove)>();
|
||||
|
||||
public void add(TSource value) {
|
||||
foreach ((var add, var remove) in _members.Values)
|
||||
add(value);
|
||||
}
|
||||
|
||||
public void remove(TSource value) {
|
||||
foreach ((var add, var remove) in _members.Values)
|
||||
remove(value);
|
||||
}
|
||||
|
||||
public IDisposable add_member_mon<T>(CommonValueMonitor<T> member_mon, Func<TSource, T> get_value) {
|
||||
int id = _member_id_counter++;
|
||||
_members.Add(id, (
|
||||
v => member_mon.add(get_value(v)),
|
||||
v => member_mon.remove(get_value(v))));
|
||||
return Disposable.Create(() => _members.Remove(id));
|
||||
}
|
||||
|
||||
public IDisposable add_member_mon<T>(CommonValueMonitor<T> member_mon, Func<TSource, T> get_value, Func<T, bool> where) {
|
||||
int id = _member_id_counter++;
|
||||
_members.Add(id, (
|
||||
v => {
|
||||
var res = get_value(v);
|
||||
if (where(res))
|
||||
member_mon.add(res);
|
||||
},
|
||||
v => {
|
||||
var res = get_value(v);
|
||||
if (where(res))
|
||||
member_mon.remove(res);
|
||||
}));
|
||||
return Disposable.Create(() => _members.Remove(id));
|
||||
}
|
||||
}
|
||||
|
||||
static class CommonValueMonitorExtensions {
|
||||
public static bool try_get_common_value<T>(this CommonValueMonitor<T> monitor, out T result) =>
|
||||
monitor.distinct_values.TryGetSingle(out result);
|
||||
|
||||
public static T? common_value_nullable<T>(this CommonValueMonitor<T> monitor) where T : struct =>
|
||||
monitor.try_get_common_value(out T result) ? result : (T?)null;
|
||||
|
||||
public static IObservable<T> SubscribeThrottled<T>(this CommonValueMonitor<T> monitor, TimeSpan throttle) =>
|
||||
Observable.Create<T>(o => {
|
||||
bool busy = false, dirty = false;
|
||||
o.OnNext(monitor.common_value);
|
||||
monitor.common_value_changed += on_changed;
|
||||
return Disposable.Create(() => monitor.common_value_changed -= on_changed);
|
||||
async void on_changed(T _) {
|
||||
if (busy) {
|
||||
dirty = true;
|
||||
return;
|
||||
}
|
||||
dirty = false;
|
||||
do {
|
||||
o.OnNext(monitor.common_value);
|
||||
await Task.Delay(throttle);
|
||||
} while (dirty);
|
||||
busy = false;
|
||||
}
|
||||
});
|
||||
|
||||
public static IObservable<T> SubscribeThrottled<T>(this CommonValueMonitor<T> monitor, TimeSpan throttle, T default_value) =>
|
||||
Observable.Create<T>(o => {
|
||||
bool busy = false, dirty = false;
|
||||
notify();
|
||||
monitor.common_value_changed += on_changed;
|
||||
return Disposable.Create(() => monitor.common_value_changed -= on_changed);
|
||||
async void on_changed(T _) {
|
||||
if (busy) {
|
||||
dirty = true;
|
||||
return;
|
||||
}
|
||||
dirty = false;
|
||||
do {
|
||||
notify();
|
||||
await Task.Delay(throttle);
|
||||
} while (dirty);
|
||||
busy = false;
|
||||
}
|
||||
void notify() =>
|
||||
o.OnNext(monitor.try_get_common_value(out T result) ? result : default_value);
|
||||
});
|
||||
|
||||
public static IObservable<TResult> SubscribeThrottled<T, TResult>(this CommonValueMonitor<T> monitor, TimeSpan throttle, Func<T, TResult> selector, TResult default_value) =>
|
||||
Observable.Create<TResult>(o => {
|
||||
bool busy = false, dirty = false;
|
||||
notify();
|
||||
monitor.common_value_changed += on_changed;
|
||||
return Disposable.Create(() => monitor.common_value_changed -= on_changed);
|
||||
async void on_changed(T _) {
|
||||
if (busy) {
|
||||
dirty = true;
|
||||
return;
|
||||
}
|
||||
dirty = false;
|
||||
do {
|
||||
notify();
|
||||
await Task.Delay(throttle);
|
||||
} while (dirty);
|
||||
busy = false;
|
||||
}
|
||||
void notify() =>
|
||||
o.OnNext(monitor.try_get_common_value(out T result) ? selector(result) : default_value);
|
||||
});
|
||||
|
||||
public static IObservable<T?> SubscribeThrottledNullable<T>(this CommonValueMonitor<T> monitor, TimeSpan throttle) where T : struct =>
|
||||
monitor.SubscribeThrottled<T, T?>(throttle, val => val, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user