solrengine-realtime 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/solrengine/realtime/account_monitor.rb +3 -2
- data/lib/solrengine/realtime/version.rb +1 -1
- data/lib/solrengine/realtime.rb +64 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e82459ac5e5f823da98101741161c00aa3f7ce6ae18118979b698c65895a5195
|
|
4
|
+
data.tar.gz: 1e7e6f233474d46a3433477cd5514487b841b80867e272b10e1f9a62f4edc31a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6fc37b96ecb575e062841dd55d92ed80f813c14be0a1a051c0148066d3e9db1ad21acdf2b9f562da25cfa91dcb55c5b24e4c19c0fb0378873d65f48050e49a27
|
|
7
|
+
data.tar.gz: c1d2a5812ed1f613e399bca1424e6ee470c561ea8fe94d2a49375a33fa4661c1a1f9fd022d78be7a3ef471e627609799d02835b2650485e8a7911b140b8d79ca
|
|
@@ -5,7 +5,8 @@ require "websocket-client-simple"
|
|
|
5
5
|
module Solrengine
|
|
6
6
|
module Realtime
|
|
7
7
|
# Subscribes to Solana WebSocket RPC for real-time account changes.
|
|
8
|
-
# When an account changes,
|
|
8
|
+
# When an account changes, dispatches to all registered subscribers
|
|
9
|
+
# via Solrengine::Realtime.dispatch.
|
|
9
10
|
class AccountMonitor
|
|
10
11
|
SOLANA_ADDRESS_REGEX = /\A[1-9A-HJ-NP-Za-km-z]{32,44}\z/
|
|
11
12
|
RECONNECT_DELAY = 5
|
|
@@ -136,7 +137,7 @@ module Solrengine
|
|
|
136
137
|
sleep BROADCAST_DELAY
|
|
137
138
|
@mutex.synchronize { @account_changed = false }
|
|
138
139
|
begin
|
|
139
|
-
Solrengine::Realtime.
|
|
140
|
+
Solrengine::Realtime.dispatch(@wallet_address)
|
|
140
141
|
rescue => e
|
|
141
142
|
logger.error("[SolanaWS] Callback error: #{e.message}")
|
|
142
143
|
end
|
data/lib/solrengine/realtime.rb
CHANGED
|
@@ -6,8 +6,71 @@ require_relative "realtime/engine" if defined?(Rails::Engine)
|
|
|
6
6
|
|
|
7
7
|
module Solrengine
|
|
8
8
|
module Realtime
|
|
9
|
-
|
|
9
|
+
# Reserved name used by the legacy single-callback API
|
|
10
|
+
# (Solrengine::Realtime.on_account_change=).
|
|
11
|
+
LEGACY_SUBSCRIBER_NAME = :legacy_on_account_change
|
|
10
12
|
|
|
13
|
+
class << self
|
|
14
|
+
# Registry of account-change subscribers, keyed by name.
|
|
15
|
+
def subscribers
|
|
16
|
+
@subscribers ||= {}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Register a subscriber. The block is called with the wallet address
|
|
20
|
+
# whenever a monitored account changes. Registering under an existing
|
|
21
|
+
# name replaces the previous subscriber.
|
|
22
|
+
def subscribe(name, &block)
|
|
23
|
+
raise ArgumentError, "a block is required" unless block
|
|
24
|
+
|
|
25
|
+
subscribers[name] = block
|
|
26
|
+
block
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Remove a subscriber by name. Returns the removed block, or nil.
|
|
30
|
+
def unsubscribe(name)
|
|
31
|
+
subscribers.delete(name)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Dispatch an account change to all registered subscribers.
|
|
35
|
+
# A subscriber raising is logged and does not prevent the remaining
|
|
36
|
+
# subscribers from running. With zero subscribers this is a no-op.
|
|
37
|
+
def dispatch(wallet_address)
|
|
38
|
+
subscribers.dup.each do |name, block|
|
|
39
|
+
block.call(wallet_address)
|
|
40
|
+
rescue => e
|
|
41
|
+
log_subscriber_error(name, e)
|
|
42
|
+
end
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Legacy single-callback API (soft-deprecated; prefer subscribe/unsubscribe).
|
|
47
|
+
# Assignment registers the callable under LEGACY_SUBSCRIBER_NAME;
|
|
48
|
+
# re-assignment replaces it, preserving the 0.1 replace semantics.
|
|
49
|
+
def on_account_change=(callable)
|
|
50
|
+
if callable.nil?
|
|
51
|
+
unsubscribe(LEGACY_SUBSCRIBER_NAME)
|
|
52
|
+
else
|
|
53
|
+
subscribers[LEGACY_SUBSCRIBER_NAME] = callable
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def on_account_change
|
|
58
|
+
subscribers[LEGACY_SUBSCRIBER_NAME]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def log_subscriber_error(name, error)
|
|
64
|
+
message = "[Solrengine::Realtime] Subscriber #{name.inspect} raised: #{error.class}: #{error.message}"
|
|
65
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
66
|
+
Rails.logger.warn(message)
|
|
67
|
+
else
|
|
68
|
+
warn(message)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Default subscriber, kept from 0.1; assigning on_account_change= replaces it.
|
|
11
74
|
self.on_account_change = ->(wallet_address) {
|
|
12
75
|
puts "[SolRengine] Account changed: #{wallet_address}"
|
|
13
76
|
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: solrengine-realtime
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jose Ferrer
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-06-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|