diff --git a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs index e7a16fdce..0d1b3ac40 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs @@ -1,4 +1,5 @@ using bts.api; +using bts.collections; using bts.md; using mbts; using mbts.config; @@ -50,6 +51,7 @@ namespace bts.shell { _mbo = mbo; _dispose = Disposable.Create( bus.subscribe(SparkTags.SecuritiesTag).Subscribe(s => _securities = s), + bus.subscribe(SparkTags.ShowLadderAutoLookupTag).Subscribe(_show_windows), bus.subscribe(SparkTags.ShowLadderTag).OnDispatcher().Subscribe(s => show_windows(new[] { s })), bus.subscribe(SparkTags.ShowLaddersTag).OnDispatcher().Subscribe(show_windows) ); @@ -273,7 +275,21 @@ namespace bts.shell { }); } - void show_windows(IReadOnlyCollection securities) { + async void _show_windows(IImmutableSet securities) { + if (securities == null) + return; + var start_time = DateTime.Now; + do { + var found = securities.KeyJoin(_securities).ToList(); + securities = securities.RemoveRange(found.Select(SecurityMD.key_fn)); + show_windows(found); + if (securities.Count == 0) + break; + await Task.Delay(500); + } while ((DateTime.Now - start_time) < _ladder_conf.security_timeout); + } + + void show_windows(IEnumerable securities) { int n_allowed = (int)_ladder_conf.max_open_ladders - _wm.list_windows().Where(w => w.window_type == _window_type).Count(); int n_opened = 0; foreach (var s in securities) { diff --git a/src/client/spark/core/Bus/SparkCommands.cs b/src/client/spark/core/Bus/SparkCommands.cs index 1c2e0cea5..ee4414e48 100644 --- a/src/client/spark/core/Bus/SparkCommands.cs +++ b/src/client/spark/core/Bus/SparkCommands.cs @@ -8,6 +8,7 @@ using mbts.spark; using mbts.ui; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Windows.Input; namespace bts.spark { @@ -41,6 +42,9 @@ namespace bts.spark { SparkCommand.create("P&L (2.0)", Icons.Accounting, SparkTags.ShowPNLTag); // Ladder + public static PartialCommand> ShowLadderAutoLookup(KeyGesture key_gesture = null) => + SparkCommand.create("Ladder", Icons.Ladder, SparkTags.ShowLadderAutoLookupTag, key_gesture); + public static PartialCommand ShowLadder(KeyGesture key_gesture = null) => SparkCommand.create("Ladder", Icons.Ladder, SparkTags.ShowLadderTag, key_gesture); diff --git a/src/client/spark/core/Bus/SparkTags.cs b/src/client/spark/core/Bus/SparkTags.cs index 0449e8065..311e7fb13 100644 --- a/src/client/spark/core/Bus/SparkTags.cs +++ b/src/client/spark/core/Bus/SparkTags.cs @@ -7,6 +7,7 @@ using bts.spark; using mbts.ui; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Threading.Tasks; namespace mbts.spark { @@ -55,6 +56,8 @@ namespace mbts.spark { // The owners of the various SparkWindows may remove dependencies on the former at their leasure. // For now, both tags go through the same logic in the LadderWindowFactory anyways. public static ITag ShowLadderTag { get; } = new SparkTag("Show Ladder"); + // TODO: get rid of the SecurityMD ShowLadderTag. This has created burdensome dependency issues. + public static ITag> ShowLadderAutoLookupTag { get; } = new SparkTag>("Show Ladder Auto Lookup"); public static ITag> ShowLaddersTag { get; } = new SparkTag>(nameof(ShowLaddersTag)); public static ITag> ShowLinkedLadder { get; } = new SparkTag>("Linked Ladder"); public static ITag ShowMarketViewTag { get; } = new SparkTag("Show MarketView"); diff --git a/src/client/spark_api/md/SecurityMD.cs b/src/client/spark_api/md/SecurityMD.cs index d4ce00c1b..6af6e0324 100644 --- a/src/client/spark_api/md/SecurityMD.cs +++ b/src/client/spark_api/md/SecurityMD.cs @@ -1,13 +1,11 @@ using bts.api; using mbts.co2; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace mbts { - struct SecurityMD : IEquatable { + readonly struct SecurityMD : IEquatable { + public static readonly Func key_fn = smd => smd.security.Id; + public SecurityImmutable security { get; } public MDConsumerHandle md { get; } @@ -36,4 +34,4 @@ namespace mbts { return !(left == right); } } -} +} \ No newline at end of file diff --git a/src/client/spark_core/Collections/Extensions/ImmutableSetExtensions.cs b/src/client/spark_core/Collections/Extensions/ImmutableSetExtensions.cs new file mode 100644 index 000000000..d22b9f6df --- /dev/null +++ b/src/client/spark_core/Collections/Extensions/ImmutableSetExtensions.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace bts.collections { + public static class ImmutableSetExtensions { + public static IImmutableSet RemoveRange(this IImmutableSet source, IEnumerable values) { + /// Prefer the implementation. + /// It uses an to remove values, which is optimized for bulk changes (less memory allocations, etc.). + if (source is ImmutableHashSet ihs) { + return ihs.RemoveRange(values); + } + if (source == null) throw new ArgumentNullException(nameof(source)); + if (values == null) throw new ArgumentNullException(nameof(values)); + foreach (var v in values) + source = source.Remove(v); + return source; + } + + public static ImmutableHashSet RemoveRange(this ImmutableHashSet source, IEnumerable values) { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (values == null) throw new ArgumentNullException(nameof(values)); + var bd = source.ToBuilder(); + foreach (var v in values) + bd.Remove(v); + return bd.ToImmutable(); + } + } +} \ No newline at end of file