Module: Solrengine::Realtime

Defined in:
lib/solrengine/realtime.rb,
lib/solrengine/realtime/engine.rb,
lib/solrengine/realtime/version.rb,
lib/solrengine/realtime/account_monitor.rb

Defined Under Namespace

Classes: AccountMonitor, Engine

Constant Summary collapse

LEGACY_SUBSCRIBER_NAME =

Reserved name used by the legacy single-callback API (Solrengine::Realtime.on_account_change=).

:legacy_on_account_change
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.dispatch(wallet_address) ⇒ Object

Dispatch an account change to all registered subscribers. A subscriber raising is logged and does not prevent the remaining subscribers from running. With zero subscribers this is a no-op.



37
38
39
40
41
42
43
44
# File 'lib/solrengine/realtime.rb', line 37

def dispatch(wallet_address)
  subscribers.dup.each do |name, block|
    block.call(wallet_address)
  rescue => e
    log_subscriber_error(name, e)
  end
  nil
end

.on_account_changeObject



57
58
59
# File 'lib/solrengine/realtime.rb', line 57

def 
  subscribers[LEGACY_SUBSCRIBER_NAME]
end

.on_account_change=(callable) ⇒ Object

Legacy single-callback API (soft-deprecated; prefer subscribe/unsubscribe). Assignment registers the callable under LEGACY_SUBSCRIBER_NAME; re-assignment replaces it, preserving the 0.1 replace semantics.



49
50
51
52
53
54
55
# File 'lib/solrengine/realtime.rb', line 49

def (callable)
  if callable.nil?
    unsubscribe(LEGACY_SUBSCRIBER_NAME)
  else
    subscribers[LEGACY_SUBSCRIBER_NAME] = callable
  end
end

.subscribe(name, &block) ⇒ Object

Register a subscriber. The block is called with the wallet address whenever a monitored account changes. Registering under an existing name replaces the previous subscriber.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/solrengine/realtime.rb', line 22

def subscribe(name, &block)
  raise ArgumentError, "a block is required" unless block

  subscribers[name] = block
  block
end

.subscribersObject

Registry of account-change subscribers, keyed by name.



15
16
17
# File 'lib/solrengine/realtime.rb', line 15

def subscribers
  @subscribers ||= {}
end

.unsubscribe(name) ⇒ Object

Remove a subscriber by name. Returns the removed block, or nil.



30
31
32
# File 'lib/solrengine/realtime.rb', line 30

def unsubscribe(name)
  subscribers.delete(name)
end