tiny_bus 3.7.1 → 3.8.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 +14 -4
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69976083efa62900031ef18daf5d222157fa8c2e1250a4fac06c8068a98631db
4
- data.tar.gz: 3f428977292bfdbe108d859406b3f2359646a1e28e716779ce5b6c828a2a5e6a
3
+ metadata.gz: 38ce7566363b86a218bb2860573779d5737e6051bc34b78051ca0f20d737ada1
4
+ data.tar.gz: af3d2e8f729655b6907793580c20024ff94c3fb5bda862266623c78c94d35dbf
5
5
  SHA512:
6
- metadata.gz: df717813c4b3b516c6a2f16fdf539e2a8636a67a89a5c9b273640b721feaa37c16c69d9cd66198e8c36fbf322904384eac39987db3ae43d288e99da43c4b8b9b
7
- data.tar.gz: d95a5ad0da6d327bd4c4586e4ee7ec9a9859a1c540a34a5f87ba9a9301280ff58822a9f805c54f499eb7d4ea18a18e7e309f30c67acb5c9a6c614caf79555a68
6
+ metadata.gz: a4e70298e41c92c63bdd3dc88ac487ab301823aba258198cae8ea10256170fec4c08ac9ba7bc2acfa722c1c57c933858d07302b8f2ef4fac2ba57c21ee5d4a77
7
+ data.tar.gz: bc01af0b3a2a97770972103424f0220f20629eed25e0ae1613029b09a8b14083c39233d7ac52faf13576771b57358885cc325160f54fa95ad7518b7a745221d4
data/lib/tiny_bus.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'set'
1
2
  require 'time'
2
3
  require 'set'
3
4
  require 'securerandom'
@@ -29,6 +30,8 @@ require 'tiny_pipe'
29
30
  class TinyBus
30
31
  ANNOTATION_PREFIX_DEFAULT = '.'
31
32
 
33
+ attr_reader :dead_topics
34
+
32
35
  # log:
33
36
  # if specified, it should be a TinyLog instance
34
37
  # if not specified, it will create a new TinyLog instance for $stdout
@@ -55,6 +58,7 @@ class TinyBus
55
58
  def initialize(log: nil, dead: nil, translator: nil, raise_on_dead: false,
56
59
  annotation_prefix: ANNOTATION_PREFIX_DEFAULT)
57
60
  @subs = {}
61
+ @dead_topics = Set.new
58
62
  @translator = translator
59
63
 
60
64
  @total_key = "#{annotation_prefix}total"
@@ -80,6 +84,7 @@ class TinyBus
80
84
  def sub(topic, subber)
81
85
  raise TinyBus::SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
82
86
 
87
+ @dead_topics.delete(topic)
83
88
  @subs[topic] ||= Set.new
84
89
  @subs[topic] << subber
85
90
  @stats[topic] ||= 0
@@ -90,6 +95,7 @@ class TinyBus
90
95
  # removes a subscriber from a topic
91
96
  def unsub(topic, subber)
92
97
  @subs[topic]&.delete(subber)
98
+ @dead_topics << topic if @subs[topic].empty?
93
99
 
94
100
  msg({ @topic_key => 'unsub', 'from_topic' => topic, 'subber' => _to_subber_id(subber) }, 'TINYBUS-UNSUB')
95
101
  end
@@ -118,8 +124,8 @@ class TinyBus
118
124
  subbers = @subs[topic]
119
125
 
120
126
  @stats[topic] ||= 0
121
- @stats[topic] += 1
122
127
  @stats[@total_key] += 1
128
+ @stats[topic] += 1
123
129
  if (subbers&.length || 0) > 0
124
130
  subbers.each{|s| s.msg(msg) }
125
131
  @log.send(lvl, "S #{msg}")
@@ -128,6 +134,7 @@ class TinyBus
128
134
  raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{topic}'")
129
135
  else
130
136
  @stats[@dead_key] += 1
137
+ @dead_topics << topic
131
138
  @dead.send(lvl, "D #{msg}")
132
139
  end
133
140
  end
@@ -144,9 +151,12 @@ class TinyBus
144
151
  # there are no subscribes for a given topic
145
152
  def to_s
146
153
  <<~DEBUG
147
- TinyBus stats: #{@stats.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
148
- Topics & Subscribers:
149
- #{@subs.map{|topic, subbers| "#{topic}\n #{subbers.map(&:to_s).join("\n ")}" }.join("\n ") }
154
+ TinyBus stats: #{@stats.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
155
+ Dead topics: [
156
+ #{@dead_topics.sort.each_slice(3).map{|slice| slice.join(', ') }.join("\n ")}
157
+ ]
158
+ Topics & Subscribers:
159
+ #{@subs.map{|topic, subbers| "#{topic}:\n #{subbers.map(&:to_s).join("\n ")}" }.join("\n ") }
150
160
  DEBUG
151
161
  end
152
162
  end
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.7.1
4
+ version: 3.8.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-05 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiny_log