diff --git a/src/client/spark_api/api/Update.cs b/src/client/spark_api/api/Update.cs index f02d96bf1..85056d0e0 100644 --- a/src/client/spark_api/api/Update.cs +++ b/src/client/spark_api/api/Update.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Reactive.Linq; @@ -43,9 +44,9 @@ namespace bts.api.core { public class TableUpdate where TKey : IEquatable { - readonly Dictionary _state; + ImmutableDictionary _state; - internal TableUpdate(Dictionary init, IReadOnlyList added, IReadOnlyList removed) { + internal TableUpdate(ImmutableDictionary init, IReadOnlyList added, IReadOnlyList removed) { _state = init; this.added = added; this.removed = removed; @@ -59,14 +60,10 @@ namespace bts.api.core { this.update(update.removed, update.added, update.key); internal TableUpdate update(IReadOnlyList removed, IReadOnlyList added, Func key) { - var table = new TableUpdate(new Dictionary(_state), added, removed); - foreach (var i in removed) { - table._state.Remove(key(i)); - } - foreach (var item in added) { - table._state.Add(key(item), item); - } - return table; + var builder = _state.ToBuilder(); + builder.RemoveRange(removed.Select(i => key(i))); + builder.AddRange(added.Select(i => new KeyValuePair(key(i), i))); + return new TableUpdate(builder.ToImmutableDictionary(), added, removed); } internal TableUpdate as_snapshot() { @@ -102,7 +99,7 @@ namespace bts.api.core { public static IObservable> Table(this IObservable> source) where TKey : IEquatable => source.Scan( - new TableUpdate(new Dictionary(), new List(), new List()), + new TableUpdate(ImmutableDictionary.Empty, new List(), new List()), (table, update) => table.update(update)); public static IObservable> Cache(this IObservable> source) where TKey : IEquatable { diff --git a/src/client/spark_api/spark_api.csproj b/src/client/spark_api/spark_api.csproj index fbb1f6778..5a555f48a 100644 --- a/src/client/spark_api/spark_api.csproj +++ b/src/client/spark_api/spark_api.csproj @@ -1,34 +1,35 @@ - - - netstandard2.0 - Blue Trading Systems - Blue Trading Systems - Spark - - true - x64 - AnyCPU - true - - 1.0.0 - 7.2 - - - - - - - - - - - - - - - - - - - - + + + netstandard2.0 + Blue Trading Systems + Blue Trading Systems + Spark + + true + x64 + AnyCPU + true + + 1.0.0 + 7.2 + + + + + + + + + + + + + + + + + + + + +