message_bus 2.0.9 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of message_bus might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 558fcebb3ac3abe9031039c73f996264da91cf27
4
- data.tar.gz: f1bc3ef44e127a51952a8f27acf33e0980494a51
3
+ metadata.gz: 6609e7158e64715d6113ffb1c2d61d99c919d16b
4
+ data.tar.gz: d561cb2c1342bc50cca592676f7f45c6eb50326e
5
5
  SHA512:
6
- metadata.gz: 64fde0c29746bd402f04c9502aa304b700ea8b27eff505a9385f88de11f054cdb84f12f006478b6efcc4de9b897dbaf2c0d4e0ac485497ce32fa7e47ead9caae
7
- data.tar.gz: 3f8dd7191ef89cdf0ec82211098a5a310382d91525c9ea39006d326f13e2e61b5b59dac4ddc6b77b60e1fd74973d00d0d7a12eb9913c772abc2b3cc4091dd740
6
+ metadata.gz: 9f42d527e19b5a9e15edabb29198a860447fb2e57697b2d946aa1bac26e5d2c8eec860a8839ff6fede461e61a050c806ae9fbfba39ff6e2ae0a945238d3921e6
7
+ data.tar.gz: 271dc3f565aa0dde4f2e513f5fb9cddaf4e59680bbe5b87136708c042020de47c9469cdf56d8f9d5025ce1272138e9ac7b4adfde5713ed896b7a9002bf8cbc42
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 18-12-2017
2
+
3
+ - Version 2.1.0
4
+
5
+ - FEATURE: you can now lookup last N messages on channel on subscribe from JavaScript
6
+ Subscribe at position (-1 - numberOfMessages) from the client
7
+
1
8
  24-11-2017
2
9
 
3
10
  - Version 2.0.9
data/README.md CHANGED
@@ -196,10 +196,28 @@ MessageBus.start(); // call once at startup
196
196
 
197
197
  // how often do you want the callback to fire in ms
198
198
  MessageBus.callbackInterval = 500;
199
+
200
+ // you will get all new messages sent to channel
199
201
  MessageBus.subscribe("/channel", function(data){
200
202
  // data shipped from server
201
203
  });
202
204
 
205
+
206
+ // you will get all new messages sent to channel (-1 is implicit)
207
+ MessageBus.subscribe("/channel", function(data){
208
+ // data shipped from server
209
+ }, -1);
210
+
211
+
212
+ // all messages AFTER message id 7 AND all new messages
213
+ MessageBus.subscribe("/channel", function(data){
214
+ // data shipped from server
215
+ }, 7);
216
+
217
+ // last 2 messages in channel AND all new messages
218
+ MessageBus.subscribe("/channel", function(data){
219
+ // data shipped from server
220
+ }, -3);
203
221
  ```
204
222
 
205
223
  There is also a Ruby implementation of the client library, at
@@ -365,15 +365,21 @@
365
365
  },
366
366
 
367
367
  // Subscribe to a channel
368
+ // if lastId is 0 or larger, it will recieve messages AFTER that id
369
+ // if lastId is nagative it will perform lookbehind"
370
+ // -1 will subscribe to all new messages
371
+ // -2 will recieve last message + all new messages
372
+ // -3 will recieve last 2 messages + all new messages
368
373
  subscribe: function(channel, func, lastId) {
369
374
 
370
375
  if(!started && !stopped){
371
376
  me.start();
372
377
  }
373
378
 
374
- if (typeof(lastId) !== "number" || lastId < -1){
379
+ if (typeof(lastId) !== "number") {
375
380
  lastId = -1;
376
381
  }
382
+
377
383
  callbacks.push({
378
384
  channel: channel,
379
385
  func: func,
@@ -1,4 +1,4 @@
1
- base_image: "discourse/base:1.0.15"
1
+ base_image: "discourse/base:2.0.20171204"
2
2
 
3
3
  update_pups: false
4
4
 
@@ -121,11 +121,19 @@ class MessageBus::Client
121
121
  new_message_ids = nil
122
122
 
123
123
  @subscriptions.each do |k, v|
124
- next if v.to_i < 0
125
- messages = @bus.backlog(k, v, site_id)
124
+ id = v.to_i
125
+
126
+ if id < -1
127
+ last_id = @bus.last_id(k, site_id)
128
+ id = last_id + id + 1
129
+ id = 0 if id < 0
130
+ end
131
+
132
+ next if id < 0
133
+ messages = @bus.backlog(k, id, site_id)
126
134
 
127
135
  if messages.length == 0
128
- if v.to_i > @bus.last_id(k, site_id)
136
+ if id > @bus.last_id(k, site_id)
129
137
  @subscriptions[k] = -1
130
138
  end
131
139
  else
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module MessageBus
3
- VERSION = "2.0.9"
3
+ VERSION = "2.1.0"
4
4
  end
@@ -29,7 +29,7 @@ describe MessageBus::Client do
29
29
 
30
30
  while line = lines.shift
31
31
  break if line == ""
32
- name,val = line.split(": ")
32
+ name, val = line.split(": ")
33
33
  headers[name] = val
34
34
  end
35
35
 
@@ -39,7 +39,7 @@ describe MessageBus::Client do
39
39
  break if length == 0
40
40
  rest = lines.join("\r\n")
41
41
  chunks << rest[0...length]
42
- lines = (rest[length+2..-1] || "").split("\r\n")
42
+ lines = (rest[length + 2..-1] || "").split("\r\n")
43
43
  end
44
44
 
45
45
  # split/join gets tricky
@@ -49,15 +49,15 @@ describe MessageBus::Client do
49
49
  end
50
50
 
51
51
  def parse_chunk(data)
52
- payload,_ = data.split(/\r\n\|\r\n/m)
52
+ payload, _ = data.split(/\r\n\|\r\n/m)
53
53
  JSON.parse(payload)
54
54
  end
55
55
 
56
56
  it "can chunk replies" do
57
57
  @client.use_chunked = true
58
- r,w = IO.pipe
58
+ r, w = IO.pipe
59
59
  @client.io = w
60
- @client.headers = {"Content-Type" => "application/json; charset=utf-8"}
60
+ @client.headers = { "Content-Type" => "application/json; charset=utf-8" }
61
61
  @client << MessageBus::Message.new(1, 1, '/test', 'test')
62
62
  @client << MessageBus::Message.new(2, 2, '/test', "a|\r\n|\r\n|b")
63
63
 
@@ -84,7 +84,7 @@ describe MessageBus::Client do
84
84
 
85
85
  data[-5..-1].must_equal "0\r\n\r\n"
86
86
 
87
- _,_,chunks = http_parse("HTTP/1.1 200 OK\r\n\r\n" << data)
87
+ _, _, chunks = http_parse("HTTP/1.1 200 OK\r\n\r\n" << data)
88
88
 
89
89
  chunks.length.must_equal 2
90
90
 
@@ -116,6 +116,18 @@ describe MessageBus::Client do
116
116
  log[0].data.must_equal("/hello" => 0)
117
117
  end
118
118
 
119
+ it "allows negative subscribes to look behind" do
120
+
121
+ @bus.publish '/hello', 'world'
122
+ @bus.publish '/hello', 'sam'
123
+
124
+ @client.subscribe('/hello', -2)
125
+
126
+ log = @client.backlog
127
+ log.length.must_equal(1)
128
+ log[0].data.must_equal("sam")
129
+ end
130
+
119
131
  it "provides status" do
120
132
  @client.subscribe('/hello', -1)
121
133
  log = @client.backlog
@@ -139,7 +151,7 @@ describe MessageBus::Client do
139
151
  log = another_client.backlog
140
152
  log.length.must_equal 1
141
153
  log[0].channel.must_equal '/__status'
142
- log[0].data.must_equal({ '/hello' => 1 })
154
+ log[0].data.must_equal('/hello' => 1)
143
155
  end
144
156
 
145
157
  it "should provide a list of subscriptions" do
@@ -158,7 +170,7 @@ describe MessageBus::Client do
158
170
 
159
171
  it "allows only client_id in list if message contains client_ids" do
160
172
  @message = MessageBus::Message.new(1, 2, '/test', 'hello')
161
- @message.client_ids = ["1","2"]
173
+ @message.client_ids = ["1", "2"]
162
174
  @client.client_id = "2"
163
175
  @client.allowed?(@message).must_equal true
164
176
 
@@ -168,23 +180,23 @@ describe MessageBus::Client do
168
180
 
169
181
  describe "targetted at group" do
170
182
  before do
171
- @message = MessageBus::Message.new(1,2,'/test', 'hello')
172
- @message.group_ids = [1,2,3]
183
+ @message = MessageBus::Message.new(1, 2, '/test', 'hello')
184
+ @message.group_ids = [1, 2, 3]
173
185
  end
174
186
 
175
187
  it "denies users that are not members of group" do
176
- @client.group_ids = [77,0,10]
188
+ @client.group_ids = [77, 0, 10]
177
189
  @client.allowed?(@message).must_equal false
178
190
  end
179
191
 
180
192
  it "allows users that are members of group" do
181
- @client.group_ids = [1,2,3]
193
+ @client.group_ids = [1, 2, 3]
182
194
  @client.allowed?(@message).must_equal true
183
195
  end
184
196
 
185
197
  it "allows all users if groups not set" do
186
198
  @message.group_ids = nil
187
- @client.group_ids = [77,0,10]
199
+ @client.group_ids = [77, 0, 10]
188
200
  @client.allowed?(@message).must_equal true
189
201
  end
190
202
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.9
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2017-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack