local_bus 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc7597ce848ed87503b21e8b893d9637f7f601945d4e6d0904938f2319b691f0
4
- data.tar.gz: 6b65f578aac4e885afc4d76265fdf3812b2b3d8d32b2f0ba7d6abdc6f824fcf7
3
+ metadata.gz: 3abe4359b671f5e051a9e3b4e2cb0ac73ca9c00ef63c1622c158ebf4c116a1b5
4
+ data.tar.gz: 408ea743c2b5b8d3a000a8db5b8207f6e3d90edfb3976b8bf1aad3ec11dde8ab
5
5
  SHA512:
6
- metadata.gz: 18c4ca9e676956c6b2571093bec9f57a76714ceb833f7aae021ac6563965d438499c05fcd5214e63b5b9cb8a192ed739d6926b54bc9d97e87a9f33b193289052
7
- data.tar.gz: b1b99ef58b04fcfcf45808596be55bc9919f455aeb65a3ef53a56157305bff5aaf1310d418a02c14413f6db735a06dea58440d5f9d3a849073fd6a53f837247d
6
+ metadata.gz: de71a197041a072f69ad37e971663983da3f77a00889c71afc622d382e74480bf5bef25ebfdc5d97762d4a15f385b0d63bdd929f67c6e455dc378340d3afda93
7
+ data.tar.gz: fe165204058f9e5f9df6aa00d4f3d07db69478eeca3232a7204be758e0b6418800e903c576c927b04b0a958946e729c385a27341bb1f9168bcb2cb83198396db
data/README.md CHANGED
@@ -87,7 +87,7 @@ Both interfaces ensure optimal performance:
87
87
  Best for real-time operations like logging, metrics, and state updates.
88
88
 
89
89
  ```ruby
90
- bus = LocalBus.instance.bus
90
+ bus = LocalBus::Bus.new # ... or LocalBus.instance.bus
91
91
 
92
92
  bus.subscribe "user.created" do |message|
93
93
  AuditLog.record(message.payload)
@@ -101,6 +101,33 @@ result.wait # blocks until all subscribers complete
101
101
  result.value # blocks and waits until all subscribers complete and returns the subscribers
102
102
  ```
103
103
 
104
+ Subscribe with an explicit `callable`.
105
+
106
+ ```ruby
107
+ callable = ->(message) { "Received message: #{message.payload}" }
108
+ LocalBus.instance.bus.subscribe "user.created", callable: callable
109
+
110
+ subscribers = LocalBus.instance.bus.publish("user.created", user_id: 123).value
111
+ # => [#<LocalBus::Subscriber:0x0000000126b7cf38 ...>]
112
+
113
+ subscribers.first.value
114
+ # => "Received message: {:user_id=>123}"
115
+
116
+ # you can use any object that responds to #call
117
+ class ExampleCallable
118
+ def call(message)
119
+ "Received message: #{message.payload}"
120
+ end
121
+ end
122
+
123
+ LocalBus.instance.bus.subscribe "user.created", callable: ExampleCallable.new
124
+ subscribers = LocalBus.instance.bus.publish("user.created", user_id: 123).value
125
+ # => [#<LocalBus::Subscriber:0x0000000126b7cf38 ...>]
126
+
127
+ subscribers.first.value
128
+ # => "Received message: {:user_id=>123}"
129
+ ```
130
+
104
131
  ### Station (background processing)
105
132
 
106
133
  Best for async operations like emails, notifications, and resource-intensive tasks.
@@ -120,6 +147,33 @@ result.wait # blocks until all subscribers complete
120
147
  result.value # blocks and waits until all subscribers complete and returns the subscribers
121
148
  ```
122
149
 
150
+ Subscribe with an explicit `callable`.
151
+
152
+ ```ruby
153
+ callable = ->(message) { "Received message: #{message.payload}" }
154
+ LocalBus.instance.station.subscribe "email.welcome", callable: callable
155
+
156
+ subscribers = LocalBus.instance.station.publish("email.welcome", user_id: 123).value
157
+ # => [#<LocalBus::Subscriber:0x0000000126b7cf38 ...>]
158
+
159
+ subscribers.first.value
160
+ # => "Received message: {:user_id=>123}"
161
+
162
+ # you can use any object that responds to #call
163
+ class ExampleCallable
164
+ def call(message)
165
+ "Received message: #{message.payload}"
166
+ end
167
+ end
168
+
169
+ LocalBus.instance.station.subscribe "email.welcome", callable: ExampleCallable.new
170
+ subscribers = LocalBus.instance.station.publish("email.welcome", user_id: 123).value
171
+ # => [#<LocalBus::Subscriber:0x0000000126b7cf38 ...>]
172
+
173
+ subscribers.first.value
174
+ # => "Received message: {:user_id=>123}"
175
+ ```
176
+
123
177
  ## Advanced Usage & Considerations
124
178
 
125
179
  ### Concurrency Controls
data/lib/local_bus/bus.rb CHANGED
@@ -73,6 +73,7 @@ class LocalBus
73
73
  # @rbs topic: String -- topic name
74
74
  # @rbs return: self
75
75
  def unsubscribe_all(topic)
76
+ topic = topic.to_s
76
77
  @subscriptions[topic].clear
77
78
  @subscriptions.delete topic
78
79
  self
@@ -99,6 +99,8 @@ class LocalBus
99
99
 
100
100
  @pool = nil
101
101
  end
102
+ rescue
103
+ nil # ignore errors during shutdown
102
104
  end
103
105
 
104
106
  # Clean up shutdown handler
@@ -124,8 +126,7 @@ class LocalBus
124
126
  # @rbs &block: (Message) -> untyped -- alternative way to provide a callable
125
127
  # @rbs return: self
126
128
  def subscribe(topic, callable: nil, &block)
127
- callable ||= block
128
- bus.subscribe(topic, &callable)
129
+ bus.subscribe(topic, callable: callable || block)
129
130
  self
130
131
  end
131
132
 
@@ -214,9 +215,9 @@ class LocalBus
214
215
  # @rbs on: Array[String] -- signals to trap
215
216
  # @rbs return: void
216
217
  def enable_safe_shutdown(on:)
218
+ at_exit { stop }
217
219
  on.each do |signal|
218
220
  trap signal do
219
- # Only queue the signal if we haven't started shutdown
220
221
  @shutdown_queue.push signal unless @shutdown.true?
221
222
  rescue
222
223
  nil
@@ -27,7 +27,10 @@ class LocalBus
27
27
  @callable = callable
28
28
  @message = message
29
29
  @id = callable.object_id
30
- @source_location = callable.source_location
30
+ @source_location = case callable
31
+ in Proc then callable.source_location
32
+ else callable.method(:call).source_location
33
+ end
31
34
  @metadata = {}
32
35
  end
33
36
 
@@ -3,5 +3,5 @@
3
3
  # rbs_inline: enabled
4
4
 
5
5
  class LocalBus
6
- VERSION = "0.1.0"
6
+ VERSION = "0.1.2"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: local_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Hopkins (hopsoft)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-04 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async