rasti-web 0.2.1 → 0.2.2

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
2
  SHA1:
3
- metadata.gz: ec9566c3c8cf96790406973506e6d9322ec10bc2
4
- data.tar.gz: 25e1535210a4212bb3733281376cfc7b2f3384ea
3
+ metadata.gz: 9172df969941d8ed2283476d1dc516f664db5f5d
4
+ data.tar.gz: 241e91821c0b1ebcea756210a53371c7ee66da7c
5
5
  SHA512:
6
- metadata.gz: d0f0f7cdbc4ddc35f0a8638c9537507b13622e41fc792104d4e4275ac8a366b9460cddb8b50d892626bce440546411b3663e89af1b064a2e2143bd9821d7d383
7
- data.tar.gz: 069d05769cb012f2b3f987f1317570a429951c9713eae3c1c37ad1b2928f00b357cf08c5df48680fc30f620190273470af33bfbde2f2fa918019c5f53637b72a
6
+ metadata.gz: 04d240bc622d210776cbab344355d7bc076adf3958a20eea4a7b28b3e2bad1d0bf629986e34fa32565b60263eaf6b74356e2b60307c419a50e9014d87d1be95b
7
+ data.tar.gz: dce35f9da9aee7a7de97881601301e6b44f71ab7710ec424fa691e9889591f2578e3bbb6ab02a692e4550791fdc9233c5430ad580d5f0c9dfcdff91e00aa1228
@@ -1,26 +1,39 @@
1
1
  module Rasti
2
2
  module Web
3
+
3
4
  class Channel
4
5
 
5
- attr_reader :streams
6
+ attr_reader :id
6
7
 
7
8
  def initialize(id)
9
+ @id = id
10
+ @subscriptions = {}
8
11
  @mutex = Mutex.new
9
- @channel = Restruct::Channel.new id: Restruct::Id[Web.channels_prefix][id]
10
- @streams = []
11
- listen
12
12
  end
13
13
 
14
14
  def subscribe
15
- Stream.new.tap do |stream|
16
- mutex.synchronize do
17
- streams << stream
15
+ stream = Stream.new
16
+
17
+ subscription_id = broadcaster.subscribe id do |message|
18
+ if stream.open?
19
+ stream.write message
20
+ else
21
+ sid = mutex.synchronize { subscriptions.delete stream }
22
+ broadcaster.unsubscribe sid
18
23
  end
19
24
  end
25
+
26
+ mutex.synchronize { subscriptions[stream] = subscription_id }
27
+
28
+ stream
20
29
  end
21
30
 
22
31
  def publish(message)
23
- channel.publish message
32
+ broadcaster.publish id, message
33
+ end
34
+
35
+ def self.broadcaster
36
+ @broadcaster ||= Broadcaster.new id: Web.channels_prefix
24
37
  end
25
38
 
26
39
  def self.[](id)
@@ -30,26 +43,12 @@ module Rasti
30
43
 
31
44
  private
32
45
 
33
- attr_reader :mutex, :channel
46
+ attr_reader :subscriptions, :mutex
34
47
 
35
- def listen
36
- Thread.new do
37
- channel.subscribe do |message|
38
- broadcast message
39
- end
40
- end
48
+ def broadcaster
49
+ self.class.broadcaster
41
50
  end
42
51
 
43
- def broadcast(message)
44
- mutex.synchronize do
45
- streams.delete_if(&:closed?)
46
- Rasti::Web.logger.debug(Channel) { "Broadcasting (#{streams.count} connections) -> #{message}" } unless streams.empty?
47
- streams.each do |stream|
48
- stream.write message
49
- end
50
- end
51
- end
52
-
53
52
  end
54
53
  end
55
54
  end
@@ -15,7 +15,7 @@ module Rasti
15
15
  end
16
16
 
17
17
  def each
18
- while opened?
18
+ while open?
19
19
  message = @queue.pop
20
20
  yield message if message
21
21
  sleep TIMEOUT
@@ -30,7 +30,7 @@ module Rasti
30
30
  @closed
31
31
  end
32
32
 
33
- def opened?
33
+ def open?
34
34
  !closed?
35
35
  end
36
36
 
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module Web
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
data/lib/rasti/web.rb CHANGED
@@ -7,7 +7,7 @@ require 'class_config'
7
7
  require 'forwardable'
8
8
  require 'logger'
9
9
  require 'hash_ext'
10
- require 'restruct'
10
+ require 'broadcaster'
11
11
 
12
12
  require_relative 'web/route'
13
13
  require_relative 'web/router'
@@ -34,7 +34,7 @@ module Rasti
34
34
  attr_config :default_layout, 'layout'
35
35
  attr_config :helpers, []
36
36
  attr_config :logger, Logger.new(STDOUT)
37
- attr_config :channels_prefix, Restruct::Id['rasti-web']['channels']
37
+ attr_config :channels_prefix, 'rasti-web:channels'
38
38
 
39
39
  after_config do |config|
40
40
  config.helpers.each { |h| ViewContext.send :include, h }
data/rasti-web.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'class_config', '~> 0.0.2'
25
25
  spec.add_dependency 'hash_ext', '~> 0.1'
26
26
  spec.add_dependency 'content-type', '~> 0.0'
27
- spec.add_dependency 'restruct', '~> 0.2'
27
+ spec.add_dependency 'broadcaster', '~> 0.1'
28
28
 
29
29
  spec.add_development_dependency 'bundler', '~> 1.12'
30
30
  spec.add_development_dependency 'rake', '~> 11.0'
@@ -15,4 +15,8 @@ Rasti::Web.configure do |config|
15
15
  config.views_path = File.expand_path '../views', __FILE__
16
16
  config.helpers << ContextMethodHelper
17
17
  config.logger.level = Logger::ERROR
18
+ end
19
+
20
+ Broadcaster.configure do |config|
21
+ config.logger = Rasti::Web.logger
18
22
  end
@@ -5,6 +5,14 @@ describe 'Straming' do
5
5
  let(:request) { Rack::Request.new Hash.new }
6
6
  let(:response) { Rack::Response.new }
7
7
  let(:render) { Rasti::Web::Render.new request, response }
8
+
9
+ def wait_for(&block)
10
+ Timeout.timeout(3) do
11
+ while !block.call
12
+ sleep 0.0001
13
+ end
14
+ end
15
+ end
8
16
 
9
17
  it 'Server sent events' do
10
18
  render.server_sent_events :channel_1
@@ -16,13 +24,13 @@ describe 'Straming' do
16
24
  response.body.must_be_instance_of Rasti::Web::Stream
17
25
 
18
26
  events = []
19
- thread = Thread.new do
27
+
28
+ Thread.new do
20
29
  response.body.each { |e| events << e }
21
30
  end
22
31
 
23
32
  channel_1 = Rasti::Web::Channel[:channel_1]
24
33
  channel_2 = Rasti::Web::Channel[:channel_2]
25
- sleep 0.05 # Wait for establish connection
26
34
 
27
35
  3.times do |i|
28
36
  data = {text: "Tick #{i}"}
@@ -31,11 +39,7 @@ describe 'Straming' do
31
39
  channel_2.publish event
32
40
  end
33
41
 
34
- Timeout.timeout(3) do
35
- while events.count < 3;
36
- sleep 0.0001 # Wait for subscriptions
37
- end
38
- end
42
+ wait_for { events.count == 3 }
39
43
 
40
44
  response.body.close
41
45
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-07 00:00:00.000000000 Z
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -95,19 +95,19 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: restruct
98
+ name: broadcaster
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0.2'
103
+ version: '0.1'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0.2'
110
+ version: '0.1'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: bundler
113
113
  requirement: !ruby/object:Gem::Requirement