stoqey / ib

Interactive Brokers TWS/IB Gateway API client library for Node.js (TS)
https://stoqey.github.io/ib-doc/
MIT License
205 stars 47 forks source link

Two active subscriptions for the same Event #193

Open rylorin opened 11 months ago

rylorin commented 11 months ago

Hello,

If you setup 2 active subscriptions using the same event, but different callbacks, for example:

this.subscriptions.register<OpenOrder[]>(
      () => {
        this.api.reqOpenOrders();
      },
      undefined,
      [
        [EventName.openOrder, this.onOpenOrder],
        [EventName.orderStatus, this.onOrderStatus],
        [EventName.orderBound, this.onOrderBound],
        [EventName.openOrderEnd, this.onOpenOrderEnd],
      ],
      "getOpenOrders", // use same instance id each time, to make sure there is only 1 pending request at time
    );

and

this.subscriptions
        .register<OpenOrder[]>(
          () => {
            this.api.reqAllOpenOrders();
          },
          undefined,
          [
            [EventName.openOrder, this.onOpenOrder],
            [EventName.orderStatus, this.onOrderStatus],
            [EventName.orderBound, this.onOrderBound],
            [EventName.openOrderEnd, this.onOpenOrderComplete],
          ],
          "getAllOpenOrders", // use same instance id each time, to make sure there is only 1 pending request at time
        );

As the subscription registry maintains a Map with the EventName as key and a callback and it's subscriptions as entry:

  /** A Map containing the subscription registry, with event name as key. */
  private readonly entires = new IBApiNextMap<EventName, RegistryEntry>();

The second callback for the same EventName.openOrderEnd will be ignored (in favour of the first active subscription callback) leading to undesired behaviour.

Thanks