local_bus 0.1.0 → 0.1.1

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: fa03fc7b7c1f5e14bff852435b75e343f150ceee9b5f9638ce365918bc39c939
4
+ data.tar.gz: 1aa8d5ffa9895245ff78f1b00c2d4f3697a1183afe82dc4bcd3d7c420453e924
5
5
  SHA512:
6
- metadata.gz: 18c4ca9e676956c6b2571093bec9f57a76714ceb833f7aae021ac6563965d438499c05fcd5214e63b5b9cb8a192ed739d6926b54bc9d97e87a9f33b193289052
7
- data.tar.gz: b1b99ef58b04fcfcf45808596be55bc9919f455aeb65a3ef53a56157305bff5aaf1310d418a02c14413f6db735a06dea58440d5f9d3a849073fd6a53f837247d
6
+ metadata.gz: 6e17c54a44652eac0c7edb7715a1db68507a0b82b4c3acdd9bfbc4e41d5741b5b80965d623be8e369c4080e43a0cb8696431d6464588655ecc863c47d8bf81e6
7
+ data.tar.gz: 57fbbe8719669a3dbee59252c2d71ce20c1ce232110779ffa9bf8fa4c68e7da1b59cb9fb916d308c7e8d21e499117eb74b4a5e464db2c0eab2866e53855d9c12
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
@@ -124,8 +124,7 @@ class LocalBus
124
124
  # @rbs &block: (Message) -> untyped -- alternative way to provide a callable
125
125
  # @rbs return: self
126
126
  def subscribe(topic, callable: nil, &block)
127
- callable ||= block
128
- bus.subscribe(topic, &callable)
127
+ bus.subscribe(topic, callable: callable || block)
129
128
  self
130
129
  end
131
130
 
@@ -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.1"
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.1
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