anycable-core 1.4.2 → 1.4.3

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: 391832d2af2280297419d7f62853313d8ac005e7ca826654d6669135748cb8e8
4
- data.tar.gz: 229e4b51b7eaed689f0394445e86ba96cb08889391ae343b810439a727e30c0f
3
+ metadata.gz: 4a7b2ae33dc506f328c7ce62c3c4c80eabb17ed36ca4826808caeb0534a8b2ab
4
+ data.tar.gz: 2cfae2f0b55718269c33dfc5877d55a2d210d9fa99f0aca85c2899720c0cf350
5
5
  SHA512:
6
- metadata.gz: c951c3a248b67b9d481865d4a7c1e3d75f76ca963a8f314c8a4889fe9ab1e28eb889ecd48094c96802a20c9196f0932051167c1bd32b54ebf7d7d95dce71620b
7
- data.tar.gz: 630c505792b0f374dc6acf7ffa861fcde660687ed7ac8894a0275e9f8d707133dbb9c29c24ed7b0e1dd8650b428a421243d5d1179361421996c850bdc50d0128
6
+ metadata.gz: b10e485726fd3f48791883af759cfdbeb963a59798a3df7a5100e993891fdb8faff370d028f6b81010be79b2737405196e7f4988041c92896326b222eab4296a
7
+ data.tar.gz: a901c3958ed1d866a6d7d7f20fb7e8ede84bfabbba29136a022e8eaf3241d10aa437d02c163fa54d0fe7aad8fef701df2299968a20fc887b9348276bee11098a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 1.4.3 (2023-10-15)
6
+
7
+ - Add broadcast options support. ([@palkan][])
8
+
9
+ - Add batch-broadcasting support. ([@palkan][])
10
+
5
11
  ## 1.4.2 (2023-09-05)
6
12
 
7
13
  - Fix parsing sentinel configuration from YAML in Rails. ([@palkan][])
@@ -7,8 +7,31 @@ module AnyCable
7
7
  raise NotImplementedError
8
8
  end
9
9
 
10
- def broadcast(stream, payload)
11
- raw_broadcast({stream: stream, data: payload}.to_json)
10
+ def batching(enabled = true)
11
+ self.batching_enabled = enabled
12
+ yield
13
+ ensure
14
+ maybe_flush_batch
15
+ end
16
+
17
+ def start_batching
18
+ self.batching_enabled = true
19
+ end
20
+
21
+ def finish_batching
22
+ maybe_flush_batch
23
+ end
24
+
25
+ def batching?
26
+ Thread.current[:anycable_batching]&.last
27
+ end
28
+
29
+ def broadcast(stream, payload, options = nil)
30
+ if batching?
31
+ current_batch << {stream: stream, data: payload, meta: options}.compact
32
+ else
33
+ raw_broadcast({stream: stream, data: payload, meta: options}.compact.to_json)
34
+ end
12
35
  end
13
36
 
14
37
  def broadcast_command(command, **payload)
@@ -21,6 +44,32 @@ module AnyCable
21
44
 
22
45
  private
23
46
 
47
+ def batching_enabled=(val)
48
+ # The stack must start with the true value,
49
+ # so we can check for emptiness to decide whether to flush
50
+ stack = batching_enabled_stack
51
+ stack << val if val || !stack.empty?
52
+ end
53
+
54
+ def batching_enabled_stack
55
+ Thread.current[:anycable_batching] ||= []
56
+ end
57
+
58
+ def current_batch
59
+ Thread.current[:anycable_batch] ||= []
60
+ end
61
+
62
+ def maybe_flush_batch
63
+ batching_enabled_stack.pop
64
+ return unless batching_enabled_stack.empty?
65
+
66
+ batch = current_batch
67
+ unless batch.empty?
68
+ raw_broadcast(batch.to_json)
69
+ end
70
+ current_batch.clear
71
+ end
72
+
24
73
  def logger
25
74
  AnyCable.logger
26
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AnyCable
4
- VERSION = "1.4.2"
4
+ VERSION = "1.4.3"
5
5
  end
data/lib/anycable.rb CHANGED
@@ -92,8 +92,8 @@ module AnyCable
92
92
 
93
93
  # Raw broadcast message to the channel, sends only string!
94
94
  # To send hash or object use ActionCable.server.broadcast instead!
95
- def broadcast(channel, payload)
96
- broadcast_adapter.broadcast(channel, payload)
95
+ def broadcast(...)
96
+ broadcast_adapter.broadcast(...)
97
97
  end
98
98
 
99
99
  def rpc_handler
@@ -1,7 +1,13 @@
1
1
  module AnyCable
2
+ type broadcastOptions = {exclude_socket: String}
3
+
2
4
  interface _BroadcastAdapter
3
5
  def raw_broadcast: (String _data) -> void
4
- def broadcast: (String stream, String payload) -> void
6
+ def batching: (?bool enabled) { () -> untyped } -> untyped
7
+ def start_batching: () -> void
8
+ def finish_batching: () -> void
9
+ def batching?: () -> (bool | NilClass)
10
+ def broadcast: (String stream, String payload, ?broadcastOptions options) -> void
5
11
  def broadcast_command: (String command, **untyped payload) -> void
6
12
  def announce!: () -> void
7
13
  end
@@ -13,6 +19,10 @@ module AnyCable
13
19
  private
14
20
 
15
21
  def logger: () -> Logger
22
+ def current_batch: () -> Array[untyped]
23
+ def maybe_flush_batch: () -> void
24
+ def batching_enabled_stack: () -> Array[bool]
25
+ def batching_enabled=: (bool) -> void
16
26
  end
17
27
  end
18
28
  end
data/sig/anycable.rbs CHANGED
@@ -36,6 +36,6 @@ module AnyCable
36
36
  def self.server_callbacks: () -> Array[^() -> void]
37
37
  def self.broadcast_adapter: () -> _BroadcastAdapter
38
38
  def self.broadcast_adapter=: (untyped adapter) -> void
39
- def self.broadcast: (String channel, String payload) -> void
39
+ def self.broadcast: (String channel, String payload, ?broadcastOptions options) -> void
40
40
  def self.rpc_handler: () -> RPC::Handler
41
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anycable-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-06 00:00:00.000000000 Z
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config