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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1a9bb3eed84795594fe3d6d7b2db33559c78ca4c7e9e34cbcaebd790b6a6900
4
- data.tar.gz: df9a53575bf91183094012c731212143b8f3fa50400b825846f0a3287e95723f
3
+ metadata.gz: 91678969419d0d4fcb917d69f7d1c9e7478877317e8c98e84dfec78fcbf114ca
4
+ data.tar.gz: 79b1ab20debf6de7a3c7c80752b15a6bfab89848276fd75ddbadafa0161b7886
5
5
  SHA512:
6
- metadata.gz: 284407936c307e1b3dbd3740db7d8c027fed86fd69fd2b46d32e345032fefde62c472d5996f8fc0f35f9e646eebcfb0a2ffe3737ac1ea9699f7b58efd3f73830
7
- data.tar.gz: e1151e8ee3053d796fb65363637341397fba6564fa630f35a7dc276dd35eb42afaf8ba30139f3a450706270e0cfe087c3ffaba814915b8d2a952e400f6b13ae2
6
+ metadata.gz: 4050cef0a111c227ffb5c722e004fed6604e0c63f30cae2ad9f599366aeec956f0bd6776095f963bb9457fd266e782832c0b885e2006e0f9b143de8005c1b6f0
7
+ data.tar.gz: cab2bcfb72b76b907e614b248a342bdad48c5c70ca2f447d10767a262676a02c5173f0dec899328d8c1f84ab9db85914d8651f529f1cb198abb3169b472d2726
data/CHANGELOG.md CHANGED
@@ -6,6 +6,34 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.18.0] - 2026-11-16
10
+
11
+ ### Added
12
+
13
+ - **`reactive_subscribe` catches up on a broadcast sent before its source
14
+ connected (#478, pgbus).** On a durable stream the root carries the stream's
15
+ watermark at render (`data-reactive-subscribe-since`, read as
16
+ `pgbus_stream_from` reads it: one `MAX(msg_id)` query per render of a durable
17
+ subscribing root), and the minted `<pgbus-stream-source>` opens with it as
18
+ `since-id`. A broadcast sent between the render and the connection now
19
+ arrives. Roots sharing a source take the lowest watermark, and a source minted
20
+ again after a page render opens from its roots' watermarks then: it never
21
+ skips a frame, but it may replay one a root already shows, so a `js:` or
22
+ append frame can apply twice. A `data-turbo-permanent` subscribing root hits
23
+ this on every Drive visit (its source is minted again each time, from the
24
+ root's first render). Each time a
25
+ root's source connects, the root gets `reactive:subscribed`
26
+ (`detail: { connectionId, reconnect }`, not bubbling), so
27
+ `on(:refresh, event: "reactive:subscribed")` refetches what an ephemeral
28
+ stream cannot replay.
29
+
30
+ ### Fixed
31
+
32
+ - **A `reactive_subscribe` stream durable by configuration no longer replays
33
+ frames older than the render (#478).** The source opened with no `since`, which pgbus reads as
34
+ 0, so it replayed every retained frame: a `js:` broadcast sent before the
35
+ page rendered ran again on load.
36
+
9
37
  ## [0.17.0] - 2026-11-16
10
38
 
11
39
  ### Added
@@ -4630,7 +4658,8 @@ room for dogfood fixes.
4630
4658
  scaffolds a reactive component (and an RSpec spec when the app uses RSpec),
4631
4659
  state-backed by default or record-backed with `--record`.
4632
4660
 
4633
- [Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.17.0...HEAD
4661
+ [Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.18.0...HEAD
4662
+ [0.18.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.17.0...v0.18.0
4634
4663
  [0.17.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.14...v0.17.0
4635
4664
  [0.16.14]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.13...v0.16.14
4636
4665
  [0.16.13]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.12...v0.16.13
data/README.md CHANGED
@@ -2410,7 +2410,33 @@ Imports::Status.broadcast_self(import, js: Phlex::Reactive::JS.new.add_class("#b
2410
2410
  - **The actor.** An action on a subscribing root sends that root's own
2411
2411
  connection id, so `exclude: reactive_connection_id` skips the actor on that
2412
2412
  stream, but not on another page-level stream.
2413
- - **No replay**: a broadcast sent before the source connects is missed.
2413
+ - **Catching up (#478).** On a durable stream (pgbus's
2414
+ `streams_durable_patterns`) the root also carries the stream's watermark at
2415
+ render (`data-reactive-subscribe-since`, read as `pgbus_stream_from` reads it,
2416
+ one query per render), and the source replays from it: a broadcast sent
2417
+ between the render and the connection still arrives, and nothing older does.
2418
+ A source minted again after a page render (a Drive render, a morph that
2419
+ dropped it, every Drive visit of a `data-turbo-permanent` root) opens from
2420
+ its roots' watermarks then: it never skips a frame, but it may replay one a
2421
+ root already shows, so a `js:` or append frame can apply twice.
2422
+ An ephemeral stream (pgbus's default) has nothing to replay. Each time a
2423
+ root's source connects, the root gets `reactive:subscribed`
2424
+ (`detail: { connectionId, reconnect }`, not bubbling), so a component that
2425
+ must not miss a change refetches on it:
2426
+
2427
+ ```ruby
2428
+ action :refresh
2429
+ def refresh
2430
+ authorize! @import, :show? # verify_authorized is on by default
2431
+ reply.morph
2432
+ end
2433
+
2434
+ def view_template
2435
+ div(**mix(reactive_root, on(:refresh, event: "reactive:subscribed"))) { @import.state }
2436
+ end
2437
+ ```
2438
+
2439
+ A root that joins a source already connected gets no event.
2414
2440
  - **Refused at render**: a stream name over pgbus's stream-name budget (a
2415
2441
  UUID-keyed record always is: use `key: -> { [Pgbus.stream_key(@record), :status] }`),
2416
2442
  a nil record or key part, and any subscribing root on Action Cable, where you
@@ -5,6 +5,8 @@
5
5
  // The markup is the Ruby side's (reactive_subscribe):
6
6
  //
7
7
  // data-reactive-subscribe="<signed src>" the stream's signed SSE src
8
+ // data-reactive-subscribe-since="<id>" a durable stream's watermark at
9
+ // render (issue #478)
8
10
  //
9
11
  // While the root is on the page, the module keeps a <pgbus-stream-source> for
10
12
  // that src in a hidden box at the end of <body> (marked
@@ -20,12 +22,27 @@
20
22
  // the roots stay connected, the module mints a fresh source for every stream
21
23
  // a live root still needs.
22
24
  //
25
+ // Catching up (issue #478). A source opens with since-id set to the lowest
26
+ // watermark of the roots on its src, so pgbus replays a durable broadcast
27
+ // sent between the render and the connection. A source minted again (after a
28
+ // page render dropped it) opens from what its roots render now, their
29
+ // watermarks: it may replay a frame a root already shows, never skip one.
30
+ // Each time
31
+ // a source connects (its pgbus:connected), every root on it gets
32
+ // reactive:subscribed (detail { connectionId, reconnect }; reconnect is false
33
+ // on the stream's first connection), not bubbling, so a component can refetch
34
+ // what an ephemeral stream could not replay. A root that joins a source
35
+ // already connected gets no event: the source is live, and a root that comes
36
+ // back under the same id (a replace) must not refresh again and again.
37
+ //
23
38
  // This module never imports the core.
24
39
 
25
40
  const ATTR = "data-reactive-subscribe"
41
+ const SINCE = "data-reactive-subscribe-since"
26
42
  const MARK = "data-reactive-subscribe-source"
27
43
 
28
- // src -> { source, ids }: one source per stream, and the root ids on it.
44
+ // src -> { source, ids, connected }: one source per stream, the root ids on
45
+ // it, and whether it ever connected.
29
46
  const streams = new Map()
30
47
  // root id -> { id, root, src, removing, observer }: kept across a replace.
31
48
  // The id is the one the root had at connect: a root whose id changes while
@@ -89,8 +106,9 @@ function rejoin() {
89
106
 
90
107
  function join(src, id) {
91
108
  let stream = streams.get(src)
92
- if (!stream?.source.isConnected) streams.set(src, (stream = { source: mint(src), ids: stream?.ids ?? new Set() }))
109
+ if (!stream) streams.set(src, (stream = { source: null, ids: new Set(), connected: false }))
93
110
  stream.ids.add(id)
111
+ if (!stream.source?.isConnected) stream.source = mint(src, stream)
94
112
  }
95
113
 
96
114
  function leave(src, id) {
@@ -102,7 +120,7 @@ function leave(src, id) {
102
120
  stream.source.remove()
103
121
  }
104
122
 
105
- function mint(src) {
123
+ function mint(src, stream) {
106
124
  // pgbus's client is an async module script: mint the element anyway (it
107
125
  // upgrades when pgbus defines it), and call a still-missing client a
108
126
  // misconfiguration only after a grace period.
@@ -120,11 +138,38 @@ function mint(src) {
120
138
  source.setAttribute("src", src)
121
139
  source.setAttribute(MARK, "")
122
140
  source.hidden = true
141
+ // Before the append: pgbus reads since-id once, when the element connects.
142
+ const since = lowestWatermark(src)
143
+ if (since != null) source.setAttribute("since-id", String(since))
144
+ source.addEventListener("pgbus:connected", (event) => {
145
+ if (stream.source !== source) return
146
+ const reconnect = stream.connected
147
+ stream.connected = true
148
+ const connectionId = event.detail?.connectionId ?? null
149
+ for (const id of stream.ids) {
150
+ const entry = roots.get(id)
151
+ if (!entry || entry.removing || entry.src !== src || !entry.root.isConnected) continue
152
+ entry.root.dispatchEvent(new CustomEvent("reactive:subscribed", { detail: { connectionId, reconnect } }))
153
+ }
154
+ })
123
155
  minted.add(source)
124
156
  box().appendChild(source)
125
157
  return source
126
158
  }
127
159
 
160
+ // Where a durable stream's source replays from: the lowest watermark of the
161
+ // roots on its src (two roots rendered apart share the source). null for an
162
+ // ephemeral stream (no root carries a watermark).
163
+ function lowestWatermark(src) {
164
+ let since = null
165
+ for (const root of document.querySelectorAll(`[${SINCE}]`)) {
166
+ if (root.getAttribute(ATTR) !== src) continue
167
+ const value = Number(root.getAttribute(SINCE))
168
+ if (Number.isFinite(value) && value >= 0 && (since == null || value < since)) since = value
169
+ }
170
+ return since
171
+ }
172
+
128
173
  // The minted sources live in one hidden <reactive-stream-sources> at the end
129
174
  // of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps
130
175
  // it, sources and connections included. A tag no server page renders, so
@@ -1,4 +1,4 @@
1
- var o=new Map,s=new Map,a=new WeakMap,l=new WeakSet,c=!1;function p(e){if(!c)c=!0,document.addEventListener("turbo:morph",u),document.addEventListener("turbo:render",u);let r=e.element,t=s.get(r.id);if(!t)s.set(r.id,t={id:r.id,src:null});a.set(r,t),t.observer?.disconnect(),t.root=r,t.removing=!1;for(let i of document.querySelectorAll("pgbus-stream-source[data-reactive-subscribe-source]"))if(!l.has(i))i.remove();n(t,r.getAttribute("data-reactive-subscribe")),t.observer=new MutationObserver(()=>n(t,r.getAttribute("data-reactive-subscribe"))),t.observer.observe(r,{attributes:!0,attributeFilter:["data-reactive-subscribe"]})}function g(e){let r=e.element,t=a.get(r);if(!t||t.root!==r)return;t.observer.disconnect(),t.removing=!0,queueMicrotask(()=>{if(!t.removing)return;s.delete(t.id),n(t,null)})}function n(e,r){let t=e.src;if(e.src=r||null,e.src)m(e.src,e.id);if(t&&t!==e.src)b(t,e.id)}function u(){for(let e of s.values())if(e.src&&!e.removing&&e.root.isConnected)m(e.src,e.id)}function m(e,r){let t=o.get(e);if(!t?.source.isConnected)o.set(e,t={source:f(e),ids:t?.ids??new Set});t.ids.add(r)}function b(e,r){let t=o.get(e);if(!t)return;if(t.ids.delete(r),t.ids.size)return;o.delete(e),t.source.remove()}function f(e){if(!d())setTimeout(()=>{if(!d())console.error('[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: import "pgbus/stream_source_element"')},5000);let r=document.createElement("pgbus-stream-source");return r.setAttribute("src",e),r.setAttribute("data-reactive-subscribe-source",""),r.hidden=!0,l.add(r),v().appendChild(r),r}function v(){let e=[...document.body.children].find((r)=>r.localName==="reactive-stream-sources");if(!e)e=document.createElement("reactive-stream-sources"),e.hidden=!0,e.setAttribute("data-turbo-permanent",""),document.body.appendChild(e);return e}var d=()=>!!globalThis.customElements?.get?.("pgbus-stream-source");export{p as connect,g as disconnect};
1
+ var i=new Map,s=new Map,a=new WeakMap,b=new WeakSet,u=!1;function w(t){if(!u)u=!0,document.addEventListener("turbo:morph",d),document.addEventListener("turbo:render",d);let n=t.element,e=s.get(n.id);if(!e)s.set(n.id,e={id:n.id,src:null});a.set(n,e),e.observer?.disconnect(),e.root=n,e.removing=!1;for(let o of document.querySelectorAll("pgbus-stream-source[data-reactive-subscribe-source]"))if(!b.has(o))o.remove();c(e,n.getAttribute("data-reactive-subscribe")),e.observer=new MutationObserver(()=>c(e,n.getAttribute("data-reactive-subscribe"))),e.observer.observe(n,{attributes:!0,attributeFilter:["data-reactive-subscribe"]})}function C(t){let n=t.element,e=a.get(n);if(!e||e.root!==n)return;e.observer.disconnect(),e.removing=!0,queueMicrotask(()=>{if(!e.removing)return;s.delete(e.id),c(e,null)})}function c(t,n){let e=t.src;if(t.src=n||null,t.src)f(t.src,t.id);if(e&&e!==t.src)A(e,t.id)}function d(){for(let t of s.values())if(t.src&&!t.removing&&t.root.isConnected)f(t.src,t.id)}function f(t,n){let e=i.get(t);if(!e)i.set(t,e={source:null,ids:new Set,connected:!1});if(e.ids.add(n),!e.source?.isConnected)e.source=h(t,e)}function A(t,n){let e=i.get(t);if(!e)return;if(e.ids.delete(n),e.ids.size)return;i.delete(t),e.source.remove()}function h(t,n){if(!l())setTimeout(()=>{if(!l())console.error('[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: import "pgbus/stream_source_element"')},5000);let e=document.createElement("pgbus-stream-source");e.setAttribute("src",t),e.setAttribute("data-reactive-subscribe-source",""),e.hidden=!0;let o=T(t);if(o!=null)e.setAttribute("since-id",String(o));return e.addEventListener("pgbus:connected",(m)=>{if(n.source!==e)return;let v=n.connected;n.connected=!0;let g=m.detail?.connectionId??null;for(let p of n.ids){let r=s.get(p);if(!r||r.removing||r.src!==t||!r.root.isConnected)continue;r.root.dispatchEvent(new CustomEvent("reactive:subscribed",{detail:{connectionId:g,reconnect:v}}))}}),b.add(e),E().appendChild(e),e}function T(t){let n=null;for(let e of document.querySelectorAll("[data-reactive-subscribe-since]")){if(e.getAttribute("data-reactive-subscribe")!==t)continue;let o=Number(e.getAttribute("data-reactive-subscribe-since"));if(Number.isFinite(o)&&o>=0&&(n==null||o<n))n=o}return n}function E(){let t=[...document.body.children].find((n)=>n.localName==="reactive-stream-sources");if(!t)t=document.createElement("reactive-stream-sources"),t.hidden=!0,t.setAttribute("data-turbo-permanent",""),document.body.appendChild(t);return t}var l=()=>!!globalThis.customElements?.get?.("pgbus-stream-source");export{w as connect,C as disconnect};
2
2
 
3
- //# debugId=3B73B338E42109E564756E2164756E21
3
+ //# debugId=87CA872C63C935B364756E2164756E21
4
4
  //# sourceMappingURL=subscribe.min.js.map
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["features/subscribe.js"],
4
4
  "sourcesContent": [
5
- "// phlex/reactive/features/subscribe — a root subscribed to its own broadcast\n// stream (issue #474). One of the feature modules the core imports on demand\n// (issue #275); in the default client it is part of the one file.\n//\n// The markup is the Ruby side's (reactive_subscribe):\n//\n// data-reactive-subscribe=\"<signed src>\" the stream's signed SSE src\n//\n// While the root is on the page, the module keeps a <pgbus-stream-source> for\n// that src in a hidden box at the end of <body> (marked\n// data-reactive-subscribe-source); pgbus's element\n// applies what arrives as Turbo Stream actions. Two roots with the same src\n// share ONE source (an append lands once, not once per root): it closes when\n// the last of them goes. A replace of the root disconnects and reconnects the\n// controller: the source is released only a microtask after a disconnect, so\n// the same root id coming back keeps the connection. A change of the src (a\n// morph that re-keys the root) moves the root to the new stream; a morph that\n// keeps it changes nothing. A page refresh by morph (Turbo 8) keeps the\n// source, connection and all (see box). Should a render drop it anyway while\n// the roots stay connected, the module mints a fresh source for every stream\n// a live root still needs.\n//\n// This module never imports the core.\n\nconst ATTR = \"data-reactive-subscribe\"\nconst MARK = \"data-reactive-subscribe-source\"\n\n// src -> { source, ids }: one source per stream, and the root ids on it.\nconst streams = new Map()\n// root id -> { id, root, src, removing, observer }: kept across a replace.\n// The id is the one the root had at connect: a root whose id changes while\n// connected still releases what it holds (byRoot finds its entry).\nconst roots = new Map()\nconst byRoot = new WeakMap()\n// The sources this module minted (a Turbo cache restore brings back clones).\nconst minted = new WeakSet()\nlet listening = false\n\nexport function connect(controller) {\n if (!listening) {\n listening = true\n document.addEventListener(\"turbo:morph\", rejoin)\n document.addEventListener(\"turbo:render\", rejoin)\n }\n const root = controller.element\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { id: root.id, src: null }))\n byRoot.set(root, entry)\n entry.observer?.disconnect()\n entry.root = root\n entry.removing = false\n for (const clone of document.querySelectorAll(`pgbus-stream-source[${MARK}]`)) {\n if (!minted.has(clone)) clone.remove()\n }\n follow(entry, root.getAttribute(ATTR))\n entry.observer = new MutationObserver(() => follow(entry, root.getAttribute(ATTR)))\n entry.observer.observe(root, { attributes: true, attributeFilter: [ATTR] })\n}\n\nexport function disconnect(controller) {\n const root = controller.element\n const entry = byRoot.get(root)\n if (!entry || entry.root !== root) return\n entry.observer.disconnect()\n entry.removing = true\n queueMicrotask(() => {\n if (!entry.removing) return\n roots.delete(entry.id)\n follow(entry, null)\n })\n}\n\n// Move the root to `src` (null: no stream): join the new one first, so a\n// shared source is never closed and reopened, then leave the old.\nfunction follow(entry, src) {\n const previous = entry.src\n entry.src = src || null\n if (entry.src) join(entry.src, entry.id)\n if (previous && previous !== entry.src) leave(previous, entry.id)\n}\n\n// After a page render, re-mint each source the page lost while a root on its\n// stream is still on the page (a root that left releases it on its own).\nfunction rejoin() {\n for (const entry of roots.values()) {\n if (entry.src && !entry.removing && entry.root.isConnected) join(entry.src, entry.id)\n }\n}\n\nfunction join(src, id) {\n let stream = streams.get(src)\n if (!stream?.source.isConnected) streams.set(src, (stream = { source: mint(src), ids: stream?.ids ?? new Set() }))\n stream.ids.add(id)\n}\n\nfunction leave(src, id) {\n const stream = streams.get(src)\n if (!stream) return\n stream.ids.delete(id)\n if (stream.ids.size) return\n streams.delete(src)\n stream.source.remove()\n}\n\nfunction mint(src) {\n // pgbus's client is an async module script: mint the element anyway (it\n // upgrades when pgbus defines it), and call a still-missing client a\n // misconfiguration only after a grace period.\n if (!defined()) {\n setTimeout(() => {\n if (!defined()) {\n console.error(\n \"[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: \" +\n 'import \"pgbus/stream_source_element\"',\n )\n }\n }, 5000)\n }\n const source = document.createElement(\"pgbus-stream-source\")\n source.setAttribute(\"src\", src)\n source.setAttribute(MARK, \"\")\n source.hidden = true\n minted.add(source)\n box().appendChild(source)\n return source\n}\n\n// The minted sources live in one hidden <reactive-stream-sources> at the end\n// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps\n// it, sources and connections included. A tag no server page renders, so\n// Idiomorph never pairs it with (and drops) a node the refreshed page adds.\nfunction box() {\n let el = [...document.body.children].find((child) => child.localName === \"reactive-stream-sources\")\n if (!el) {\n el = document.createElement(\"reactive-stream-sources\")\n el.hidden = true\n el.setAttribute(\"data-turbo-permanent\", \"\")\n document.body.appendChild(el)\n }\n return el\n}\n\nconst defined = () => !!globalThis.customElements?.get?.(\"pgbus-stream-source\")\n"
5
+ "// phlex/reactive/features/subscribe — a root subscribed to its own broadcast\n// stream (issue #474). One of the feature modules the core imports on demand\n// (issue #275); in the default client it is part of the one file.\n//\n// The markup is the Ruby side's (reactive_subscribe):\n//\n// data-reactive-subscribe=\"<signed src>\" the stream's signed SSE src\n// data-reactive-subscribe-since=\"<id>\" a durable stream's watermark at\n// render (issue #478)\n//\n// While the root is on the page, the module keeps a <pgbus-stream-source> for\n// that src in a hidden box at the end of <body> (marked\n// data-reactive-subscribe-source); pgbus's element\n// applies what arrives as Turbo Stream actions. Two roots with the same src\n// share ONE source (an append lands once, not once per root): it closes when\n// the last of them goes. A replace of the root disconnects and reconnects the\n// controller: the source is released only a microtask after a disconnect, so\n// the same root id coming back keeps the connection. A change of the src (a\n// morph that re-keys the root) moves the root to the new stream; a morph that\n// keeps it changes nothing. A page refresh by morph (Turbo 8) keeps the\n// source, connection and all (see box). Should a render drop it anyway while\n// the roots stay connected, the module mints a fresh source for every stream\n// a live root still needs.\n//\n// Catching up (issue #478). A source opens with since-id set to the lowest\n// watermark of the roots on its src, so pgbus replays a durable broadcast\n// sent between the render and the connection. A source minted again (after a\n// page render dropped it) opens from what its roots render now, their\n// watermarks: it may replay a frame a root already shows, never skip one.\n// Each time\n// a source connects (its pgbus:connected), every root on it gets\n// reactive:subscribed (detail { connectionId, reconnect }; reconnect is false\n// on the stream's first connection), not bubbling, so a component can refetch\n// what an ephemeral stream could not replay. A root that joins a source\n// already connected gets no event: the source is live, and a root that comes\n// back under the same id (a replace) must not refresh again and again.\n//\n// This module never imports the core.\n\nconst ATTR = \"data-reactive-subscribe\"\nconst SINCE = \"data-reactive-subscribe-since\"\nconst MARK = \"data-reactive-subscribe-source\"\n\n// src -> { source, ids, connected }: one source per stream, the root ids on\n// it, and whether it ever connected.\nconst streams = new Map()\n// root id -> { id, root, src, removing, observer }: kept across a replace.\n// The id is the one the root had at connect: a root whose id changes while\n// connected still releases what it holds (byRoot finds its entry).\nconst roots = new Map()\nconst byRoot = new WeakMap()\n// The sources this module minted (a Turbo cache restore brings back clones).\nconst minted = new WeakSet()\nlet listening = false\n\nexport function connect(controller) {\n if (!listening) {\n listening = true\n document.addEventListener(\"turbo:morph\", rejoin)\n document.addEventListener(\"turbo:render\", rejoin)\n }\n const root = controller.element\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { id: root.id, src: null }))\n byRoot.set(root, entry)\n entry.observer?.disconnect()\n entry.root = root\n entry.removing = false\n for (const clone of document.querySelectorAll(`pgbus-stream-source[${MARK}]`)) {\n if (!minted.has(clone)) clone.remove()\n }\n follow(entry, root.getAttribute(ATTR))\n entry.observer = new MutationObserver(() => follow(entry, root.getAttribute(ATTR)))\n entry.observer.observe(root, { attributes: true, attributeFilter: [ATTR] })\n}\n\nexport function disconnect(controller) {\n const root = controller.element\n const entry = byRoot.get(root)\n if (!entry || entry.root !== root) return\n entry.observer.disconnect()\n entry.removing = true\n queueMicrotask(() => {\n if (!entry.removing) return\n roots.delete(entry.id)\n follow(entry, null)\n })\n}\n\n// Move the root to `src` (null: no stream): join the new one first, so a\n// shared source is never closed and reopened, then leave the old.\nfunction follow(entry, src) {\n const previous = entry.src\n entry.src = src || null\n if (entry.src) join(entry.src, entry.id)\n if (previous && previous !== entry.src) leave(previous, entry.id)\n}\n\n// After a page render, re-mint each source the page lost while a root on its\n// stream is still on the page (a root that left releases it on its own).\nfunction rejoin() {\n for (const entry of roots.values()) {\n if (entry.src && !entry.removing && entry.root.isConnected) join(entry.src, entry.id)\n }\n}\n\nfunction join(src, id) {\n let stream = streams.get(src)\n if (!stream) streams.set(src, (stream = { source: null, ids: new Set(), connected: false }))\n stream.ids.add(id)\n if (!stream.source?.isConnected) stream.source = mint(src, stream)\n}\n\nfunction leave(src, id) {\n const stream = streams.get(src)\n if (!stream) return\n stream.ids.delete(id)\n if (stream.ids.size) return\n streams.delete(src)\n stream.source.remove()\n}\n\nfunction mint(src, stream) {\n // pgbus's client is an async module script: mint the element anyway (it\n // upgrades when pgbus defines it), and call a still-missing client a\n // misconfiguration only after a grace period.\n if (!defined()) {\n setTimeout(() => {\n if (!defined()) {\n console.error(\n \"[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: \" +\n 'import \"pgbus/stream_source_element\"',\n )\n }\n }, 5000)\n }\n const source = document.createElement(\"pgbus-stream-source\")\n source.setAttribute(\"src\", src)\n source.setAttribute(MARK, \"\")\n source.hidden = true\n // Before the append: pgbus reads since-id once, when the element connects.\n const since = lowestWatermark(src)\n if (since != null) source.setAttribute(\"since-id\", String(since))\n source.addEventListener(\"pgbus:connected\", (event) => {\n if (stream.source !== source) return\n const reconnect = stream.connected\n stream.connected = true\n const connectionId = event.detail?.connectionId ?? null\n for (const id of stream.ids) {\n const entry = roots.get(id)\n if (!entry || entry.removing || entry.src !== src || !entry.root.isConnected) continue\n entry.root.dispatchEvent(new CustomEvent(\"reactive:subscribed\", { detail: { connectionId, reconnect } }))\n }\n })\n minted.add(source)\n box().appendChild(source)\n return source\n}\n\n// Where a durable stream's source replays from: the lowest watermark of the\n// roots on its src (two roots rendered apart share the source). null for an\n// ephemeral stream (no root carries a watermark).\nfunction lowestWatermark(src) {\n let since = null\n for (const root of document.querySelectorAll(`[${SINCE}]`)) {\n if (root.getAttribute(ATTR) !== src) continue\n const value = Number(root.getAttribute(SINCE))\n if (Number.isFinite(value) && value >= 0 && (since == null || value < since)) since = value\n }\n return since\n}\n\n// The minted sources live in one hidden <reactive-stream-sources> at the end\n// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps\n// it, sources and connections included. A tag no server page renders, so\n// Idiomorph never pairs it with (and drops) a node the refreshed page adds.\nfunction box() {\n let el = [...document.body.children].find((child) => child.localName === \"reactive-stream-sources\")\n if (!el) {\n el = document.createElement(\"reactive-stream-sources\")\n el.hidden = true\n el.setAttribute(\"data-turbo-permanent\", \"\")\n document.body.appendChild(el)\n }\n return el\n}\n\nconst defined = () => !!globalThis.customElements?.get?.(\"pgbus-stream-source\")\n"
6
6
  ],
7
- "mappings": "AA4BA,IAAM,EAAU,IAAI,IAId,EAAQ,IAAI,IACZ,EAAS,IAAI,QAEb,EAAS,IAAI,QACf,EAAY,GAET,SAAS,CAAO,CAAC,EAAY,CAClC,GAAI,CAAC,EACH,EAAY,GACZ,SAAS,iBAAiB,cAAe,CAAM,EAC/C,SAAS,iBAAiB,eAAgB,CAAM,EAElD,IAAM,EAAO,EAAW,QACpB,EAAQ,EAAM,IAAI,EAAK,EAAE,EAC7B,GAAI,CAAC,EAAO,EAAM,IAAI,EAAK,GAAK,EAAQ,CAAE,GAAI,EAAK,GAAI,IAAK,IAAK,CAAE,EACnE,EAAO,IAAI,EAAM,CAAK,EACtB,EAAM,UAAU,WAAW,EAC3B,EAAM,KAAO,EACb,EAAM,SAAW,GACjB,QAAW,KAAS,SAAS,iBAAiB,qDAA8B,EAC1E,GAAI,CAAC,EAAO,IAAI,CAAK,EAAG,EAAM,OAAO,EAEvC,EAAO,EAAO,EAAK,aA9BR,yBA8ByB,CAAC,EACrC,EAAM,SAAW,IAAI,iBAAiB,IAAM,EAAO,EAAO,EAAK,aA/BpD,yBA+BqE,CAAC,CAAC,EAClF,EAAM,SAAS,QAAQ,EAAM,CAAE,WAAY,GAAM,gBAAiB,CAhCvD,yBAgC4D,CAAE,CAAC,EAGrE,SAAS,CAAU,CAAC,EAAY,CACrC,IAAM,EAAO,EAAW,QAClB,EAAQ,EAAO,IAAI,CAAI,EAC7B,GAAI,CAAC,GAAS,EAAM,OAAS,EAAM,OACnC,EAAM,SAAS,WAAW,EAC1B,EAAM,SAAW,GAC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