hide IRXCollection implementations

This commit is contained in:
John Colvin committed 2018-01-30 17:26:07 -05:00
1 parent b13c55ed28
commit 563d973023
7 files changed
+98 -173

No files matched your search

@@ -10,7 +10,7 @@ namespace mbts.spark
partial class MarketViewVM : SecurityMDGrid<MarketViewVM.Row, SecurityMD>
{
public MarketViewVM(IBus bus, OrdersModel orders_model, SecurityCriteria security_criteria, IReadOnlyCollection<SortingCriteria> sorting_criteria, FilterStates filters)
: base(new RXObservableCollection<SecurityMD>(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<Row, SecurityImmutable, SecurityCriteria>(_rows_vm, r => r.security, secs => new SecurityCriteria(secs), sc => sc.securities(bus), nameof(Row.security));
@@ -16,7 +16,7 @@ namespace mbts.spark
{
readonly IBus _bus;
public TimeAndSalesVM(IBus bus, TimeAndSalesModel model, IReadOnlyCollection<SortingCriteria> sorting_criteria, FilterStates filters) : base(new RXCircularCollection<TradeInfo>(model.trades, 1000), sorting_criteria) {
public TimeAndSalesVM(IBus bus, TimeAndSalesModel model, IReadOnlyCollection<SortingCriteria> 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<Row, SecurityImmutable, SecurityCriteria>(_rows_vm, r => r.security, secs => new SecurityCriteria(secs), sc => sc.securities(bus), nameof(Row.security));
@@ -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<T>(this ObservableCollection<T> dest, IEnumerable<T> src) { dest.sync_with(src, EqualityComparer<T>.Default); }
public static void sync_with<T>(this IList<T> dest, IEnumerable<T> src) { dest.sync_with(src, EqualityComparer<T>.Default); }
public static void sync_with_old<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp=null) {
try {
/*
@@ -244,7 +244,7 @@ namespace mbts
throw ex;
}
}
public static void sync_with<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
public static void sync_with<T>(this IList<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
Logger.notnull(dest);
if (src == null) {
dest.Clear();
@@ -17,10 +17,10 @@ namespace mbts.spark
abstract class SecurityGridBase<TRow, TRowModel> : DisposableViewModelBase where TRow : class, ISecurityRow
{
protected SecurityGridBase(IObservable<IEnumerable<TRowModel>> row_models, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models), sorting_criteria) { }
: this(RXCollection.Create(row_models), sorting_criteria) { }
protected SecurityGridBase(IObservable<IEnumerable<TRowModel>> row_models, IEqualityComparer<TRowModel> comparer, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models, comparer), sorting_criteria) { }
: this(RXCollection.Create(row_models, comparer), sorting_criteria) { }
protected SecurityGridBase(IRXCollection<TRowModel> row_models, IEnumerable<SortingCriteria> sorting_criteria) {
_rows_model = row_models;
@@ -56,10 +56,10 @@ namespace mbts.spark
abstract class SecurityGrid<TRow, TRowModel> : SecurityGridBase<TRow, TRowModel>, IHandleRowSelection where TRow : class, ISecurityRow
{
protected SecurityGrid(IObservable<IEnumerable<TRowModel>> row_models, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models), sorting_criteria) { }
: this(RXCollection.Create(row_models), sorting_criteria) { }
protected SecurityGrid(IObservable<IEnumerable<TRowModel>> row_models, IEqualityComparer<TRowModel> comparer, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models, comparer), sorting_criteria) { }
: this(RXCollection.Create(row_models, comparer), sorting_criteria) { }
protected SecurityGrid(IRXCollection<TRowModel> row_models, IEnumerable<SortingCriteria> 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<TRow, TRowModel, TCol> : SecurityGridBase<TRow, TRowModel>, IHandleCellSelection where TRow : class, ISecurityRow
{
protected SecurityGrid(IObservable<IEnumerable<TRowModel>> row_models, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models), sorting_criteria) { }
: this(RXCollection.Create(row_models), sorting_criteria) { }
protected SecurityGrid(IObservable<IEnumerable<TRowModel>> row_models, IEqualityComparer<TRowModel> comparer, IEnumerable<SortingCriteria> sorting_criteria)
: this(new RXObservableCollection<TRowModel>(row_models, comparer), sorting_criteria) { }
: this(RXCollection.Create(row_models, comparer), sorting_criteria) { }
protected SecurityGrid(IRXCollection<TRowModel> row_models, IEnumerable<SortingCriteria> sorting_criteria) : base(row_models, sorting_criteria) =>
_current_cell = backing<DataGridCellInfo>(nameof(current_cell));
@@ -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<OrderRecordImmutable, uint> get_orders_collection(this OrdersModel orders_model) =>
new TableUpdateCollection<OrderRecordImmutable, uint>(orders_model.on_orders.ObserveOnDispatcher());
RXCollection.CreateTableUpdate(orders_model.on_orders.ObserveOnDispatcher());
public static ITableUpdateCollection<OrderRecordImmutable, uint> get_security_orders_collection(this OrdersModel orders_model, uint security) =>
get_security_orders_collection(orders_model, new HashSet<uint>(new[] { security }));
public static ITableUpdateCollection<OrderRecordImmutable, uint> get_security_orders_collection(this OrdersModel orders_model, HashSet<uint> securities) =>
new TableUpdateCollection<OrderRecordImmutable, uint>(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<OrderRecordImmutable, uint> get_active_security_orders_collection(this OrdersModel orders_model, uint security) =>
get_active_security_orders_collection(orders_model, new HashSet<uint>(new[] { security }));
public static ITableUpdateCollection<OrderRecordImmutable, uint> get_active_security_orders_collection(this OrdersModel orders_model, HashSet<uint> securities) =>
new TableUpdateCollection<OrderRecordImmutable, uint>(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<FillTuple, string> get_fill_tuple_collection(this OrdersModel orders_model) =>
new TableUpdateCollection<FillTuple, string>(orders_model.on_fill_tuples);
RXCollection.CreateTableUpdate(orders_model.on_fill_tuples);
#endregion
#region Cancel Orders
@@ -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<T> : IList<T>, INotifyCollectionChanged, IDisposable { }
interface ITableUpdateCollection<T, TKey> : IRXCollection<T> where TKey : IEquatable<TKey> {
bool TryGetValue(TKey key, out T result);
}
static class RXCollection
{
public static IRXCollection<T> Create<T>(IObservable<IEnumerable<T>> source) =>
Create<T, IEnumerable<T>>(source, (c, d) => c.sync_with(d));
public static IRXCollection<T> Create<T>(IObservable<IEnumerable<T>> source, IEqualityComparer<T> comparer) =>
Create<T, IEnumerable<T>>(source, (c, d) => c.sync_with(d, comparer));
public static IRXCollection<T> Create<T, TData>(IObservable<TData> source, Action<IRXCollection<T>, TData> on_data) =>
new AnonymousRXCollection<T, TData>(source, on_data);
public static IRXCollection<T> CreateBuffer<T>(IObservable<IReadOnlyList<T>> source, int buffer) =>
new AnonymousRXCollection<T, IReadOnlyList<T>>(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<T> CreateChunkedBuffer<T>(IObservable<IList<List<T>>> source, int buffer) =>
new AnonymousRXCollection<T, IList<List<T>>>(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<T, TKey> CreateTableUpdate<T, TKey>(IObservable<TableUpdate<T, TKey>> source) where TKey : IEquatable<TKey> =>
CreateTableUpdate(source, _ => true);
public static ITableUpdateCollection<T, TKey> CreateTableUpdate<T, TKey>(IObservable<TableUpdate<T, TKey>> source, Func<T, bool> collection_filter) where TKey : IEquatable<TKey> =>
new TableUpdateCollection<T, TKey>(source, collection_filter ?? new Func<T, bool>(_ => true));
class AnonymousRXCollection<T, TData> : ObservableCollection<T>, IRXCollection<T> {
IDisposable _sub;
public AnonymousRXCollection(IObservable<TData> source, Action<IRXCollection<T>, TData> on_data) =>
_sub = source.ObserveOnDispatcher().Subscribe(d => on_data(this, d));
public void Dispose() => Disposable.dispose(ref _sub);
}
class TableUpdateCollection<T, TKey> : ObservableCollection<T>, ITableUpdateCollection<T, TKey> where TKey : IEquatable<TKey> {
readonly Func<T, bool> _collection_filter;
IReadOnlyDictionary<TKey, T> _raw_state = new ReadOnlyDictionary<TKey, T>(new Dictionary<TKey, T>());
IDisposable _sub;
internal TableUpdateCollection(IObservable<TableUpdate<T, TKey>> source, Func<T, bool> 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);
}
}
}
@@ -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<T> : ICollection<T>, INotifyCollectionChanged, IDisposable { }
interface ITableUpdateCollection<T, TKey> : IRXCollection<T> where TKey : IEquatable<TKey> {
bool TryGetValue(TKey key, out T result);
}
public abstract class RXObservableCollectionBase<T, TData> : ObservableCollection<T>, IRXCollection<T> {
IObservable<TData> _data;
IDisposable _dispose;
protected RXObservableCollectionBase(IObservable<TData> 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<T, TCollection> : RXObservableCollectionBase<T, TCollection> where TCollection : IEnumerable<T> {
protected static readonly TimeSpan DefaultPollInterval = TimeSpan.FromSeconds(1);
readonly IEqualityComparer<T> _comparer;
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the current synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data)
: this(data, EqualityComparer<T>.Default) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the current synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, IEqualityComparer<T> comparer)
: this(data, comparer, SynchronizationContext.Current) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the current synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, SynchronizationContext context)
: this(data, EqualityComparer<T>.Default, context) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, IEqualityComparer<T> comparer, SynchronizationContext context)
: this(data, comparer, context, DefaultPollInterval) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, IEqualityComparer<T> comparer, TimeSpan throttle)
: this(data, comparer, SynchronizationContext.Current, throttle) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, SynchronizationContext context, TimeSpan throttle)
: this(data, EqualityComparer<T>.Default, context, throttle) { }
/// <summary>
/// <para>Initializes a new instance of RXObservableCollection&lt;T, TCollection&gt;, that will observe the source with the specified throttle on the specified synchronization context,
/// and synchronize with the specified IEqualityComparer&lt;T&gt;</para>
/// <para>If a throttle of TimeSpan.Zero is passed, no throttle will used.</para>
/// </summary>
public RXObservableCollection(IObservable<TCollection> data, IEqualityComparer<T> comparer, SynchronizationContext context, TimeSpan throttle)
: base(_parameterized_data(data, context, throttle)) {
_comparer = comparer ?? EqualityComparer<T>.Default;
connect();
}
static IObservable<TCollection> _parameterized_data(IObservable<TCollection> 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<T> : RXObservableCollection<T, IEnumerable<T>> {
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T&gt;, that will observe the source with a 1 second throttle on the current synchronization context
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data)
: base(data, SynchronizationContext.Current) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection&lt;T&gt;, that will observe the source with a 1 second throttle on the current synchronization context
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, IEqualityComparer<T> comparer)
: base(data, SynchronizationContext.Current) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, SynchronizationContext context)
: base(data, context, TimeSpan.FromSeconds(1)) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, IEqualityComparer<T> comparer, SynchronizationContext context)
: base(data, context, TimeSpan.FromSeconds(1)) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection, that will observe the source with the specified throttle
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, IEqualityComparer<T> comparer, TimeSpan throttle)
: base(data, comparer, SynchronizationContext.Current, throttle) { }
/// <summary>
/// Initializes a new instance of RXObservableCollection, that will observe the source with a 1 second throttle on the specified synchronization context
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, SynchronizationContext context, TimeSpan throttle)
: base(data, context, throttle) { }
/// <summary>
/// <para>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&lt;T&gt;</para>
/// <para>If a throttle of TimeSpan.Zero is passed, no throttle will used.</para>
/// </summary>
public RXObservableCollection(IObservable<IEnumerable<T>> data, IEqualityComparer<T> comparer, SynchronizationContext context, TimeSpan throttle)
: base(data, comparer, context, throttle) { }
}
public class RXCircularCollection<T> : RXObservableCollection<T, IReadOnlyList<T>> {
readonly int _buffer;
public RXCircularCollection(IObservable<IReadOnlyList<T>> data, int buffer) : base(data) => _buffer = buffer;
protected override void synchronize(IReadOnlyList<T> 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<T>(ts);
}
}
}
public class TableUpdateCollection<T, TKey> : RXObservableCollectionBase<T, TableUpdate<T, TKey>>, ITableUpdateCollection<T, TKey> where TKey : IEquatable<TKey> {
readonly Func<T, bool> _collection_filter;
IReadOnlyDictionary<TKey, T> _raw_state = new ReadOnlyDictionary<TKey, T>(new Dictionary<TKey, T>());
public TableUpdateCollection(IObservable<TableUpdate<T, TKey>> data) : this(data, null) { }
public TableUpdateCollection(IObservable<TableUpdate<T, TKey>> data, Predicate<T> collection_filter) : base(data) {
_collection_filter = collection_filter == null ? _ => true : new Func<T, bool>(collection_filter);
connect();
}
protected override void synchronize(TableUpdate<T, TKey> 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);
}
}