Class: DhanHQ::WS::SubState

Inherits:
Object
  • Object
show all
Defined in:
lib/DhanHQ/ws/sub_state.rb

Overview

Maintains the current subscription state and performs diffing so that the client only sends incremental subscribe/unsubscribe requests.

Instance Method Summary collapse

Constructor Details

#initializeSubState

Returns a new instance of SubState.



10
11
12
13
# File 'lib/DhanHQ/ws/sub_state.rb', line 10

def initialize
  @set   = Concurrent::Set.new
  @mutex = Mutex.new
end

Instance Method Details

#mark_subscribed!(list) ⇒ void

This method returns an undefined value.

Marks the provided instruments as subscribed.

Parameters:

  • list (Array<Hash>)


27
28
29
# File 'lib/DhanHQ/ws/sub_state.rb', line 27

def mark_subscribed!(list)
  @mutex.synchronize { list.each { |i| @set.add(key_for(i)) } }
end

#mark_unsubscribed!(list) ⇒ void

This method returns an undefined value.

Marks the provided instruments as unsubscribed.

Parameters:

  • list (Array<Hash>)


43
44
45
# File 'lib/DhanHQ/ws/sub_state.rb', line 43

def mark_unsubscribed!(list)
  @mutex.synchronize { list.each { |i| @set.delete(key_for(i)) } }
end

#snapshotArray<String>

Returns the current subscription snapshot.

Returns:

  • (Array<String>)

    Instrument identifiers in "SEGMENT:SECID" form.



50
51
52
# File 'lib/DhanHQ/ws/sub_state.rb', line 50

def snapshot
  @mutex.synchronize { @set.to_a }
end

#want_sub(list) ⇒ Array<Hash>

Filters out instruments that are already subscribed.

Parameters:

  • list (Array<Hash>)

Returns:

  • (Array<Hash>)

    Instruments that still need to be subscribed.



19
20
21
# File 'lib/DhanHQ/ws/sub_state.rb', line 19

def want_sub(list)
  @mutex.synchronize { list.reject { |i| @set.include?(key_for(i)) } }
end

#want_unsub(list) ⇒ Array<Hash>

Filters the instruments that are currently subscribed.

Parameters:

  • list (Array<Hash>)

Returns:

  • (Array<Hash>)

    Instruments that can be unsubscribed.



35
36
37
# File 'lib/DhanHQ/ws/sub_state.rb', line 35

def want_unsub(list)
  @mutex.synchronize { list.select { |i| @set.include?(key_for(i)) } }
end