Class: IGMarkets::Streaming::MarketSubscriptionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/streaming/market_subscription_manager.rb

Overview

This class manages a set of streaming subscriptions for a changing set of EPICs. The set of EPICs to stream data for is set by calls to #epics=, and when streaming market data becomes available it is passed to all registered #on_data callbacks.

This class can be used standalone, but its primary function is as a helper for the AccountState class.

Instance Method Summary collapse

Constructor Details

#initialize(dealing_platform) ⇒ MarketSubscriptionManager

Initializes this market subscription manager with the specified dealing platform.

Parameters:



12
13
14
15
16
17
# File 'lib/ig_markets/streaming/market_subscription_manager.rb', line 12

def initialize(dealing_platform)
  @dealing_platform = dealing_platform

  @subscriptions = {}
  @on_data_callbacks = []
end

Instance Method Details

#clearObject

Removes all market subscriptions and data callbacks.



20
21
22
23
24
25
# File 'lib/ig_markets/streaming/market_subscription_manager.rb', line 20

def clear
  @dealing_platform.streaming.remove_subscriptions @subscriptions.values

  @subscriptions = {}
  @on_data_callbacks = []
end

#epics=(epics) ⇒ Object

Sets the EPICs that this market subscription manager should be subscribed to and notifying updates for through #on_data.

Parameters:

  • epics (Array<String>)


41
42
43
44
45
46
# File 'lib/ig_markets/streaming/market_subscription_manager.rb', line 41

def epics=(epics)
  epics = Array(epics).uniq

  create_missing_subscriptions epics
  remove_unused_subscriptions epics
end

#on_data(&callback) ⇒ Object

Adds the passed block to the list of callbacks that will be run when a market that is subscribed to receives new data. The block will be called on a worker thread and so the code that is run by the block must be thread-safe. The arguments passed to the block are ‘|data, merged_data|`, and both arguments will be instances of IGMarkets::Streaming::MarketUpdate.

Parameters:

  • callback (Proc)

    The callback that is to be run.



33
34
35
# File 'lib/ig_markets/streaming/market_subscription_manager.rb', line 33

def on_data(&callback)
  @on_data_callbacks << callback
end