Class: DhanHQ::WS::SubState
- Inherits:
-
Object
- Object
- DhanHQ::WS::SubState
- 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
-
#initialize ⇒ SubState
constructor
A new instance of SubState.
-
#mark_subscribed!(list) ⇒ void
Marks the provided instruments as subscribed.
-
#mark_unsubscribed!(list) ⇒ void
Marks the provided instruments as unsubscribed.
-
#snapshot ⇒ Array<String>
Returns the current subscription snapshot.
-
#want_sub(list) ⇒ Array<Hash>
Filters out instruments that are already subscribed.
-
#want_unsub(list) ⇒ Array<Hash>
Filters the instruments that are currently subscribed.
Constructor Details
#initialize ⇒ SubState
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.
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.
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 |
#snapshot ⇒ Array<String>
Returns the current subscription snapshot.
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.
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.
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 |