diff --git a/src/client/spark/core/Utility/CommonValueMonitor.cs b/src/client/spark/core/Utility/CommonValueMonitor.cs index c9759d0e9..d3e29debe 100644 --- a/src/client/spark/core/Utility/CommonValueMonitor.cs +++ b/src/client/spark/core/Utility/CommonValueMonitor.cs @@ -95,6 +95,86 @@ namespace bts.utils { } } + class CommonValueGroupMonitor where TKey : struct { + readonly IEqualityComparer _key_comparer; + readonly IEqualityComparer _value_comparer; + readonly SetMap _value_groups; + readonly Func _key; + readonly HashSet _deferred_adds, _deferred_removes; + int _deferral_count; + bool _is_deferred => _deferral_count > 0; + + public CommonValueGroupMonitor(Func key) + : this(key, EqualityComparer.Default, EqualityComparer.Default) { } + + public CommonValueGroupMonitor(Func key, IEqualityComparer key_comparer, IEqualityComparer value_comparer) { + _key = key; + _key_comparer = key_comparer; + _value_comparer = value_comparer; + _deferred_adds = new HashSet(value_comparer); + _deferred_removes = new HashSet(value_comparer); + _value_groups = new SetMap(key_comparer, value_comparer); + } + + public IReadOnlyDictionary> distinct_value_groups => _value_groups; + public KeyValuePair>? common_value_group => _value_groups.TryGetSingle(out IGroupingSet gs) ? new KeyValuePair>(gs.Key, gs) : (KeyValuePair>?)null; + + public event Action>?> common_value_group_changed; + public event Action<(IEnumerable removed, IEnumerable 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(), 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())); + } + } + } + + 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 { int _member_id_counter; readonly Dictionary add, Action remove, Func defer)> _members = new Dictionary add, Action remove, Func defer)>(); @@ -137,6 +217,22 @@ namespace bts.utils { member_mon.defer_updates)); return Disposable.Create(() => _members.Remove(id)); } + + public IDisposable add_member_mon(CommonValueGroupMonitor member_mon, Func 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 SubscribeThrottled(this CommonValueGroupMonitor monitor, TimeSpan throttle, Func, TResult> selector, TResult default_value) where TKey : struct => + Observable.Create(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, IEnumerable) _) => on_changed(); + void on_value_group_changed(KeyValuePair>? _) => 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> kv ? selector(kv.Key, kv.Value) : default_value); + }); + public static IObservable SubscribeThrottledNullable(this CommonValueMonitor monitor, TimeSpan throttle) where T : struct => monitor.SubscribeThrottled(throttle, val => val, null);