From 563d9730235befa3f42a477a2773ccacaa2fa023 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 30 Jan 2018 11:48:39 -0500 Subject: [PATCH] hide IRXCollection implementations --- .../Modules/Market/MarketView/MarketViewVM.cs | 2 +- .../Market/TimeAndSales/TimeAndSalesVM.cs | 2 +- .../core/GlobalExtensions/GlobalExtensions.cs | 4 +- .../spark/view/Spark/Grid/SecurityGrid.cs | 12 +- .../spark/view/Spark/Orders/OrdersModel.cs | 10 +- src/client/spark/view/Utility/RXCollection.cs | 84 ++++++++++ .../view/Utility/RXObservableCollection.cs | 157 ------------------ 7 files changed, 98 insertions(+), 173 deletions(-) create mode 100644 src/client/spark/view/Utility/RXCollection.cs delete mode 100644 src/client/spark/view/Utility/RXObservableCollection.cs diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index 88f9600fe..6dd90152c 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -10,7 +10,7 @@ namespace mbts.spark partial class MarketViewVM : SecurityMDGrid { public MarketViewVM(IBus bus, OrdersModel orders_model, SecurityCriteria security_criteria, IReadOnlyCollection sorting_criteria, FilterStates filters) - : base(new RXObservableCollection(security_criteria.subscribe(bus), SecurityDataHelper.EqualityComparer.MDComparer), sorting_criteria) { + : base(RXCollection.Create(security_criteria.subscribe(bus), SecurityDataHelper.EqualityComparer.MDComparer), sorting_criteria) { _bus = bus; _orders_model = orders_model; security_filter = new DiscreteValues(_rows_vm, r => r.security, secs => new SecurityCriteria(secs), sc => sc.securities(bus), nameof(Row.security)); diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs index ddad18777..c994b074d 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesVM.cs @@ -16,7 +16,7 @@ namespace mbts.spark { readonly IBus _bus; - public TimeAndSalesVM(IBus bus, TimeAndSalesModel model, IReadOnlyCollection sorting_criteria, FilterStates filters) : base(new RXCircularCollection(model.trades, 1000), sorting_criteria) { + public TimeAndSalesVM(IBus bus, TimeAndSalesModel model, IReadOnlyCollection sorting_criteria, FilterStates filters) : base(RXCollection.CreateBuffer(model.trades, 1000), sorting_criteria) { _bus = bus; show_ladder = DelegateCommandParamObserver.Create(current_row_observable, r => _show_ladder_async(r).ignore(), r => r != null); security_filter = new DiscreteValues(_rows_vm, r => r.security, secs => new SecurityCriteria(secs), sc => sc.securities(bus), nameof(Row.security)); diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index 2049eea5c..32b1ae795 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -198,7 +198,7 @@ namespace mbts } //this overloaded declaration, (as opposed to one function with comp = null) allows observable_collection.sync_with to be passed into functions as a parameterless Action - public static void sync_with(this ObservableCollection dest, IEnumerable src) { dest.sync_with(src, EqualityComparer.Default); } + public static void sync_with(this IList dest, IEnumerable src) { dest.sync_with(src, EqualityComparer.Default); } public static void sync_with_old(this ObservableCollection dest, IEnumerable src, IEqualityComparer comp=null) { try { /* @@ -244,7 +244,7 @@ namespace mbts throw ex; } } - public static void sync_with(this ObservableCollection dest, IEnumerable src, IEqualityComparer comp) { + public static void sync_with(this IList dest, IEnumerable src, IEqualityComparer comp) { Logger.notnull(dest); if (src == null) { dest.Clear(); diff --git a/src/client/spark/view/Spark/Grid/SecurityGrid.cs b/src/client/spark/view/Spark/Grid/SecurityGrid.cs index f9b35a7d4..437c46910 100644 --- a/src/client/spark/view/Spark/Grid/SecurityGrid.cs +++ b/src/client/spark/view/Spark/Grid/SecurityGrid.cs @@ -17,10 +17,10 @@ namespace mbts.spark abstract class SecurityGridBase : DisposableViewModelBase where TRow : class, ISecurityRow { protected SecurityGridBase(IObservable> row_models, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models), sorting_criteria) { } + : this(RXCollection.Create(row_models), sorting_criteria) { } protected SecurityGridBase(IObservable> row_models, IEqualityComparer comparer, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models, comparer), sorting_criteria) { } + : this(RXCollection.Create(row_models, comparer), sorting_criteria) { } protected SecurityGridBase(IRXCollection row_models, IEnumerable sorting_criteria) { _rows_model = row_models; @@ -56,10 +56,10 @@ namespace mbts.spark abstract class SecurityGrid : SecurityGridBase, IHandleRowSelection where TRow : class, ISecurityRow { protected SecurityGrid(IObservable> row_models, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models), sorting_criteria) { } + : this(RXCollection.Create(row_models), sorting_criteria) { } protected SecurityGrid(IObservable> row_models, IEqualityComparer comparer, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models, comparer), sorting_criteria) { } + : this(RXCollection.Create(row_models, comparer), sorting_criteria) { } protected SecurityGrid(IRXCollection row_models, IEnumerable sorting_criteria) : base(row_models, sorting_criteria) => _current_row = backing(nameof(current_row), default(TRow)); @@ -79,10 +79,10 @@ namespace mbts.spark abstract class SecurityGrid : SecurityGridBase, IHandleCellSelection where TRow : class, ISecurityRow { protected SecurityGrid(IObservable> row_models, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models), sorting_criteria) { } + : this(RXCollection.Create(row_models), sorting_criteria) { } protected SecurityGrid(IObservable> row_models, IEqualityComparer comparer, IEnumerable sorting_criteria) - : this(new RXObservableCollection(row_models, comparer), sorting_criteria) { } + : this(RXCollection.Create(row_models, comparer), sorting_criteria) { } protected SecurityGrid(IRXCollection row_models, IEnumerable sorting_criteria) : base(row_models, sorting_criteria) => _current_cell = backing(nameof(current_cell)); diff --git a/src/client/spark/view/Spark/Orders/OrdersModel.cs b/src/client/spark/view/Spark/Orders/OrdersModel.cs index bf7cd8ade..1826b968f 100644 --- a/src/client/spark/view/Spark/Orders/OrdersModel.cs +++ b/src/client/spark/view/Spark/Orders/OrdersModel.cs @@ -2,9 +2,7 @@ using bts.api.core; using bts.api.orderman2; using bts.spark; -using Google.Protobuf; using mbts.collections.extensions; -using mbts.ui; using mbts.ui.utility; using mbts.utils; using System; @@ -117,24 +115,24 @@ namespace mbts.spark { #region Get Orders Collection public static ITableUpdateCollection get_orders_collection(this OrdersModel orders_model) => - new TableUpdateCollection(orders_model.on_orders.ObserveOnDispatcher()); + RXCollection.CreateTableUpdate(orders_model.on_orders.ObserveOnDispatcher()); public static ITableUpdateCollection get_security_orders_collection(this OrdersModel orders_model, uint security) => get_security_orders_collection(orders_model, new HashSet(new[] { security })); public static ITableUpdateCollection get_security_orders_collection(this OrdersModel orders_model, HashSet securities) => - new TableUpdateCollection(orders_model.on_orders.ObserveOnDispatcher(), o => securities.Contains(o.Def.SecurityKey)); + RXCollection.CreateTableUpdate(orders_model.on_orders.ObserveOnDispatcher(), o => securities.Contains(o.Def.SecurityKey)); public static ITableUpdateCollection get_active_security_orders_collection(this OrdersModel orders_model, uint security) => get_active_security_orders_collection(orders_model, new HashSet(new[] { security })); public static ITableUpdateCollection get_active_security_orders_collection(this OrdersModel orders_model, HashSet securities) => - new TableUpdateCollection(orders_model.on_orders.ObserveOnDispatcher(), o => securities.Contains(o.Def.SecurityKey) && o.is_active()); + RXCollection.CreateTableUpdate(orders_model.on_orders.ObserveOnDispatcher(), o => securities.Contains(o.Def.SecurityKey) && o.is_active()); #endregion #region Get Fill Tuple Collection public static ITableUpdateCollection get_fill_tuple_collection(this OrdersModel orders_model) => - new TableUpdateCollection(orders_model.on_fill_tuples); + RXCollection.CreateTableUpdate(orders_model.on_fill_tuples); #endregion #region Cancel Orders diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs new file mode 100644 index 000000000..d5466c259 --- /dev/null +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -0,0 +1,84 @@ +using bts.api.core; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Linq; +using System.Reactive.Linq; + +namespace mbts.ui.utility +{ + interface IRXCollection : IList, INotifyCollectionChanged, IDisposable { } + + interface ITableUpdateCollection : IRXCollection where TKey : IEquatable { + bool TryGetValue(TKey key, out T result); + } + + static class RXCollection + { + public static IRXCollection Create(IObservable> source) => + Create>(source, (c, d) => c.sync_with(d)); + + public static IRXCollection Create(IObservable> source, IEqualityComparer comparer) => + Create>(source, (c, d) => c.sync_with(d, comparer)); + + public static IRXCollection Create(IObservable source, Action, TData> on_data) => + new AnonymousRXCollection(source, on_data); + + public static IRXCollection CreateBuffer(IObservable> source, int buffer) => + new AnonymousRXCollection>(source, (collection, ts) => { + if (ts.Count >= buffer) { + collection.Clear(); + for (int i = ts.Count - buffer; i < ts.Count; i++) + collection.Add(ts[i]); + } else { + int remaining_cap = buffer - collection.Count; + int remove = ts.Count - remaining_cap; + while (remove-- > 0) collection.RemoveAt(0); + collection.AddRange(ts); + } + }); + + public static IRXCollection CreateChunkedBuffer(IObservable>> source, int buffer) => + new AnonymousRXCollection>>(source, (collection, chunks) => { + for (int i = chunks.Count - 1; i > 0; i--) + for (int j = chunks[i].Count - 1; j > 0; j--) { + if (collection.Count >= buffer) return; + collection.Add(chunks[i][j]); + } + }); + + public static ITableUpdateCollection CreateTableUpdate(IObservable> source) where TKey : IEquatable => + CreateTableUpdate(source, _ => true); + + public static ITableUpdateCollection CreateTableUpdate(IObservable> source, Func collection_filter) where TKey : IEquatable => + new TableUpdateCollection(source, collection_filter ?? new Func(_ => true)); + + class AnonymousRXCollection : ObservableCollection, IRXCollection { + IDisposable _sub; + + public AnonymousRXCollection(IObservable source, Action, TData> on_data) => + _sub = source.ObserveOnDispatcher().Subscribe(d => on_data(this, d)); + + public void Dispose() => Disposable.dispose(ref _sub); + } + + class TableUpdateCollection : ObservableCollection, ITableUpdateCollection where TKey : IEquatable { + readonly Func _collection_filter; + IReadOnlyDictionary _raw_state = new ReadOnlyDictionary(new Dictionary()); + IDisposable _sub; + + internal TableUpdateCollection(IObservable> source, Func collection_filter) { + _collection_filter = collection_filter; + _sub = source.ObserveOnDispatcher().Subscribe(u => { + _raw_state = u.state; + foreach (T val in u.removed.Where(_collection_filter)) Remove(val); + foreach (T val in u.added.Where(_collection_filter)) Add(val); + }); + } + + public bool TryGetValue(TKey key, out T result) => _raw_state.TryGetValue(key, out result); + public void Dispose() => Disposable.dispose(ref _sub); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/RXObservableCollection.cs b/src/client/spark/view/Utility/RXObservableCollection.cs deleted file mode 100644 index 9d866797d..000000000 --- a/src/client/spark/view/Utility/RXObservableCollection.cs +++ /dev/null @@ -1,157 +0,0 @@ -using bts.api.core; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.Linq; -using System.Reactive.Linq; -using System.Threading; - -namespace mbts.ui.utility -{ - interface IRXCollection : ICollection, INotifyCollectionChanged, IDisposable { } - interface ITableUpdateCollection : IRXCollection where TKey : IEquatable { - bool TryGetValue(TKey key, out T result); - } - - public abstract class RXObservableCollectionBase : ObservableCollection, IRXCollection { - IObservable _data; - IDisposable _dispose; - - protected RXObservableCollectionBase(IObservable data) => _data = data; - - protected void connect() => _dispose = _data.Subscribe(synchronize); - protected abstract void synchronize(TData data); - public void Dispose() => Disposable.dispose(ref _dispose); - } - - public class RXObservableCollection : RXObservableCollectionBase where TCollection : IEnumerable { - protected static readonly TimeSpan DefaultPollInterval = TimeSpan.FromSeconds(1); - readonly IEqualityComparer _comparer; - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the current synchronization context - /// - public RXObservableCollection(IObservable data) - : this(data, EqualityComparer.Default) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the current synchronization context - /// - public RXObservableCollection(IObservable data, IEqualityComparer comparer) - : this(data, comparer, SynchronizationContext.Current) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the current synchronization context - /// - public RXObservableCollection(IObservable data, SynchronizationContext context) - : this(data, EqualityComparer.Default, context) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable data, IEqualityComparer comparer, SynchronizationContext context) - : this(data, comparer, context, DefaultPollInterval) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable data, IEqualityComparer comparer, TimeSpan throttle) - : this(data, comparer, SynchronizationContext.Current, throttle) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable data, SynchronizationContext context, TimeSpan throttle) - : this(data, EqualityComparer.Default, context, throttle) { } - /// - /// Initializes a new instance of RXObservableCollection<T, TCollection>, that will observe the source with the specified throttle on the specified synchronization context, - /// and synchronize with the specified IEqualityComparer<T> - /// If a throttle of TimeSpan.Zero is passed, no throttle will used. - /// - public RXObservableCollection(IObservable data, IEqualityComparer comparer, SynchronizationContext context, TimeSpan throttle) - : base(_parameterized_data(data, context, throttle)) { - _comparer = comparer ?? EqualityComparer.Default; - connect(); - } - - static IObservable _parameterized_data(IObservable data, SynchronizationContext context, TimeSpan throttle) { - if (throttle != TimeSpan.Zero) data = data.BufferLatest(throttle); - if (context != null) data = data.ObserveOn(context); - return data; - } - - protected override void synchronize(TCollection value) => this.sync_with(value, _comparer); - } - - public class RXObservableCollection : RXObservableCollection> { - /// - /// Initializes a new instance of RXObservableCollection<T>, that will observe the source with a 1 second throttle on the current synchronization context - /// - public RXObservableCollection(IObservable> data) - : base(data, SynchronizationContext.Current) { } - /// - /// Initializes a new instance of RXObservableCollection<T>, that will observe the source with a 1 second throttle on the current synchronization context - /// - public RXObservableCollection(IObservable> data, IEqualityComparer comparer) - : base(data, SynchronizationContext.Current) { } - /// - /// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable> data, SynchronizationContext context) - : base(data, context, TimeSpan.FromSeconds(1)) { } - /// - /// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable> data, IEqualityComparer comparer, SynchronizationContext context) - : base(data, context, TimeSpan.FromSeconds(1)) { } - /// - /// Initializes a new instance of RXObservableCollection, that will observe the source with the specified throttle - /// - public RXObservableCollection(IObservable> data, IEqualityComparer comparer, TimeSpan throttle) - : base(data, comparer, SynchronizationContext.Current, throttle) { } - /// - /// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context - /// - public RXObservableCollection(IObservable> data, SynchronizationContext context, TimeSpan throttle) - : base(data, context, throttle) { } - /// - /// Initializes a new instance of RXObservableCollection, that will observe the source with the specified throttle on the specified synchronization context, - /// and synchronize with the specified IEqualityComparer<T> - /// If a throttle of TimeSpan.Zero is passed, no throttle will used. - /// - public RXObservableCollection(IObservable> data, IEqualityComparer comparer, SynchronizationContext context, TimeSpan throttle) - : base(data, comparer, context, throttle) { } - } - - public class RXCircularCollection : RXObservableCollection> { - readonly int _buffer; - - public RXCircularCollection(IObservable> data, int buffer) : base(data) => _buffer = buffer; - - protected override void synchronize(IReadOnlyList ts) { - if (ts.Count >= _buffer) { - Clear(); - for (int i = ts.Count - _buffer; i < ts.Count; i++) Add(ts[i]); - } else { - int remaining_cap = _buffer - Count; - int remove = ts.Count - remaining_cap; - while (remove-- > 0) RemoveAt(0); - this.AddRange(ts); - } - } - } - - public class TableUpdateCollection : RXObservableCollectionBase>, ITableUpdateCollection where TKey : IEquatable { - readonly Func _collection_filter; - IReadOnlyDictionary _raw_state = new ReadOnlyDictionary(new Dictionary()); - - public TableUpdateCollection(IObservable> data) : this(data, null) { } - public TableUpdateCollection(IObservable> data, Predicate collection_filter) : base(data) { - _collection_filter = collection_filter == null ? _ => true : new Func(collection_filter); - connect(); - } - - protected override void synchronize(TableUpdate data) { - _raw_state = data.state; - foreach (T val in data.removed.Where(_collection_filter)) Remove(val); - foreach (T val in data.added.Where(_collection_filter)) Add(val); - } - - public bool TryGetValue(TKey key, out T result) => _raw_state.TryGetValue(key, out result); - } -} \ No newline at end of file