litecable 0.7.2 → 0.8.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
2
  SHA256:
3
- metadata.gz: 0f2e128a054097e5ccd19b490792950d8f8cb53f36d3c6d5c56113271e6f9bb9
4
- data.tar.gz: bb9439ac1dad7d8be53173b942362eba9a3869674cd598a37a3bda9071efdeff
3
+ metadata.gz: ce37cbd6931242e4cc18f8679e3131e3b7e1dab3b589576408b7a8b3d70737ae
4
+ data.tar.gz: e12346809a6b05538edb6d417ba99fa72ae465bfb79775a0f7e046cc8070e2fb
5
5
  SHA512:
6
- metadata.gz: 430ea53e7ebbc00b1f2df127b642235200fb55e9710813d88c4182d337d38d8202b2fac0aadae6d6119e35a612a25c77e10146c1039bba130595e5e28e4579cb
7
- data.tar.gz: c31bc0b7b6fa0f33754915966bb8e16199dac3ceab604270e9df24c52b2b1b91267e5c32a1cbd5e91dfbb7b5a3f2f074649a8ec7f0d4ac13db68da5ebbe24410
6
+ metadata.gz: 9f3a5bd03cb018458eb5c9c447e3f2ae240cf92d91c071c6aec89c067753bd63b023f23a7c1dea6261b2457236466e1b8dcd07f27413d7dd1aeeeb70bb5579ef
7
+ data.tar.gz: 1972ce3fd5adc6305a22fbfcd174ee5ec5f74fba22e553e714159b5b9c822fd3d1bcbbc774a9630292e9002bfb61e4d80c0c0916a1001d3462a84433002b8651
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.8.0 (2023-03-08)
6
+
7
+ - Allow using custom channel lookup logic. ([@palkan][])
8
+
9
+ - Ruby 2.7+ is required.
10
+
5
11
  ## 0.7.2 (2021-07-06)
6
12
 
7
13
  - Fixed Ruby 3.0.1 compatibility.
data/README.md CHANGED
@@ -55,6 +55,21 @@ end
55
55
  App.cable.subscriptions.create('chat', ...)
56
56
  ```
57
57
 
58
+ ### Using a custom channel registry
59
+
60
+ Alternatively to eager loading all channel classes and providing identifiers, you can build a custom _channel registry_ object, which can perform channel class lookups:
61
+
62
+ ```ruby
63
+ # DummyRegistry which always returns a predefined channel class
64
+ class DummyRegistry
65
+ def lookup(channel_id)
66
+ DummyChannel
67
+ end
68
+ end
69
+
70
+ LiteCable.channel_registry = DummyRegistry.new
71
+ ```
72
+
58
73
  ### Using built-in server (middleware)
59
74
 
60
75
  Lite Cable comes with a simple Rack middleware for development/testing usage.
@@ -78,9 +93,11 @@ end
78
93
 
79
94
  ### Using with AnyCable
80
95
 
81
- Lite Cable is AnyCable-compatible out-of-the-box:
96
+ Lite Cable is AnyCable-compatible out-of-the-box.
97
+
98
+ If AnyCable gem is loaded, you don't need to configure Lite Cable at all.
82
99
 
83
- - Set broadcast adapter to AnyCable:
100
+ Otherwise, you must configure broadcast adapter manually:
84
101
 
85
102
  ```ruby
86
103
  LiteCable.broadcast_adapter = :any_cable
@@ -88,7 +105,7 @@ LiteCable.broadcast_adapter = :any_cable
88
105
 
89
106
  You can also do this via configuration, e.g., env var (`LITECABLE_BROADCAST_ADAPTER=any_cable`) or `broadcast_adapter: any_cable` in a YAML config.
90
107
 
91
- - Configure connection factory:
108
+ **At the AnyCable side**, you must configure a connection factory:
92
109
 
93
110
  ```ruby
94
111
  AnyCable.connection_factory = MyApp::Connection
@@ -23,7 +23,6 @@ module LiteCable # :nodoc:
23
23
  @request ||= Rack::Request.new(socket.env)
24
24
  end
25
25
 
26
- # rubocop: disable Metrics/MethodLength
27
26
  def handle_channel_command(identifier, command, data)
28
27
  channel = subscriptions.add(identifier, false)
29
28
  case command
@@ -45,7 +44,6 @@ module LiteCable # :nodoc:
45
44
  close
46
45
  false
47
46
  end
48
- # rubocop: enable Metrics/MethodLength
49
47
  end
50
48
  end
51
49
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # frozen_string_literal: true
4
-
5
3
  module LiteCable
6
4
  module BroadcastAdapters
7
5
  class Base
@@ -6,7 +6,6 @@ module LiteCable
6
6
  module BroadcastAdapters # :nodoc:
7
7
  module_function
8
8
 
9
- # rubocop: disable Metrics/AbcSize, Metrics/MethodLength
10
9
  def lookup_adapter(args)
11
10
  adapter, options = Array(args)
12
11
  path_to_adapter = "lite_cable/broadcast_adapters/#{adapter}"
@@ -30,6 +29,5 @@ module LiteCable
30
29
 
31
30
  BroadcastAdapters.const_get(adapter_class_name, false).new(**(options || {}))
32
31
  end
33
- # rubocop: enable Metrics/AbcSize, Metrics/MethodLength
34
32
  end
35
33
  end
@@ -21,6 +21,8 @@ module LiteCable
21
21
  channels[id]
22
22
  end
23
23
 
24
+ alias_method :lookup, :find
25
+
24
26
  def find!(id)
25
27
  channel_class = find(id)
26
28
  raise UnknownChannelError unless channel_class
@@ -13,7 +13,7 @@ module LiteCable
13
13
 
14
14
  attr_config :logger,
15
15
  coder: Coders::JSON,
16
- broadcast_adapter: :memory,
16
+ broadcast_adapter: defined?(::AnyCable::VERSION) ? :any_cable : :memory,
17
17
  identifier_coder: Coders::Raw,
18
18
  log_level: Logger::INFO
19
19
  end
@@ -1,6 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LiteCable
4
+ class UnknownChannelError < StandardError
5
+ attr_reader :channel_id
6
+
7
+ def initialize(channel_id)
8
+ @channel_id = channel_id
9
+ super("Unknown channel: #{channel_id}")
10
+ end
11
+ end
12
+
4
13
  module Connection
5
14
  # Manage the connection channels and route messages
6
15
  class Subscriptions
@@ -30,7 +39,9 @@ module LiteCable
30
39
 
31
40
  channel_id = params.delete("channel")
32
41
 
33
- channel_class = Channel::Registry.find!(channel_id)
42
+ channel_class = LiteCable.channel_registry.lookup(channel_id)
43
+
44
+ raise UnknownChannelError, channel_id unless channel_class
34
45
 
35
46
  subscriptions[identifier] = channel_class.new(connection, identifier, params)
36
47
  subscribe ? subscribe_channel(subscriptions[identifier]) : subscriptions[identifier]
@@ -64,7 +64,6 @@ module LiteCable
64
64
  @error_handlers << block
65
65
  end
66
66
 
67
- # rubocop: disable Metrics/MethodLength
68
67
  def listen
69
68
  keepalive
70
69
  Thread.new do
@@ -74,7 +73,7 @@ module LiteCable
74
73
  each_frame do |data|
75
74
  @message_handlers.each do |h|
76
75
  h.call(data)
77
- rescue => e # rubocop: disable Style/RescueStandardError
76
+ rescue => e
78
77
  log(:error, "Socket receive failed: #{e}")
79
78
  @error_handlers.each { |eh| eh.call(e, data) }
80
79
  close if close_on_error
@@ -85,7 +84,6 @@ module LiteCable
85
84
  end
86
85
  end
87
86
  end
88
- # rubocop: enable Metrics/MethodLength
89
87
 
90
88
  def close
91
89
  return unless @active
@@ -116,7 +114,7 @@ module LiteCable
116
114
  frame = WebSocket::Frame::Outgoing::Server.new(version: version, type: :close, code: 1000)
117
115
  @socket.write(frame.to_s) if frame.supported?
118
116
  @socket.close
119
- rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT # rubocop:disable Lint/HandleExceptions
117
+ rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT
120
118
  # already closed
121
119
  end
122
120
 
@@ -137,7 +135,7 @@ module LiteCable
137
135
  def each_frame
138
136
  framebuffer = WebSocket::Frame::Incoming::Server.new(version: version)
139
137
 
140
- while IO.select([socket])
138
+ while socket.wait_readable
141
139
  data = socket.respond_to?(:recv) ? socket.recv(2000) : socket.readpartial(2000)
142
140
  break if data.empty?
143
141
 
@@ -23,7 +23,6 @@ module LiteCable
23
23
  @stopped = true
24
24
  end
25
25
 
26
- # rubocop: disable Metrics/MethodLength
27
26
  def run
28
27
  Thread.new do
29
28
  Thread.current.abort_on_exception = true
@@ -41,7 +40,6 @@ module LiteCable
41
40
  end
42
41
  end
43
42
  end
44
- # rubocop: enable Metrics/MethodLength
45
43
 
46
44
  private
47
45
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LiteCable
4
- VERSION = "0.7.2"
4
+ VERSION = "0.8.0"
5
5
  end
data/lib/lite_cable.rb CHANGED
@@ -23,6 +23,8 @@ module LiteCable
23
23
  @config ||= Config.new
24
24
  end
25
25
 
26
+ attr_accessor :channel_registry
27
+
26
28
  # Broadcast encoded message to the stream
27
29
  def broadcast(stream, message, coder: LiteCable.config.coder)
28
30
  broadcast_adapter.broadcast(stream, message, coder: coder)
@@ -47,4 +49,6 @@ module LiteCable
47
49
  @broadcast_adapter = adapter
48
50
  end
49
51
  end
52
+
53
+ self.channel_registry = Channel::Registry
50
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-06 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '3.6'
89
+ version: '6.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '3.6'
96
+ version: '6.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: bundler
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -195,14 +195,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
195
195
  requirements:
196
196
  - - ">="
197
197
  - !ruby/object:Gem::Version
198
- version: 2.5.0
198
+ version: 2.7.0
199
199
  required_rubygems_version: !ruby/object:Gem::Requirement
200
200
  requirements:
201
201
  - - ">="
202
202
  - !ruby/object:Gem::Version
203
203
  version: '0'
204
204
  requirements: []
205
- rubygems_version: 3.2.15
205
+ rubygems_version: 3.4.6
206
206
  signing_key:
207
207
  specification_version: 4
208
208
  summary: Fat-free ActionCable implementation