magicbus 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,10 +5,14 @@ module MagicBus
5
5
  require_relative 'magicbus/subscriber'
6
6
  require_relative 'magicbus/publisher'
7
7
 
8
+ class << self
9
+ attr_accessor :connection
10
+ end
11
+
8
12
  def self.connect(params = { host: '127.0.0.1' })
9
13
  EM.next_tick do
10
- Subscriber.connection = AMQP.connect(*params)
14
+ self.connection = AMQP.connect(*params)
11
15
  end
12
16
  end
13
17
 
14
- end
18
+ end
@@ -1,23 +1,30 @@
1
1
  class MagicBus::Publisher
2
2
 
3
3
  class << self
4
- attr_accessor :conection
4
+ attr_accessor :connection
5
5
  attr_accessor :channels
6
6
  end
7
7
 
8
- def self.scatter(group, action, model, &block)
8
+ self.channels = {}
9
9
 
10
- MagicBus::Subscriber.connected_users(group).each do |user_id|
10
+ def self.scatter(group, action, model, &block)
11
+
12
+ users = group.users.map(&:id).map(&:to_s)
13
+
14
+ users.each do |user_id|
11
15
  user = User.find(user_id)
12
- self.send_to(user_id, action, model, block.call(user))
16
+ self.send_to(user.id.to_s, action, model, block.call(user))
13
17
  end
14
18
 
15
19
  end
16
20
 
17
21
  def self.broadcast(group, action, model, data = nil)
18
22
 
19
- MagicBus::Subscriber.connected_users(group).each do |user_id|
20
- self.send_to(user_id, action, model, data)
23
+ users = group.users.map(&:id).map(&:to_s)
24
+
25
+ users.each do |user_id|
26
+ user = User.find(user_id)
27
+ self.send_to(user.id.to_s, action, model, data)
21
28
  end
22
29
 
23
30
  end
@@ -25,7 +32,6 @@ class MagicBus::Publisher
25
32
  def self.send_to(user_id, action, model, data)
26
33
 
27
34
  data = { action: action, model: model, data: data }
28
-
29
35
  self.publish(user_id, data)
30
36
 
31
37
  end
@@ -35,7 +41,11 @@ class MagicBus::Publisher
35
41
  def self.publish(user_id, data)
36
42
 
37
43
  user_id = user_id.to_s
38
- channels[user_id] ||= AMQP::Channel.new(connection)
44
+
45
+ unless channels[user_id]
46
+ channels[user_id] = AMQP::Channel.new(MagicBus.connection)
47
+ end
48
+
39
49
  channel = channels[user_id]
40
50
  exchange = channel.fanout('syme')
41
51
  exchange.publish(data.to_json)
@@ -6,22 +6,10 @@ class MagicBus::Subscriber
6
6
  attr_accessor :clients
7
7
  end
8
8
 
9
+ require 'securerandom'
10
+
9
11
  self.channels, self.clients = {}, {}
10
12
 
11
- def self.connected_users(group)
12
-
13
- connected_users = []
14
-
15
- group.users.each do |user|
16
- if self.channels[user.id.to_s]
17
- connected_users << user.id.to_s
18
- end
19
- end
20
-
21
- connected_users
22
-
23
- end
24
-
25
13
  def self.subscribe(user_id, out)
26
14
 
27
15
  user_id = user_id.to_s
@@ -29,22 +17,28 @@ class MagicBus::Subscriber
29
17
  if channels[user_id].nil? ||
30
18
  channels[user_id].status == :closed
31
19
 
32
- channels[user_id] = AMQP::Channel.new(connection)
33
- channel = channels[user_id]
34
- exchange = channel.fanout('syme')
35
-
36
- queue = channel.queue(user_id, auto_delete: true)
37
- .bind(exchange).subscribe do |payload|
38
- self.clients[user_id].each do |client|
39
- client << "data: #{payload}\n\n"
40
- end
41
- end
42
-
20
+ channels[user_id] = AMQP::Channel.new(MagicBus.connection)
21
+
43
22
  end
44
-
45
- self.clients[user_id] ||= []
46
- self.clients[user_id] << out
47
- self.clients[user_id].length
23
+
24
+ channel = channels[user_id]
25
+ exchange = channel.fanout('syme')
26
+
27
+ self.clients[user_id] ||= {}
28
+
29
+ client_id = SecureRandom.uuid
30
+
31
+ self.clients[user_id][client_id] = out
32
+
33
+ queue = channel.queue(client_id)
34
+ .bind(exchange).subscribe do |payload|
35
+
36
+ self.clients[user_id][client_id] <<
37
+ "data: #{payload}\n\n"
38
+
39
+ end
40
+
41
+ client_id
48
42
 
49
43
  end
50
44
 
@@ -53,7 +47,7 @@ class MagicBus::Subscriber
53
47
  user_id = user_id.to_s
54
48
 
55
49
  if self.clients[user_id][client_id]
56
- self.clients[user_id].delete_at(client_id)
50
+ self.clients[user_id][client_id] = nil
57
51
  end
58
52
 
59
53
  if self.clients[user_id].length == 0
@@ -1,5 +1,5 @@
1
1
  module MagicBus
2
2
 
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.9'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magicbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: