made CommonValueGroupMonitor (will be used by OrderBookVM)
This commit is contained in:
1 parent
a26b44b2cc
commit
8cd8b85022
1 file changed
+124
@@ -95,6 +95,86 @@ namespace bts.utils {
|
||||
}
|
||||
}
|
||||
|
||||
class CommonValueGroupMonitor<T, TKey> where TKey : struct {
|
||||
readonly IEqualityComparer<TKey> _key_comparer;
|
||||
readonly IEqualityComparer<T> _value_comparer;
|
||||
readonly SetMap<TKey, T> _value_groups;
|
||||
readonly Func<T, TKey> _key;
|
||||
readonly HashSet<T> _deferred_adds, _deferred_removes;
|
||||
int _deferral_count;
|
||||
bool _is_deferred => _deferral_count > 0;
|
||||
|
||||
public CommonValueGroupMonitor(Func<T, TKey> key)
|
||||
: this(key, EqualityComparer<TKey>.Default, EqualityComparer<T>.Default) { }
|
||||
|
||||
public CommonValueGroupMonitor(Func<T, TKey> key, IEqualityComparer<TKey> key_comparer, IEqualityComparer<T> value_comparer) {
|
||||
_key = key;
|
||||
_key_comparer = key_comparer;
|
||||
_value_comparer = value_comparer;
|
||||
_deferred_adds = new HashSet<T>(value_comparer);
|
||||
_deferred_removes = new HashSet<T>(value_comparer);
|
||||
_value_groups = new SetMap<TKey, T>(key_comparer, value_comparer);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<TKey, IEnumerable<T>> distinct_value_groups => _value_groups;
|
||||
public KeyValuePair<TKey, IEnumerable<T>>? common_value_group => _value_groups.TryGetSingle(out IGroupingSet<TKey, T> gs) ? new KeyValuePair<TKey, IEnumerable<T>>(gs.Key, gs) : (KeyValuePair<TKey, IEnumerable<T>>?)null;
|
||||
|
||||
public event Action<KeyValuePair<TKey, IEnumerable<T>>?> common_value_group_changed;
|
||||
public event Action<(IEnumerable<T> removed, IEnumerable<T> added)> common_values_changed;
|
||||
|
||||
public void add(T value) {
|
||||
if (_is_deferred) {
|
||||
if (!_deferred_removes.Remove(value))
|
||||
_deferred_adds.Add(value);
|
||||
} else {
|
||||
if (_value_groups.adds_key(_key(value), value)) {
|
||||
switch (_value_groups.Count) {
|
||||
case 1: common_value_group_changed?.Invoke(common_value_group); break;
|
||||
case 2: common_value_group_changed?.Invoke(null); break;
|
||||
}
|
||||
} else {
|
||||
common_values_changed?.Invoke((Enumerable.Empty<T>(), new[] { value }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(T value) {
|
||||
if (_is_deferred) {
|
||||
if (!_deferred_adds.Remove(value))
|
||||
_deferred_removes.Add(value);
|
||||
} else {
|
||||
if (_value_groups.removes_key(_key(value), value)) {
|
||||
switch (_value_groups.Count) {
|
||||
case 0: common_value_group_changed?.Invoke(null); break;
|
||||
case 1: common_value_group_changed?.Invoke(common_value_group); break;
|
||||
}
|
||||
} else {
|
||||
common_values_changed?.Invoke((new[] { value }, Enumerable.Empty<T>()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable deferr_updates() {
|
||||
var last_fired_common_key = common_value_group?.Key;
|
||||
++_deferral_count;
|
||||
return Disposable.Create(() => {
|
||||
if (--_deferral_count == 0 && (_deferred_adds.Count > 0 || _deferred_removes.Count > 0)) {
|
||||
foreach(var v in _deferred_removes)
|
||||
_value_groups.remove(_key(v), v);
|
||||
foreach (var v in _deferred_adds)
|
||||
_value_groups.add(_key(v), v);
|
||||
if (!EqualityComparer.nullable_equals(last_fired_common_key, common_value_group?.Key)) {
|
||||
common_value_group_changed?.Invoke(common_value_group);
|
||||
} else if (last_fired_common_key is TKey k) {
|
||||
common_values_changed?.Invoke((_deferred_removes.Where(v => _key_comparer.Equals(_key(v), k)), _deferred_adds.Where(v => _key_comparer.Equals(_key(v), k))));
|
||||
}
|
||||
_deferred_adds.Clear();
|
||||
_deferred_removes.Clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class CommonValueMonitorMultiplexor<TSource> {
|
||||
int _member_id_counter;
|
||||
readonly Dictionary<int, (Action<TSource> add, Action<TSource> remove, Func<IDisposable> defer)> _members = new Dictionary<int, (Action<TSource> add, Action<TSource> remove, Func<IDisposable> defer)>();
|
||||
@@ -137,6 +217,22 @@ namespace bts.utils {
|
||||
member_mon.defer_updates));
|
||||
return Disposable.Create(() => _members.Remove(id));
|
||||
}
|
||||
|
||||
public IDisposable add_member_mon<T, TKey>(CommonValueGroupMonitor<T, TKey> member_mon, Func<TSource, T> get_value) where TKey : struct {
|
||||
int id = _member_id_counter++;
|
||||
_members.Add(id, (
|
||||
v => {
|
||||
var res = get_value(v);
|
||||
member_mon.add(res);
|
||||
},
|
||||
v => {
|
||||
var res = get_value(v);
|
||||
member_mon.remove(res);
|
||||
},
|
||||
member_mon.deferr_updates
|
||||
));
|
||||
return Disposable.Create(() => _members.Remove(id));
|
||||
}
|
||||
}
|
||||
|
||||
static class CommonValueMonitorExtensions {
|
||||
@@ -210,6 +306,34 @@ namespace bts.utils {
|
||||
o.OnNext(monitor.try_get_common_value(out T result) ? selector(result) : default_value);
|
||||
});
|
||||
|
||||
public static IObservable<TResult> SubscribeThrottled<T, TKey, TResult>(this CommonValueGroupMonitor<T, TKey> monitor, TimeSpan throttle, Func<TKey, IEnumerable<T>, TResult> selector, TResult default_value) where TKey : struct =>
|
||||
Observable.Create<TResult>(o => {
|
||||
bool busy = false, dirty = false;
|
||||
notify();
|
||||
monitor.common_values_changed += on_values_changed;
|
||||
monitor.common_value_group_changed += on_value_group_changed;
|
||||
return Disposable.Create(() => {
|
||||
monitor.common_values_changed -= on_values_changed;
|
||||
monitor.common_value_group_changed -= on_value_group_changed;
|
||||
});
|
||||
void on_values_changed((IEnumerable<T>, IEnumerable<T>) _) => on_changed();
|
||||
void on_value_group_changed(KeyValuePair<TKey, IEnumerable<T>>? _) => on_changed();
|
||||
async void on_changed() {
|
||||
if (busy) {
|
||||
dirty = true;
|
||||
return;
|
||||
}
|
||||
dirty = false;
|
||||
do {
|
||||
notify();
|
||||
await Task.Delay(throttle);
|
||||
} while (dirty);
|
||||
busy = false;
|
||||
}
|
||||
void notify() =>
|
||||
o.OnNext(monitor.common_value_group is KeyValuePair<TKey, IEnumerable<T>> kv ? selector(kv.Key, kv.Value) : 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