power_strip 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0c9e3d8262f810ab4fb28ccf752acbfafcaa6628
4
- data.tar.gz: fcfa3d47d3739d813a03ae51664bd6efa68be337
2
+ SHA256:
3
+ metadata.gz: 653cf22671aa77f68902a8e7d90d08fd4e0c783f2c748f39f0af8f79ebfd225c
4
+ data.tar.gz: 3937236883fe65bc62e6cc75f46944b82d0a0a05fbb68f9f4bfd555a03162dd5
5
5
  SHA512:
6
- metadata.gz: 4f2e2aff4dbc161af1a06d9e1f26284e6202ccb238c4ac9e4870ef437a2b09cb69ac6a617a2e5945b0571de26e9e86c69ffad212c2c7fbdf72aaac2cd8d9ef2b
7
- data.tar.gz: fff40297418a13a67dc4d0ecbe7fd2b8f30d7aa2a91306c04c6d570a45c08d3fe1a414b54f6846bf184e2e7504f206b8017e6adb21aaed0c03b6394c87c55306
6
+ metadata.gz: 1c0c35f302b3f82ad8a8008badf015e4a8b421082c7ffd37b52d91c75ff77e2460c8a680ae77047551129880e1dd56e01439fe78dc15c6c0071eb8b706d09449
7
+ data.tar.gz: fe43d7691d6885a32064036383cd8e63d6edcfaec5c959df65a09f196354678f0e61b69667b5d9d1d1b6b679cf81a2ea2bc42ba4a6d07f666be673363ccb9d95
@@ -45,24 +45,25 @@ module PowerStrip
45
45
  subscriptions << channel
46
46
 
47
47
  socket.send({
48
- event: :subscribed,
49
48
  channel: channel_name,
49
+ event: :subscribed,
50
50
  }.to_json)
51
51
  when '@unsubscribe'
52
52
  subscriptions.delete channel
53
53
  channel.delete socket
54
54
 
55
55
  socket.send({
56
- event: :unsubscribed,
57
56
  channel: channel_name,
57
+ event: :unsubscribed,
58
58
  }.to_json)
59
- else
60
- @handlers[channel_name][message.event].each do |callback|
61
- begin
62
- callback[message, @connections[socket]]
63
- rescue => e
64
- warn "[PowerStrip] #{e.inspect}"
65
- end
59
+ end
60
+
61
+ @handlers[channel_name][message.event].each do |callback|
62
+ begin
63
+ callback[message.data, @connections[socket]]
64
+ rescue => e
65
+ warn "[PowerStrip] #{e.inspect}"
66
+ warn e.backtrace
66
67
  end
67
68
  end
68
69
  rescue JSON::ParserError
@@ -71,13 +72,29 @@ module PowerStrip
71
72
  end
72
73
 
73
74
  socket.on :open do
74
- @connections[socket] = Connection.new(socket)
75
+ connection = Connection.new(socket)
76
+ @connections[socket] = connection
77
+ @handlers['meta'.freeze]['open'].each do |callback|
78
+ begin
79
+ callback[connection]
80
+ rescue => e
81
+ warn "[PowerStrip] #{e.inspect}"
82
+ end
83
+ end
75
84
  end
76
85
 
77
86
  socket.on :close do
78
87
  # Remove this connection from all channels it was subscribed to.
79
88
  subscriptions.each { |channel| channel.delete socket }
80
- @connections.delete socket
89
+ connection = @connections.delete socket
90
+ channels.prune
91
+ @handlers['meta'.freeze]['close'].each do |callback|
92
+ begin
93
+ callback[connection]
94
+ rescue => e
95
+ warn "[PowerStrip] #{e.inspect}"
96
+ end
97
+ end
81
98
  end
82
99
 
83
100
  socket.rack_response
@@ -90,6 +107,10 @@ module PowerStrip
90
107
  @handlers[channel.to_s][event.to_s] << block
91
108
  end
92
109
 
110
+ def close channel_name
111
+ @handlers.delete channel_name.to_s
112
+ end
113
+
93
114
  def listen
94
115
  redis.dup.subscribe(:power_strip) do |on|
95
116
  on.message do |_, message|
@@ -37,5 +37,9 @@ module PowerStrip
37
37
  @redis.publish :power_strip, message.to_json
38
38
  self
39
39
  end
40
+
41
+ def close
42
+ PowerStrip.close name
43
+ end
40
44
  end
41
45
  end
@@ -2,6 +2,8 @@ require 'power_strip/channel'
2
2
 
3
3
  module PowerStrip
4
4
  class ChannelList
5
+ include Enumerable
6
+
5
7
  def initialize(redis:)
6
8
  @redis = redis
7
9
  @channels = Hash.new do |hash, channel|
@@ -17,6 +19,10 @@ module PowerStrip
17
19
  @channels[name.to_s]
18
20
  end
19
21
 
22
+ def each
23
+ @channels.each_value { |channel| yield channel }
24
+ end
25
+
20
26
  def to_a
21
27
  @channels.values
22
28
  end
@@ -28,5 +34,20 @@ module PowerStrip
28
34
  def has_channel? channel_name
29
35
  @channels.key? channel_name.to_s
30
36
  end
37
+
38
+ def inspect
39
+ longest_name = @channels.each_key.max_by(&:length).length
40
+
41
+ <<-EOF
42
+ #<#{self.class.name}:0x#{(object_id * 2).to_s(16)}
43
+ #{@channels.map { |name, channel| " #{name.rjust(longest_name)}: #{channel.sockets.length}" }.join("\n") }>
44
+ EOF
45
+ end
46
+
47
+ def prune
48
+ @channels.reject! do |name, channel|
49
+ channel.empty?
50
+ end
51
+ end
31
52
  end
32
53
  end
@@ -15,6 +15,15 @@ module PowerStrip
15
15
  @metadata[attribute] = value
16
16
  end
17
17
 
18
+ def send message
19
+ case message
20
+ when Message, Hash
21
+ @socket.send message.to_json
22
+ when String
23
+ @socket.send message
24
+ end
25
+ end
26
+
18
27
  def method_missing *args, &block
19
28
  @socket.public_send *args, &block
20
29
  end
@@ -1,3 +1,3 @@
1
1
  module PowerStrip
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/power_strip.rb CHANGED
@@ -20,7 +20,14 @@ module PowerStrip
20
20
 
21
21
  def start(**args)
22
22
  @app = App.instance(**args)
23
- @thread = Thread.new { app.listen }
23
+ @thread = Thread.new do
24
+ begin
25
+ app.listen
26
+ rescue => e
27
+ warn "[PowerStrip Handler Thread] #{e}"
28
+ warn e.backtrace
29
+ end
30
+ end
24
31
  end
25
32
 
26
33
  def [] channel
@@ -31,6 +38,10 @@ module PowerStrip
31
38
  app.on event_name, channel: channel, &block
32
39
  end
33
40
 
41
+ def close channel_name
42
+ app.close channel_name
43
+ end
44
+
34
45
  class Channel
35
46
  def on message, &block
36
47
  PowerStrip.on(message, channel: name, &block)
data/opal/power_strip.rb CHANGED
@@ -5,14 +5,20 @@ module PowerStrip
5
5
  class Client
6
6
  attr_reader :url
7
7
 
8
- def initialize(url)
8
+ def initialize(url, auto_resubscribe: true)
9
9
  @url = url
10
10
  @socket = Bowser.websocket(url)
11
11
  @socket.autoreconnect!
12
12
  @subscriptions = {}
13
13
 
14
14
  @socket.on :open do
15
- @subscriptions.each { |name, channel| channel.subscribe }
15
+ if auto_resubscribe
16
+ @subscriptions.each do |name, channel|
17
+ channel.subscribe
18
+ end
19
+ else
20
+ @subscriptions = {}
21
+ end
16
22
  end
17
23
  @socket.on :message do |event|
18
24
  message = Message.new(event.data)
@@ -34,11 +40,10 @@ module PowerStrip
34
40
  end
35
41
 
36
42
  def unsubscribe channel_name
37
- channel = @subscriptions[channel_name]
43
+ channel = @subscriptions.delete(channel_name)
38
44
  return if channel.nil?
39
45
 
40
- @subscriptions.delete channel
41
- channel.unsubscribe
46
+ channel.unsubscribe if @socket.connected?
42
47
 
43
48
  channel
44
49
  end
@@ -96,7 +101,7 @@ module PowerStrip
96
101
 
97
102
  def receive_message message
98
103
  @handlers[message.event].each do |handler|
99
- handler.call message
104
+ handler.call message.data
100
105
  end
101
106
  end
102
107
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power_strip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Gaskins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-26 00:00:00.000000000 Z
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  requirements: []
154
154
  rubyforge_project:
155
- rubygems_version: 2.6.8
155
+ rubygems_version: 2.7.3
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Push data from server to client with Ruby