tiny_bus 3.0.0 → 3.1.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_bus.rb +24 -10
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33fd7e7b558ef0142c5edccdd2f680edd44b1b857a9bd6b39c6c6c554b3fc007
4
- data.tar.gz: 75c523e3d559488de425a0ebe96c2e36253d85e226f2cc83de35c72bc56254b3
3
+ metadata.gz: 27b59f1cbc599aa563486a8fb29274e2369b1099dc01523f96b14bc9f3f0d06f
4
+ data.tar.gz: e8f3511275ca57d791808e343e9f70e4855cbaea72a1eb119935b09b4acbbb4e
5
5
  SHA512:
6
- metadata.gz: 8e12dff36fe08ad554645939c3b5509e9f8718bbb4eedc4819a1dbb53460093459c1e77fae52b1dd6b20ae4b25fe840a06651c446c46b8dcfac81c0a8b9a065e
7
- data.tar.gz: f30197ba6ef9502d38066d6223789c2c6dccdf704abc289b1f022e8cbc8aa75bcd14ea5c3553546d5e7ca5bb9d8f1ec63370da167d55fb576d79f1373bc73fe5
6
+ metadata.gz: adfd193f7b1c3f55c66f86d5a096d21b0d1d8e5fb04c2c978cf9885cdb8663ca3d2850dfda25c4debb4b93e5407dd601428f76a184a72f7a1210b0abf7a2ce03
7
+ data.tar.gz: 34c31596855df2ac266fd839de02b84c1827cc64ec4ecf2d295cf98748364604be1285c9b0efb9278392fadadd23553d5d342cb76f9b67a037c12c51443361c0
data/lib/tiny_bus.rb CHANGED
@@ -6,7 +6,7 @@ require 'tiny_pipe'
6
6
 
7
7
  # NOTE: This library depends on the TinyLog library.
8
8
  #
9
- # This class implements a very simpler PubSub system where:
9
+ # This class implements a very simple PubSub system where:
10
10
  # - subscribers can subscribe via the #sub method
11
11
  # - subscribers can unsubscribe via the #unsub method
12
12
  # - msgs can enter the TinyBus via the #msg method
@@ -27,6 +27,8 @@ require 'tiny_pipe'
27
27
  # TinyBus.new(dead: <a TinyLog for dead messages>) # will log all undeliverable msgs in this file
28
28
  # TinyBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
29
29
  class TinyBus
30
+ ANNOTATION_PREFIX_DEFAULT = '.'
31
+
30
32
  # log:
31
33
  # if specified, it should be a TinyLog instance
32
34
  # if not specified, it will create a new TinyLog instance for $stdout
@@ -46,13 +48,25 @@ class TinyBus
46
48
  # kind of a strict mode. if false, then messages with a '.topic' with no
47
49
  # subscribers will go to the dead file. if true, then messages with a
48
50
  # '.topic' with no subscribers will raise an exception.
49
- def initialize(log: nil, dead: nil, translator: nil, raise_on_dead: false)
51
+ # annotation_prefix:
52
+ # default: '.'
53
+ # if specified, the annotated message attributes ('.time', '.msg_uuid', and
54
+ # '.trace') will have the dot ('.') replaced with the specified prefix text
55
+ def initialize(log: nil, dead: nil, translator: nil, raise_on_dead: false,
56
+ annotation_prefix: ANNOTATION_PREFIX_DEFAULT)
50
57
  @subs = {}
51
58
  @translator = translator
59
+
60
+ @dead_key = "#{annotation_prefix}dead"
61
+ @topic_key = "#{annotation_prefix}topic"
62
+ @time_key = "#{annotation_prefix}time"
63
+ @msg_uuid_key = "#{annotation_prefix}msg_uuid"
64
+ @trace_key = "#{annotation_prefix}trace"
65
+
52
66
  @annotator = TinyPipe.new([
53
- ->(m){ m['.time'] = (Time.now.to_f * 1000).to_i; m },
54
- ->(m){ m['.msg_uuid'] = SecureRandom.uuid; m },
55
- ->(m){ m['.trace'] ||= SecureRandom.uuid; m }
67
+ ->(m){ m[@time_key] = (Time.now.to_f * 1000).to_i; m },
68
+ ->(m){ m[@msg_uuid_key] = SecureRandom.uuid; m },
69
+ ->(m){ m[@trace_key] ||= SecureRandom.uuid; m }
56
70
  ])
57
71
 
58
72
  @stats = { '.dead' => 0 }
@@ -69,14 +83,14 @@ class TinyBus
69
83
  @subs[topic] << subber
70
84
  @stats[topic] ||= 0
71
85
 
72
- msg({ '.topic' => 'sub', 'to_topic' => topic, 'subber' => subber.to.s })
86
+ msg({ @topic_key => 'sub', 'to_topic' => topic, 'subber' => subber.to_s })
73
87
  end
74
88
 
75
89
  # removes a subscriber from a topic
76
90
  def unsub(topic, subber)
77
91
  @subs[topic]&.delete(subber)
78
92
 
79
- msg({ '.topic' => 'unsub', 'from_topic' => topic, 'subber' => subber.to.s })
93
+ msg({ @topic_key => 'unsub', 'from_topic' => topic, 'subber' => subber.to_s })
80
94
  end
81
95
 
82
96
  # takes an incoming message and distributes it to subscribers
@@ -94,11 +108,11 @@ class TinyBus
94
108
  msg = @annotator.pipe(msg)
95
109
  msg = @translator&.pipe(msg) || msg
96
110
 
97
- topic = msg['topic']
111
+ topic = msg[@topic_key]
98
112
 
99
113
  subbers = @subs[topic]
100
114
 
101
- if subbers
115
+ if (subbers&.length || 0) > 0
102
116
  @stats[topic] += 1
103
117
  subbers.each{|s| s.msg(msg) }
104
118
  @log.sent msg
@@ -106,7 +120,7 @@ class TinyBus
106
120
  if @raise_on_dead
107
121
  raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{topic}'")
108
122
  else
109
- @stats['.dead'] += 1
123
+ @stats[@dead_key] += 1
110
124
  @dead.dead msg
111
125
  end
112
126
  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.0.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-24 00:00:00.000000000 Z
11
+ date: 2022-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiny_log