Class: Cryptomarket::Websocket::CallbackCache

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptomarket/websocket/callback_cache.rb

Overview

A cache store for callbacks. uses reusable callbacks, so it cans use a callback for more than one time. Each callback stored

Instance Method Summary collapse

Constructor Details

#initializeCallbackCache

Returns a new instance of CallbackCache.



10
11
12
13
14
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 10

def initialize
  @reusable_callbacks = {}
  @subscription_callbacks = {}
  @next_id = 1
end

Instance Method Details

#_get_next_idObject



16
17
18
19
20
21
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 16

def _get_next_id
  next_id = @next_id
  @next_id += 1
  @next_id = 1 if @next_id.negative?
  next_id
end

#delete_subscription_callback(key) ⇒ Object



47
48
49
50
51
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 47

def delete_subscription_callback(key)
  return unless @subscription_callbacks.key? key

  @subscription_callbacks.delete(key)
end

#get_callback(id) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 29

def get_callback(id)
  return nil unless @reusable_callbacks.key? id

  callback, done_using = @reusable_callbacks[id].get_callback
  @reusable_callbacks.delete(id) if done_using
  callback
end

#get_subscription_callback(key) ⇒ Object



41
42
43
44
45
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 41

def get_subscription_callback(key)
  return nil unless @subscription_callbacks.key? key

  @subscription_callbacks[key]
end

#store_callback(callback, call_count = 1) ⇒ Object



23
24
25
26
27
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 23

def store_callback(callback, call_count = 1)
  id = _get_next_id
  @reusable_callbacks[id] = ReusableCallback.new(callback, call_count)
  id
end

#store_subscription_callback(key, callback) ⇒ Object



37
38
39
# File 'lib/cryptomarket/websocket/callback_cache.rb', line 37

def store_subscription_callback(key, callback)
  @subscription_callbacks[key] = callback
end