isomorfeus-transport 1.0.0.zeta23 → 1.0.0.zeta24

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
  SHA256:
3
- metadata.gz: f880aa11773aa387acfd1e8bcee0b3dc1678c6657341a4dff9612b4e0cbd73d6
4
- data.tar.gz: a17830e51f92e94ea20b1e059dcb6832578a6688583e1579edc1ccbc32d51729
3
+ metadata.gz: 57acf05b4a459474ca19bac1e618d668107a680eda369801eac39ed8da92fa0d
4
+ data.tar.gz: 9c6ec632e4c577d2c6c3eacf361c9454c14cbc7f279846513f0445be93b1ae54
5
5
  SHA512:
6
- metadata.gz: 8cc684ec49a19d7aedb5f1f59c284e78ae426bd9ff03f33874f07a37bf5085f902b803049efebb24ceea982e71ae97c23b59ae323fe510d8d97e09aeb6fab74a
7
- data.tar.gz: 102b5ff34f5127d81ed4cd026b02b7beb87f9c595b2d5cfe49a4a42a675a8147cc9faff7ae5797293d3bf5982e86c5b1321e06a092acf7c195cf5374c4facb52
6
+ metadata.gz: c76f4b1de79e6a3ee13ccdca85f6c8ffc31b778791970b631d33adee9f28b2a43885dfdf75ff36f674d215700ebfb4e853b44692350581c56b8bdf7d85c3c39e
7
+ data.tar.gz: c085ed5a02a6d7040b5e0ca45dd7ec5225ccb39702956953124f2c7f11662ffd1dd9202d733136c9d5d890213c1b6df8c88fa68c648cc0ee74d27a553b0ee7d7
data/README.md CHANGED
@@ -32,5 +32,5 @@ Server only:
32
32
 
33
33
  ## Usage
34
34
 
35
- - [Authentication and Current User](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-policy/docs/authentication.md)
36
- - [Channels (PubSub)](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-policy/docs/channels.md)
35
+ - [Authentication and Current User](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-transport/docs/authentication.md)
36
+ - [Channels (PubSub)](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-transport/docs/channels.md)
@@ -2,7 +2,6 @@ require 'isomorfeus-policy'
2
2
  require 'lucid_authentication/mixin'
3
3
  if RUBY_ENGINE == 'opal'
4
4
  require 'json'
5
- require 'isomorfeus/execution_environment'
6
5
  require 'isomorfeus/transport/version'
7
6
  require 'isomorfeus/transport/config'
8
7
  require 'isomorfeus/transport/request_agent'
@@ -38,7 +37,7 @@ else
38
37
  require 'isomorfeus/transport/server_processor'
39
38
  require 'isomorfeus/transport/server_socket_processor'
40
39
  require 'isomorfeus/transport/websocket'
41
-
40
+ require 'isomorfeus/transport'
42
41
  require 'isomorfeus/transport/rack_middleware'
43
42
  require 'isomorfeus/transport/middlewares'
44
43
 
@@ -4,10 +4,6 @@ module Isomorfeus
4
4
  if RUBY_ENGINE == 'opal'
5
5
  attr_accessor :socket
6
6
 
7
- def delay(ms = 1000, &block)
8
- `setTimeout(#{block.to_n}, #{ms})`
9
- end
10
-
11
7
  def init
12
8
  @requests_in_progress = { requests: {}, agent_ids: {} }
13
9
  @socket = nil
@@ -32,7 +28,7 @@ module Isomorfeus
32
28
  @socket = Isomorfeus::Transport::Websocket.new(ws_url)
33
29
  @socket.on_error do
34
30
  @socket.close
35
- delay 1000 do
31
+ after 1000 do
36
32
  Isomorfeus::Transport.promise_connect
37
33
  end
38
34
  end
@@ -99,14 +95,14 @@ module Isomorfeus
99
95
  begin
100
96
  @socket.send(`JSON.stringify(#{{request: { agent_ids: { agent.id => request }}}.to_n})`)
101
97
  agent.sent = true
102
- delay(Isomorfeus.on_ssr? ? 8000 : 20000) do
98
+ after(Isomorfeus.on_ssr? ? 8000 : 20000) do
103
99
  unless agent.promise.realized?
104
100
  agent.promise.reject({agent_response: { error: 'Request timeout!' }, full_response: {}})
105
101
  end
106
102
  end
107
103
  rescue
108
104
  @socket.close
109
- delay 5000 do
105
+ after 5000 do
110
106
  Isomorfeus::Transport.promise_connect
111
107
  end
112
108
  end
@@ -114,51 +110,41 @@ module Isomorfeus
114
110
  agent.promise
115
111
  end
116
112
 
117
- def send_notification(channel_class, channel, message)
113
+ def send_message(channel_class, channel, message)
118
114
  Isomorfeus.raise_error(message: 'No socket!') unless @socket
119
- @socket.send(`JSON.stringify(#{{notification: { class: channel_class.name, channel: channel, message: message}}.to_n})`)
115
+ @socket.send(`JSON.stringify(#{{ notification: { class: channel_class.name, channel: channel, message: message }}.to_n})`)
120
116
  true
121
117
  end
122
118
 
123
- def subscribe(channel_class, channel, &block)
124
- request = { subscribe: true, class: channel_class.name, channel: channel }
119
+ def promise_subscribe(channel_class_name, channel)
120
+ request = { subscribe: true, class: channel_class_name, channel: channel }
125
121
  if request_in_progress?(request)
126
122
  agent = get_agent_for_request_in_progress(request)
127
123
  else
128
124
  agent = Isomorfeus::Transport::RequestAgent.new(request)
129
125
  register_request_in_progress(request, agent.id)
130
126
  Isomorfeus.raise_error(message: 'No socket!') unless @socket
131
- @socket.send(`JSON.stringify(#{{subscribe: { agent_ids: { agent.id => request }}}.to_n})`)
127
+ @socket.send(`JSON.stringify(#{{ subscribe: { agent_ids: { agent.id => request }}}.to_n})`)
132
128
  end
133
129
  result_promise = agent.promise.then do |agent|
134
130
  agent.response
135
131
  end
136
- if block_given?
137
- result_promise = result_promise.then do |response|
138
- block.call(response)
139
- end
140
- end
141
132
  result_promise
142
133
  end
143
134
 
144
- def unsubscribe(channel_class, channel, &block)
145
- request = { unsubscribe: true, class: channel_class.name, channel: channel }
135
+ def promise_unsubscribe(channel_class_name, channel)
136
+ request = { unsubscribe: true, class: channel_class_name, channel: channel }
146
137
  if request_in_progress?(request)
147
138
  agent = get_agent_for_request_in_progress(request)
148
139
  else
149
140
  agent = Isomorfeus::Transport::RequestAgent.new(request)
150
141
  register_request_in_progress(request, agent.id)
151
142
  Isomorfeus.raise_error(message: 'No socket!') unless @socket
152
- @socket.send(`JSON.stringify(#{{unsubscribe: { agent_ids: { agent.id => request }}}.to_n})`)
143
+ @socket.send(`JSON.stringify(#{{ unsubscribe: { agent_ids: { agent.id => request }}}.to_n})`)
153
144
  end
154
145
  result_promise = agent.promise.then do |agent|
155
146
  agent.response
156
147
  end
157
- if block_given?
158
- result_promise = result_promise.then do |response|
159
- block.call(response)
160
- end
161
- end
162
148
  result_promise
163
149
  end
164
150
 
@@ -189,23 +175,20 @@ module Isomorfeus
189
175
  @requests_in_progress[:requests].delete(request)
190
176
  end
191
177
  else # RUBY_ENGINE
192
- def send_notification(channel_class, channel, message)
193
- Thread.current[:isomorfeus_pub_sub_client].publish(Oj.dump({notification: { class: channel_class.name, channel: channel, message: message}}, mode: :strict))
178
+ def send_message(channel_class, channel, message)
179
+ channel_class_name = channel_class.name
180
+ Isomorfeus.pub_sub_client.publish("#{channel_class_name}_#{channel}", Oj.dump({notification: { class: channel_class_name, channel: channel, message: message}}, mode: :strict))
194
181
  true
195
182
  end
196
183
 
197
- def subscribe(channel_class, channel, &block)
198
- Thread.current[:isomorfeus_pub_sub_client].subscribe(channel)
199
- result_promise = Promise.new
200
- result_promise.resolve({ success: channel })
201
- result_promise
184
+ def promise_subscribe(channel_class, channel, &block)
185
+ Isomorfeus.pub_sub_client.subscribe(channel)
186
+ Promise.new.resolve({ success: channel })
202
187
  end
203
188
 
204
- def unsubscribe(channel_class, channel, &block)
205
- Thread.current[:isomorfeus_pub_sub_client].unsubscribe(channel)
206
- result_promise = Promise.new
207
- result_promise.resolve({ success: channel })
208
- result_promise
189
+ def promise_unsubscribe(channel_class, channel, &block)
190
+ Isomorfeus.pub_sub_client.unsubscribe(channel)
191
+ Promise.new.resolve({ success: channel })
209
192
  end
210
193
  end # RUBY_ENGINE
211
194
  end
@@ -5,13 +5,18 @@ module Isomorfeus
5
5
  if json_hash.key?(:response)
6
6
  process_response(json_hash)
7
7
  elsif json_hash.key?(:notification)
8
- process_notification(json_hash)
8
+ process_message(json_hash)
9
9
  end
10
10
  end
11
11
 
12
- def self.process_notification(notification_hash)
13
- processor_class = "::#{notification_hash[:notification][:class]}".constantize
14
- processor_class.process_message(notification_hash[:notification][:channel], notification_hash[:notification][:message])
12
+ def self.process_message(message_hash)
13
+ processor_class_name = message_hash[:notification][:class]
14
+ Isomorfeus.raise_error(message: "Not a valid channel class #{processor_class_name}!") unless Isomorfeus.valid_channel_class_name?(processor_class_name)
15
+ processor_class = Isomorfeus.cached_channel_class(processor_class_name)
16
+ unless processor_class.respond_to?(:process_message)
17
+ Isomorfeus.raise_error(message: "Cannot process message, #{processor_class} must be a Channel and must have the on_message block defined!")
18
+ end
19
+ processor_class.process_message(message_hash[:notification][:message], message_hash[:notification][:error], message_hash[:notification][:channel])
15
20
  end
16
21
 
17
22
  def self.process_response(response_hash)
@@ -27,4 +32,4 @@ module Isomorfeus
27
32
  end
28
33
  end
29
34
  end
30
- end
35
+ end
@@ -1,33 +1,65 @@
1
1
  module Isomorfeus
2
2
  # available settings
3
+ class << self
4
+ def cached_channel_classes
5
+ @cached_channel_classes ||= {}
6
+ end
7
+
8
+ def cached_channel_class(class_name)
9
+ return "::#{class_name}".constantize if Isomorfeus.development?
10
+ return cached_channel_classes[class_name] if cached_channel_classes.key?(class_name)
11
+ cached_channel_classes[class_name] = "::#{class_name}".constantize
12
+ end
13
+
14
+ def valid_channel_class_names
15
+ @valid_channel_class_names ||= Set.new
16
+ end
17
+
18
+ def add_valid_channel_class(klass)
19
+ valid_channel_class_names << raw_class_name(klass)
20
+ end
21
+
22
+ def raw_class_name(klass)
23
+ class_name = klass.name
24
+ class_name = class_name.split('>::').last if class_name.start_with?('#<')
25
+ class_name
26
+ end
27
+ end
3
28
 
4
29
  if RUBY_ENGINE == 'opal'
5
30
  add_client_option(:api_websocket_path)
6
31
  add_client_option(:cookie_eater_path)
7
32
  add_client_option(:transport_init_class_names, [])
8
33
 
9
- def self.add_transport_init_class_name(init_class_name)
10
- transport_init_class_names << init_class_name
11
- end
34
+ class << self
35
+ def valid_channel_class_name?(class_name)
36
+ cached_channel_class(class_name) # because of autoloader
37
+ valid_channel_class_names.include?(class_name)
38
+ end
12
39
 
13
- def self.current_user
14
- @current_user ||= init_current_user
15
- end
40
+ def add_transport_init_class_name(init_class_name)
41
+ transport_init_class_names << init_class_name
42
+ end
16
43
 
17
- def self.init_current_user
18
- if Isomorfeus.current_user_sid
19
- Isomorfeus.instance_from_sid(Isomorfeus.current_user_sid)
20
- else
21
- Anonymous.new
44
+ def current_user
45
+ @current_user ||= init_current_user
46
+ end
47
+
48
+ def init_current_user
49
+ if Isomorfeus.current_user_sid
50
+ Isomorfeus.instance_from_sid(Isomorfeus.current_user_sid)
51
+ else
52
+ Anonymous.new
53
+ end
22
54
  end
23
- end
24
55
 
25
- def self.set_current_user(user)
26
- if user
27
- @current_user = user
28
- Isomorfeus.current_user_sid = user.to_sid
29
- else
30
- @current_user = Anonymous.new
56
+ def set_current_user(user)
57
+ if user
58
+ @current_user = user
59
+ Isomorfeus.current_user_sid = user.to_sid
60
+ else
61
+ @current_user = Anonymous.new
62
+ end
31
63
  end
32
64
  end
33
65
  else
@@ -37,6 +69,10 @@ module Isomorfeus
37
69
  attr_accessor :session_store
38
70
  attr_accessor :cookie_dbm_path
39
71
 
72
+ def valid_channel_class_name?(class_name)
73
+ valid_channel_class_names.include?(class_name)
74
+ end
75
+
40
76
  def add_middleware(middleware)
41
77
  Isomorfeus.middlewares << middleware
42
78
  end
@@ -67,28 +103,6 @@ module Isomorfeus
67
103
  @middlewares ||= Set.new
68
104
  end
69
105
 
70
- def cached_channel_classes
71
- @cached_channel_classes ||= {}
72
- end
73
-
74
- def cached_channel_class(class_name)
75
- return "::#{class_name}".constantize if Isomorfeus.development?
76
- return cached_channel_classes[class_name] if cached_channel_classes.key?(class_name)
77
- cached_channel_classes[class_name] = "::#{class_name}".constantize
78
- end
79
-
80
- def valid_channel_class_names
81
- @valid_channel_class_names ||= Set.new
82
- end
83
-
84
- def valid_channel_class_name?(class_name)
85
- valid_channel_class_names.include?(class_name)
86
- end
87
-
88
- def add_valid_channel_class(klass)
89
- valid_channel_class_names << raw_class_name(klass)
90
- end
91
-
92
106
  def valid_handler_class_names
93
107
  @valid_handler_class_names ||= Set.new
94
108
  end
@@ -133,12 +147,6 @@ module Isomorfeus
133
147
  cached_user_classes[class_name] = "::#{class_name}".constantize
134
148
  end
135
149
 
136
- def raw_class_name(klass)
137
- class_name = klass.name
138
- class_name = class_name.split('>::').last if class_name.start_with?('#<')
139
- class_name
140
- end
141
-
142
150
  def current_user
143
151
  Thread.current[:isomorfeus_user]
144
152
  end
@@ -29,26 +29,42 @@ module Isomorfeus
29
29
  elsif request.key?('notification')
30
30
  begin
31
31
  channel = request['notification']['channel']
32
- class_name = request['notification']['class']
33
-
34
- if Isomorfeus.valid_channel_class_name?(class_name) && channel
35
- channel_class = Isomorfeus.cached_channel_class(class_name)
36
- if channel_class && Isomorfeus.current_user.authorized?(channel_class, :send_message, channel)
37
- Isomorfeus.pub_sub_client.publish(request['notification']['channel'], Oj.dump({ 'notification' => request['notification'] }, mode: :strict))
32
+ channel_class_name = request['notification']['class']
33
+ if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
34
+ channel_class = Isomorfeus.cached_channel_class(channel_class_name)
35
+ if channel_class && channel_class.valid_channel?(channel)
36
+ if Isomorfeus.current_user.authorized?(channel_class_name, :send_message, channel)
37
+ allow_publish = if channel_class.server_is_processing_messages?(channel)
38
+ channel_class.server_process_message(request['notification']['message'], channel)
39
+ else
40
+ true
41
+ end
42
+ if allow_publish == true
43
+ Isomorfeus.pub_sub_client.publish("#{channel_class_name}_#{channel}", Oj.dump({ 'notification' => request['notification'] }, mode: :strict))
44
+ else
45
+ response_agent = OpenStruct.new
46
+ response_agent.result = { notification: request['notification'].merge(error: 'Message cancelled!') }
47
+ response_agent_array << response_agent
48
+ end
49
+ else
50
+ response_agent = OpenStruct.new
51
+ response_agent.result = { notification: request['notification'].merge(error: 'Not authorized!') }
52
+ response_agent_array << response_agent
53
+ end
38
54
  else
39
55
  response_agent = OpenStruct.new
56
+ response_agent.result = { notification: request['notification'].merge(error: "Not a valid channel #{channel} for #{channel_class_name}!") }
40
57
  response_agent_array << response_agent
41
- response_agent.result = { response: { error: 'Not authorized!' }}
42
58
  end
43
59
  else
44
60
  response_agent = OpenStruct.new
61
+ response_agent.result = { notification: request['notification'].merge(error: "Not a valid Channel class #{channel_class_name}!") }
45
62
  response_agent_array << response_agent
46
- response_agent.result = { response: { error: 'No such thing!' }}
47
63
  end
48
64
  rescue Exception => e
49
65
  response_agent = OpenStruct.new
66
+ response_agent.result = { notification: request['notification'].merge(error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}") }
50
67
  response_agent_array << response_agent
51
- response_agent.result = { response: { error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}" }}
52
68
  end
53
69
  elsif request.key?('subscribe') && request['subscribe'].key?('agent_ids')
54
70
  begin
@@ -56,17 +72,23 @@ module Isomorfeus
56
72
  response_agent = Isomorfeus::Transport::ResponseAgent.new(agent_id, request['subscribe']['agent_ids'][agent_id])
57
73
  response_agent_array << response_agent
58
74
  channel = response_agent.request['channel']
59
- class_name = response_agent.request['class']
60
- if Isomorfeus.valid_channel_class_name?(class_name) && channel
61
- channel_class = Isomorfeus.cached_channel_class(class_name)
62
- if channel_class && Isomorfeus.current_user.authorized?(channel_class, :subscribe, channel)
63
- Isomorfeus.pub_sub_client.subscribe(channel)
64
- response_agent.agent_result = { success: channel }
75
+ channel_class_name = response_agent.request['class']
76
+ if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
77
+ channel_class = Isomorfeus.cached_channel_class(channel_class_name)
78
+ if channel_class && channel_class.valid_channel?(channel)
79
+ if Isomorfeus.current_user.authorized?(channel_class, :subscribe, channel)
80
+ Isomorfeus.pub_sub_client.subscribe("#{channel_class_name}_#{channel}")
81
+ response_agent.agent_result = { success: channel }
82
+ else
83
+ response_agent.error = { error: "Not authorized!"}
84
+ end
65
85
  else
66
- response_agent.error = { error: "Not authorized!"}
86
+ response_agent = OpenStruct.new
87
+ response_agent.result = { response: { error: "Not a valid channel #{channel} for #{channel_class_name}!" }}
88
+ response_agent_array << response_agent
67
89
  end
68
90
  else
69
- response_agent.error = { error: "No such thing!"}
91
+ response_agent.error = { error: "Not a valid Channel class #{channel_class_name}!" }
70
92
  end
71
93
  rescue Exception => e
72
94
  response_agent.error = { error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}" }
@@ -77,17 +99,23 @@ module Isomorfeus
77
99
  response_agent = Isomorfeus::Transport::ResponseAgent.new(agent_id, request['unsubscribe']['agent_ids'][agent_id])
78
100
  response_agent_array << response_agent
79
101
  channel = response_agent.request['channel']
80
- class_name = response_agent.request['class']
81
- if Isomorfeus.valid_channel_class_name?(class_name) && channel
82
- channel_class = Isomorfeus.cached_channel_class(class_name)
83
- if channel_class && Isomorfeus.current_user.authorized?(channel_class, :unsubscribe, channel)
84
- Isomorfeus.pub_sub_client.unsubscribe(channel)
85
- response_agent.agent_result = { success: channel }
102
+ channel_class_name = response_agent.request['class']
103
+ if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
104
+ channel_class = Isomorfeus.cached_channel_class(channel_class_name)
105
+ if channel_class && channel_class.valid_channel?(channel)
106
+ if Isomorfeus.current_user.authorized?(channel_class, :unsubscribe, channel)
107
+ Isomorfeus.pub_sub_client.unsubscribe("#{channel_class_name}_#{channel}")
108
+ response_agent.agent_result = { success: channel }
109
+ else
110
+ response_agent.error = { error: "Not authorized!"}
111
+ end
86
112
  else
87
- response_agent.error = { error: "Not authorized!"}
113
+ response_agent = OpenStruct.new
114
+ response_agent.result = { response: { error: "Not a valid channel #{channel} for #{channel_class_name}!" }}
115
+ response_agent_array << response_agent
88
116
  end
89
117
  else
90
- response_agent.error = { error: 'No such thing!'}
118
+ response_agent.error = { error: "Not a valid Channel class #{channel_class_name}!" }
91
119
  end
92
120
  rescue Exception => e
93
121
  response_agent.error = { error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}" }
@@ -25,7 +25,7 @@ module Isomorfeus
25
25
  response_agent_array.each do |response_agent|
26
26
  result.deep_merge!(response_agent.result)
27
27
  end
28
- client.write Oj.dump(result, mode: :strict)
28
+ client.write Oj.dump(result, mode: :strict) unless result.empty?
29
29
  ensure
30
30
  Thread.current[:isomorfeus_user] = nil
31
31
  Thread.current[:isomorfeus_pub_sub_client] = nil
@@ -1,5 +1,5 @@
1
1
  module Isomorfeus
2
2
  module Transport
3
- VERSION = '1.0.0.zeta23'
3
+ VERSION = '1.0.0.zeta24'
4
4
  end
5
5
  end
@@ -16,7 +16,7 @@ module LucidAuthentication
16
16
 
17
17
  def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
18
18
  if Isomorfeus.production?
19
- Isomorfeus.raise_error(message: "Connection not secure, can't login") unless Isomorfeus::Transport.socket.url.start_with?('wss:')
19
+ Isomorfeus.raise_error(message: "Connection not secure, can't login!") unless Isomorfeus::Transport.socket.url.start_with?('wss:')
20
20
  else
21
21
  `console.warn("Connection not secure, ensure a secure connection in production, otherwise login will fail!")` unless Isomorfeus::Transport.socket.url.start_with?('wss:')
22
22
  end
@@ -47,9 +47,7 @@ module LucidAuthentication
47
47
  `window.location = cookie_query` # doing page load and redirect
48
48
  nil
49
49
  else
50
- error = agent.response[:error]
51
- `console.err(error)` if error
52
- Isomorfeus.raise_error(message: 'Login failed!') # triggers .fail
50
+ Isomorfeus.raise_error(message: "Login failed with '#{agent.response[:error]}'!") # triggers .fail
53
51
  end
54
52
  end
55
53
  end
@@ -1,11 +1,9 @@
1
1
  module LucidChannel
2
2
  class Base
3
- if RUBY_ENGINE != 'opal'
4
- def self.inherited(base)
5
- Isomorfeus.add_valid_channel_class(base)
6
- end
3
+ def self.inherited(base)
4
+ Isomorfeus.add_valid_channel_class(base)
7
5
  end
8
6
 
9
7
  include LucidChannel::Mixin
10
8
  end
11
- end
9
+ end
@@ -1,48 +1,103 @@
1
1
  module LucidChannel
2
2
  module Mixin
3
3
  def self.included(base)
4
-
5
- if RUBY_ENGINE != 'opal'
6
- Isomorfeus.add_valid_channel_class(base) unless base == LucidChannel::Base
7
- end
4
+ Isomorfeus.add_valid_channel_class(base) unless base == LucidChannel::Base
8
5
 
9
6
  base.instance_exec do
10
- def process_message(channel, message = nil)
11
- if @message_processor
12
- if channel == self.name
13
- @message_processor.call(message)
14
- else
15
- @message_processor.call(channel, message)
16
- end
17
- else
18
- puts "#{self} received: #{channel} #{message}, but no 'on_message' block defined!"
7
+ def subscription_channels
8
+ @subscription_channels ||= {}
9
+ end
10
+
11
+ def channel(name, options = {})
12
+ subscription_channels[name.to_s] = options
13
+ end
14
+
15
+ def valid_channel?(name)
16
+ name = name.to_s
17
+ subscription_channels.key?(name) || name == self.name
18
+ end
19
+
20
+ def process_message(message, error, channel = nil)
21
+ channel = self.name unless channel
22
+ channel = channel.to_s
23
+ unless valid_channel?(channel)
24
+ Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot process message")
19
25
  end
26
+ block = subscription_channels[channel][:block]
27
+ Isomorfeus.raise_error(message: "#{self} received: #{channel} #{message}, but no 'on_message' block defined!") unless block
28
+ block.call(message, error)
29
+ nil
20
30
  end
21
31
 
22
- def on_message(&block)
23
- @message_processor = block
32
+ def on_message(channel = nil, &block)
33
+ channel = self.name unless channel
34
+ channel = channel.to_s
35
+ unless valid_channel?(channel)
36
+ Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
37
+ end
38
+ subscription_channels[channel] = {} unless subscription_channels.key?(channel)
39
+ subscription_channels[channel][:block] = block
24
40
  end
25
41
 
26
- def send_message(channel, message = nil)
27
- unless message
28
- message = channel
29
- channel = self.name
42
+ def send_message(message, channel = nil)
43
+ channel = self.name unless channel
44
+ unless valid_channel?(channel)
45
+ Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot send message")
30
46
  end
31
- Isomorfeus::Transport.send_notification(self, channel, message)
47
+ Isomorfeus::Transport.send_message(self, channel, message)
32
48
  end
33
49
 
34
50
  def subscribe(channel = nil)
51
+ promise_subscribe(channel)
52
+ nil
53
+ end
54
+
55
+ def promise_subscribe(channel = nil)
35
56
  channel = channel ? channel : self.name
36
- Isomorfeus::Transport.subscribe(self, channel)
57
+ Isomorfeus::Transport.promise_subscribe(self.name, channel)
37
58
  end
38
59
 
39
60
  def unsubscribe(channel = nil)
61
+ promise_unsubscribe(channel)
62
+ nil
63
+ end
64
+
65
+ def promise_unsubscribe(channel = nil)
40
66
  channel = channel ? channel : self.name
41
- Isomorfeus::Transport.unsubscribe(self, channel)
67
+ Isomorfeus::Transport.promise_unsubscribe(self.name, channel)
42
68
  end
43
69
 
44
- def current_user
45
- Isomorfeus.current_user
70
+ if RUBY_ENGINE == 'opal'
71
+ def server_subscription_channels; end
72
+ def server_process_message(message, channel = nil); end
73
+ def server_on_message(channel = nil, &block); end
74
+ else
75
+ def server_is_processing_messages?(channel)
76
+ return false if server_subscription_channels.empty?
77
+ return true if server_subscription_channels.key?(channel) && server_subscription_channels[channel].key?(:block)
78
+ false
79
+ end
80
+
81
+ def server_subscription_channels
82
+ @server_subscription_channels ||= {}
83
+ end
84
+
85
+ def server_process_message(message, channel = nil)
86
+ channel = self.name unless channel
87
+ channel = channel.to_s
88
+ block = server_subscription_channels[channel][:block]
89
+ block.call(message)
90
+ end
91
+
92
+ def server_on_message(channel = nil, &block)
93
+ channel = self.name unless channel
94
+ channel = channel.to_s
95
+ unless valid_channel?(channel)
96
+ Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
97
+ end
98
+ server_subscription_channels[channel] = {} unless server_subscription_channels.key?(channel)
99
+ server_subscription_channels[channel][:block] = block
100
+ end
46
101
  end
47
102
  end
48
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isomorfeus-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.zeta23
4
+ version: 1.0.0.zeta24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-22 00:00:00.000000000 Z
11
+ date: 2020-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -100,42 +100,42 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 4.0.21
103
+ version: 4.0.22
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: 4.0.21
110
+ version: 4.0.22
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: isomorfeus-react
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 16.12.23
117
+ version: 16.13.0
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: 16.12.23
124
+ version: 16.13.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: isomorfeus-policy
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - '='
130
130
  - !ruby/object:Gem::Version
131
- version: 1.0.0.zeta23
131
+ version: 1.0.0.zeta24
132
132
  type: :runtime
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - '='
137
137
  - !ruby/object:Gem::Version
138
- version: 1.0.0.zeta23
138
+ version: 1.0.0.zeta24
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: websocket-driver
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - '='
158
158
  - !ruby/object:Gem::Version
159
- version: 1.0.0.zeta23
159
+ version: 1.0.0.zeta24
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - '='
165
165
  - !ruby/object:Gem::Version
166
- version: 1.0.0.zeta23
166
+ version: 1.0.0.zeta24
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: opal-webpack-loader
169
169
  requirement: !ruby/object:Gem::Requirement