tiny_bus 3.8.0 → 3.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_bus.rb +18 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38ce7566363b86a218bb2860573779d5737e6051bc34b78051ca0f20d737ada1
4
- data.tar.gz: af3d2e8f729655b6907793580c20024ff94c3fb5bda862266623c78c94d35dbf
3
+ metadata.gz: a1da58fd94dfa2c8ad51e0b0031ee4e8372a04f2fd8f9089ef605f2e5827cc85
4
+ data.tar.gz: 6cc67c07bf48e25acc9ec74da88a0012cc967f150746c6c54aae32ce3ccec309
5
5
  SHA512:
6
- metadata.gz: a4e70298e41c92c63bdd3dc88ac487ab301823aba258198cae8ea10256170fec4c08ac9ba7bc2acfa722c1c57c933858d07302b8f2ef4fac2ba57c21ee5d4a77
7
- data.tar.gz: bc01af0b3a2a97770972103424f0220f20629eed25e0ae1613029b09a8b14083c39233d7ac52faf13576771b57358885cc325160f54fa95ad7518b7a745221d4
6
+ metadata.gz: 19208ab76103862362cf7eefe6707717521222b4f5ae657ac92606b77647dbb3a308c12dba6ff1fe47184d4ceab3c3d2bf80aaed9fed7eed53b7d06666b51325
7
+ data.tar.gz: a1e704fc61c63c2bdc9b471b3befd96deb5b0497514e8a38be437c6b2a57529e0c1d3d851b19942604e64592f24ff919aa78b53eac7bfd068e85435dcc8139c0
data/lib/tiny_bus.rb CHANGED
@@ -81,15 +81,22 @@ class TinyBus
81
81
  end
82
82
 
83
83
  # adds a subscriber to a topic
84
+ #
85
+ # - topic: either a String, or an array of String
86
+ # - subber: an object that responds to #msg that can receive messages
84
87
  def sub(topic, subber)
85
88
  raise TinyBus::SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
86
89
 
87
- @dead_topics.delete(topic)
88
- @subs[topic] ||= Set.new
89
- @subs[topic] << subber
90
- @stats[topic] ||= 0
90
+ topics = topic.is_a?(Array) ? topic : [topic]
91
+
92
+ topics.each do |t|
93
+ @dead_topics.delete(t)
94
+ @subs[t] ||= Set.new
95
+ @subs[t] << subber
96
+ @stats[t] ||= 0
91
97
 
92
- msg({ @topic_key => 'sub', 'to_topic' => topic, 'subber' => _to_subber_id(subber) }, 'TINYBUS-SUB')
98
+ msg({ @topic_key => 'sub', 'to_topic' => t, 'subber' => _to_subber_id(subber) }, 'TINYBUS-SUB')
99
+ end
93
100
  end
94
101
 
95
102
  # removes a subscriber from a topic
@@ -127,7 +134,12 @@ class TinyBus
127
134
  @stats[@total_key] += 1
128
135
  @stats[topic] += 1
129
136
  if (subbers&.length || 0) > 0
130
- subbers.each{|s| s.msg(msg) }
137
+ # cloning is necessary, because sending messanges may result in new
138
+ # subscribers to the same topic we're iterating over right now. in that
139
+ # situation we would run into a RuntimeError that prevented the
140
+ # modification of the Set we're iterating over to send messages.
141
+ subbers.clone.each{|s| s.msg(msg) }
142
+
131
143
  @log.send(lvl, "S #{msg}")
132
144
  else
133
145
  if @raise_on_dead
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.0
4
+ version: 3.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-19 00:00:00.000000000 Z
11
+ date: 2023-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiny_log