datastar 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/datastar/dispatcher.rb +24 -16
- data/lib/datastar/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 568e2026e9657b4042957c360641bc3c1b51bfdfe7dcf1710a78b75e07a71ccf
|
|
4
|
+
data.tar.gz: 25851c3fc4b6bb74c8db43d6ecfbbd8fa796be259fe9b0b4bc4653e3077c0e41
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 880721f4912523484e4c79c8faeba5241e371007d5a6d81cbf45f1979ef4d25a6a1c32c25afe24165c4e646bf1523355ae9bea9a13310da2a05287fab71acb82
|
|
7
|
+
data.tar.gz: 9b594d6249a5a923d8da9d693e495d8f1622835bd8b698c5e8ff8396bbf00f5ac6f5c6302591ac9ae4690f06c7b6e9dc1c9e752d9d9f3757e74648349472ba3d
|
data/README.md
CHANGED
|
@@ -245,6 +245,21 @@ You can also set it to a different number (in seconds)
|
|
|
245
245
|
heartbeat: 0.5
|
|
246
246
|
```
|
|
247
247
|
|
|
248
|
+
#### Per-stream override
|
|
249
|
+
|
|
250
|
+
The `#stream` method also accepts a `heartbeat:` keyword that overrides the constructor-level setting for a single call. This is useful when a dispatcher is generally configured with a heartbeat but a particular response doesn't need one (e.g. a one-shot update). The previous value is restored once the call returns.
|
|
251
|
+
|
|
252
|
+
```ruby
|
|
253
|
+
datastar = Datastar.new(request:, response:) # default heartbeat
|
|
254
|
+
|
|
255
|
+
# Disable heartbeat for this single response
|
|
256
|
+
datastar.stream(heartbeat: false) do |sse|
|
|
257
|
+
sse.patch_elements(html)
|
|
258
|
+
end
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
The one-shot helpers (`#patch_elements`, `#remove_elements`, `#patch_signals`, `#remove_signals`, `#execute_script`, `#redirect`) use this internally to avoid spawning a heartbeat thread for a single message.
|
|
262
|
+
|
|
248
263
|
#### Manual connection check
|
|
249
264
|
|
|
250
265
|
If you want to check connection status on your own, you can disable the heartbeat and use `sse.check_connection!`, which will close the connection and trigger callbacks if the client is disconnected.
|
data/lib/datastar/dispatcher.rb
CHANGED
|
@@ -28,7 +28,7 @@ module Datastar
|
|
|
28
28
|
HTTP_ACCEPT = 'HTTP_ACCEPT'
|
|
29
29
|
HTTP1 = 'HTTP/1.1'
|
|
30
30
|
|
|
31
|
-
attr_reader :request, :response
|
|
31
|
+
attr_reader :request, :response, :heartbeat
|
|
32
32
|
|
|
33
33
|
# @option request [Rack::Request] the request object
|
|
34
34
|
# @option response [Rack::Response, nil] the response object
|
|
@@ -137,7 +137,7 @@ module Datastar
|
|
|
137
137
|
# @param elements [String, #call(view_context: Object) => Object] the HTML elements or object
|
|
138
138
|
# @param options [Hash] the options to send with the message
|
|
139
139
|
def patch_elements(elements, options = BLANK_OPTIONS)
|
|
140
|
-
|
|
140
|
+
stream(heartbeat: false) do |sse|
|
|
141
141
|
sse.patch_elements(elements, options)
|
|
142
142
|
end
|
|
143
143
|
end
|
|
@@ -152,7 +152,7 @@ module Datastar
|
|
|
152
152
|
# @param selector [String] a CSS selector for the fragment to remove
|
|
153
153
|
# @param options [Hash] the options to send with the message
|
|
154
154
|
def remove_elements(selector, options = BLANK_OPTIONS)
|
|
155
|
-
|
|
155
|
+
stream(heartbeat: false) do |sse|
|
|
156
156
|
sse.remove_elements(selector, options)
|
|
157
157
|
end
|
|
158
158
|
end
|
|
@@ -166,7 +166,7 @@ module Datastar
|
|
|
166
166
|
# @param signals [Hash, String] signals to merge
|
|
167
167
|
# @param options [Hash] the options to send with the message
|
|
168
168
|
def patch_signals(signals, options = BLANK_OPTIONS)
|
|
169
|
-
|
|
169
|
+
stream(heartbeat: false) do |sse|
|
|
170
170
|
sse.patch_signals(signals, options)
|
|
171
171
|
end
|
|
172
172
|
end
|
|
@@ -180,7 +180,7 @@ module Datastar
|
|
|
180
180
|
# @param paths [Array<String>] object paths to the signals to remove
|
|
181
181
|
# @param options [Hash] the options to send with the message
|
|
182
182
|
def remove_signals(paths, options = BLANK_OPTIONS)
|
|
183
|
-
|
|
183
|
+
stream(heartbeat: false) do |sse|
|
|
184
184
|
sse.remove_signals(paths, options)
|
|
185
185
|
end
|
|
186
186
|
end
|
|
@@ -194,7 +194,7 @@ module Datastar
|
|
|
194
194
|
# @param script [String] the script to execute
|
|
195
195
|
# @param options [Hash] the options to send with the message
|
|
196
196
|
def execute_script(script, options = BLANK_OPTIONS)
|
|
197
|
-
|
|
197
|
+
stream(heartbeat: false) do |sse|
|
|
198
198
|
sse.execute_script(script, options)
|
|
199
199
|
end
|
|
200
200
|
end
|
|
@@ -204,7 +204,7 @@ module Datastar
|
|
|
204
204
|
#
|
|
205
205
|
# @param url [String] the URL or path to redirect to
|
|
206
206
|
def redirect(url)
|
|
207
|
-
|
|
207
|
+
stream(heartbeat: false) do |sse|
|
|
208
208
|
sse.redirect(url)
|
|
209
209
|
end
|
|
210
210
|
end
|
|
@@ -245,10 +245,24 @@ module Datastar
|
|
|
245
245
|
# By default, the built-in Rack finalzer just returns the resposne Array which can be used by any Rack handler.
|
|
246
246
|
# On Rails, the Rails controller response is set to this objects streaming response.
|
|
247
247
|
#
|
|
248
|
+
# A per-call +heartbeat:+ keyword overrides the constructor-level heartbeat
|
|
249
|
+
# for the duration of this call. Pass +false+ to disable heartbeat for a
|
|
250
|
+
# one-shot message (e.g. a single +patch_elements+), or a Numeric interval
|
|
251
|
+
# to enable it. The previous value is restored once the call returns.
|
|
252
|
+
# @example Disable heartbeat for a single response
|
|
253
|
+
#
|
|
254
|
+
# datastar.stream(heartbeat: false) do |sse|
|
|
255
|
+
# sse.patch_elements(html)
|
|
256
|
+
# end
|
|
257
|
+
#
|
|
248
258
|
# @param streamer [#call(ServerSentEventGenerator), nil] a callable to call with the generator
|
|
259
|
+
# @param heartbeat [Numeric, false] override the heartbeat interval for this call, or +false+ to disable
|
|
249
260
|
# @yieldparam sse [ServerSentEventGenerator] the generator object
|
|
250
261
|
# @return [Object] depends on the finalize callback
|
|
251
|
-
def stream(streamer = nil, &block)
|
|
262
|
+
def stream(streamer = nil, heartbeat: @heartbeat, &block)
|
|
263
|
+
heartbeat_was = @heartbeat
|
|
264
|
+
@heartbeat = heartbeat
|
|
265
|
+
|
|
252
266
|
streamer ||= block
|
|
253
267
|
@streamers << streamer
|
|
254
268
|
if @heartbeat && !@heartbeat_on
|
|
@@ -269,18 +283,12 @@ module Datastar
|
|
|
269
283
|
|
|
270
284
|
@response.body = body
|
|
271
285
|
@finalize.call(@view_context, @response)
|
|
286
|
+
ensure
|
|
287
|
+
@heartbeat = heartbeat_was
|
|
272
288
|
end
|
|
273
289
|
|
|
274
290
|
private
|
|
275
291
|
|
|
276
|
-
def stream_no_heartbeat(&block)
|
|
277
|
-
was = @heartbeat
|
|
278
|
-
@heartbeat = false
|
|
279
|
-
stream(&block).tap do
|
|
280
|
-
@heartbeat = was
|
|
281
|
-
end
|
|
282
|
-
end
|
|
283
|
-
|
|
284
292
|
# Produce a response body for a single stream
|
|
285
293
|
# In this case, the SSE generator can write directly to the socket
|
|
286
294
|
#
|
data/lib/datastar/version.rb
CHANGED