phlex-reactive 0.17.0 → 0.18.0

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.
@@ -25,9 +25,16 @@ module Phlex
25
25
  # pgbus only: the src is a pgbus signed stream name, the one
26
26
  # pgbus_stream_from (and turbo_stream_from under pgbus) renders. Without
27
27
  # pgbus (Action Cable) a subscribing root raises ConfigurationError at
28
- # render, naming turbo_stream_from as the alternative. There is no replay:
29
- # a broadcast sent before the source connects is missed (pgbus streams are
30
- # ephemeral by default), as with any live subscription.
28
+ # render, naming turbo_stream_from as the alternative.
29
+ #
30
+ # Catching up (issue #478). A durable stream's root also carries the
31
+ # stream's watermark at render (data-reactive-subscribe-since, read the
32
+ # way pgbus_stream_from reads it), and its source replays from there: a
33
+ # broadcast sent between the render and the connection still arrives. An
34
+ # ephemeral stream (pgbus's default) has no archive to replay, so it gets
35
+ # no watermark; the client dispatches reactive:subscribed on the root each
36
+ # time its source connects, and a component that must not miss a change
37
+ # refetches on it.
31
38
  module Subscribe
32
39
  # What reactive_subscribe declared: the stream name's last part (nil
33
40
  # with key:), and key: — the stream's parts as an Array, or a callable
@@ -70,6 +77,20 @@ module Phlex
70
77
 
71
78
  # The signed SSE URL a <pgbus-stream-source> subscribes with.
72
79
  def source_src(stream_name) = Phlex::Reactive::Defer.stream_src(stream_name)
80
+
81
+ # The since-id the source replays from: a durable stream's watermark
82
+ # (MAX msg_id, 0 for a stream nothing was published to yet), read live
83
+ # like pgbus_stream_from reads it. nil for an ephemeral stream (no
84
+ # archive to replay) or a pgbus that cannot tell. One query per render
85
+ # of a durable subscribing root, never cached: pgbus's own cache is
86
+ # cleared only between Rack requests, and a broadcast renders in a job.
87
+ def since_id(stream_name)
88
+ config = ::Pgbus.configuration if ::Pgbus.respond_to?(:configuration)
89
+ return unless config.respond_to?(:stream_durable?) && config.stream_durable?(stream_name)
90
+
91
+ stream = ::Pgbus.stream(stream_name)
92
+ stream.current_msg_id if stream.respond_to?(:current_msg_id)
93
+ end
73
94
  end
74
95
 
75
96
  # What reactive_subscribe adds to a component's instances. Included only
@@ -95,11 +116,15 @@ module Phlex
95
116
  end
96
117
 
97
118
  # The root's subscription marker (reactive_attrs): the signed src the
98
- # client mints its <pgbus-stream-source> from.
119
+ # client mints its <pgbus-stream-source> from, and for a durable stream
120
+ # the watermark it replays from (issue #478).
99
121
  def reactive_subscribe_data
100
122
  Subscribe.require_capable!(self.class)
101
123
  name = Subscribe.stream_name(self.class, reactive_subscribe_parts)
102
- { reactive_subscribe: Subscribe.source_src(name) }
124
+ data = { reactive_subscribe: Subscribe.source_src(name) }
125
+ since = Subscribe.since_id(name)
126
+ data[:reactive_subscribe_since] = since unless since.nil?
127
+ data
103
128
  end
104
129
 
105
130
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Reactive
5
- VERSION = "0.17.0"
5
+ VERSION = "0.18.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-reactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson