phlex-reactive 0.16.13 → 0.17.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 +4 -4
- data/CHANGELOG.md +99 -16
- data/README.md +60 -3
- data/app/javascript/phlex/reactive/core.js +1 -0
- data/app/javascript/phlex/reactive/core.min.js +4 -4
- data/app/javascript/phlex/reactive/core.min.js.map +4 -4
- data/app/javascript/phlex/reactive/features/presence.js +37 -3
- data/app/javascript/phlex/reactive/features/presence.min.js +2 -2
- data/app/javascript/phlex/reactive/features/presence.min.js.map +3 -3
- data/app/javascript/phlex/reactive/features/subscribe.js +143 -0
- data/app/javascript/phlex/reactive/features/subscribe.min.js +4 -0
- data/app/javascript/phlex/reactive/features/subscribe.min.js.map +10 -0
- data/app/javascript/phlex/reactive/reactive_controller.js +2 -0
- data/app/javascript/phlex/reactive/reactive_controller.min.js +4 -4
- data/app/javascript/phlex/reactive/reactive_controller.min.js.map +7 -6
- data/app/javascript/phlex/reactive/runtime.js +37 -2
- data/lib/phlex/reactive/component/dsl.rb +3 -0
- data/lib/phlex/reactive/component/editable.rb +3 -0
- data/lib/phlex/reactive/component/helpers.rb +26 -0
- data/lib/phlex/reactive/component/presence_declaration.rb +2 -1
- data/lib/phlex/reactive/component/registry.rb +5 -2
- data/lib/phlex/reactive/component/subscribe_declaration.rb +132 -0
- data/lib/phlex/reactive/component.rb +1 -0
- data/lib/phlex/reactive/doctor.rb +51 -4
- data/lib/phlex/reactive/engine.rb +3 -3
- data/lib/phlex/reactive/js.rb +51 -0
- data/lib/phlex/reactive/response.rb +4 -1
- data/lib/phlex/reactive/streamable.rb +10 -3
- data/lib/phlex/reactive/subscribe.rb +125 -0
- data/lib/phlex/reactive/version.rb +1 -1
- metadata +6 -1
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
// cannot say which row is "me": the module does, from the tab's seat.
|
|
20
20
|
//
|
|
21
21
|
// The subscription. The root carries its presence stream's signed src; the
|
|
22
|
-
// module mints a <pgbus-stream-source presence> for it in
|
|
22
|
+
// module mints a <pgbus-stream-source presence> for it in a hidden box at the
|
|
23
|
+
// end of <body>, with a
|
|
23
24
|
// deterministic id (reactive-presence-src-<root id>), as the defer push lane
|
|
24
25
|
// mints its one-shot source. pgbus's element mints the tab's seat (one per
|
|
25
26
|
// document), sends it as ?seat= and exposes it as element.pgbusSeat: the
|
|
@@ -27,7 +28,11 @@
|
|
|
27
28
|
// connection id) keeps it, and this module reads it to find "me". A plain
|
|
28
29
|
// replace of the root disconnects and
|
|
29
30
|
// reconnects the controller: the source is removed only a microtask after a
|
|
30
|
-
// disconnect, and kept when the same root id comes back.
|
|
31
|
+
// disconnect, and kept when the same root id comes back. A page refresh by
|
|
32
|
+
// morph (Turbo 8) keeps the source and the tab keeps its seat (#474's review;
|
|
33
|
+
// see box). Should a render drop it anyway while
|
|
34
|
+
// the root stays connected, the module mints a fresh one, whose
|
|
35
|
+
// pgbus:connected announces the tab again.
|
|
31
36
|
//
|
|
32
37
|
// Announcing. On every pgbus:connected (the first one and each reconnect) the
|
|
33
38
|
// module sends presence_set with the tab's last state ({} at first), through
|
|
@@ -56,8 +61,14 @@ const roots = new Map()
|
|
|
56
61
|
// The sources this module minted and listens to (a Turbo cache restore brings
|
|
57
62
|
// back a clone without the listener).
|
|
58
63
|
const minted = new WeakSet()
|
|
64
|
+
let listening = false
|
|
59
65
|
|
|
60
66
|
export function connect(controller, core) {
|
|
67
|
+
if (!listening) {
|
|
68
|
+
listening = true
|
|
69
|
+
document.addEventListener("turbo:morph", resubscribe)
|
|
70
|
+
document.addEventListener("turbo:render", resubscribe)
|
|
71
|
+
}
|
|
61
72
|
const root = controller.element
|
|
62
73
|
let entry = roots.get(root.id)
|
|
63
74
|
if (!entry) roots.set(root.id, (entry = { state: null, seats: null }))
|
|
@@ -107,6 +118,14 @@ export function set(root, state) {
|
|
|
107
118
|
send(entry)
|
|
108
119
|
}
|
|
109
120
|
|
|
121
|
+
// After a page render: a root still on the page whose source the render
|
|
122
|
+
// dropped subscribes again (subscribe keeps a source that is still there).
|
|
123
|
+
function resubscribe() {
|
|
124
|
+
for (const entry of roots.values()) {
|
|
125
|
+
if (entry.core && !entry.removing && entry.root.isConnected) subscribe(entry.root)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
110
129
|
function subscribe(root) {
|
|
111
130
|
const id = SOURCE + root.id
|
|
112
131
|
const found = document.getElementById(id)
|
|
@@ -138,7 +157,22 @@ function subscribe(root) {
|
|
|
138
157
|
check(entry)
|
|
139
158
|
})
|
|
140
159
|
minted.add(source)
|
|
141
|
-
|
|
160
|
+
box().appendChild(source)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// The minted sources live in one hidden <reactive-stream-sources> at the end
|
|
164
|
+
// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps
|
|
165
|
+
// it, sources and connections included. A tag no server page renders, so
|
|
166
|
+
// Idiomorph never pairs it with (and drops) a node the refreshed page adds.
|
|
167
|
+
function box() {
|
|
168
|
+
let el = [...document.body.children].find((child) => child.localName === "reactive-stream-sources")
|
|
169
|
+
if (!el) {
|
|
170
|
+
el = document.createElement("reactive-stream-sources")
|
|
171
|
+
el.hidden = true
|
|
172
|
+
el.setAttribute("data-turbo-permanent", "")
|
|
173
|
+
document.body.appendChild(el)
|
|
174
|
+
}
|
|
175
|
+
return el
|
|
142
176
|
}
|
|
143
177
|
|
|
144
178
|
const defined = () => !!globalThis.customElements?.get?.("pgbus-stream-source")
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var c=new Map,m=new WeakSet;function
|
|
1
|
+
var c=new Map,m=new WeakSet,f=!1;function S(e,r){if(!f)f=!0,document.addEventListener("turbo:morph",p),document.addEventListener("turbo:render",p);let t=e.element,n=c.get(t.id);if(!n)c.set(t.id,n={state:null,seats:null});n.root=t,n.core=r,n.removing=!1,b(t);let o=(i)=>i.target===t&&queueMicrotask(()=>E(n)),a=(i)=>i.target===t&&i.detail?.action==="presence_set"&&console.error(`[phlex-reactive] #${t.id}: its presence_set request failed`,i.detail);t.addEventListener("turbo:morph-element",o),t.addEventListener("reactive:error",a),n.off=()=>{t.removeEventListener("turbo:morph-element",o),t.removeEventListener("reactive:error",a)},E(n)}function M(e){let r=e.element,t=c.get(r.id);if(!t||t.root!==r)return;t.off(),t.removing=!0,queueMicrotask(()=>{if(!t.removing)return;c.delete(r.id),r.ownerDocument.getElementById("reactive-presence-src-"+r.id)?.remove()})}function w(e,r){if(!e?.hasAttribute?.("data-reactive-presence")){console.warn("[phlex-reactive] js.presence ran outside a reactive_presence root — ignored");return}let t=c.get(e.id);if(!t)c.set(e.id,t={root:e,state:null,seats:null});t.state={...t.state,...r},v(t)}function p(){for(let e of c.values())if(e.core&&!e.removing&&e.root.isConnected)b(e.root)}function b(e){let r="reactive-presence-src-"+e.id,t=document.getElementById(r);if(t&&m.has(t))return;if(t?.remove(),!g())setTimeout(()=>{if(!g())console.error("[phlex-reactive] reactive_presence needs the pgbus client (<pgbus-stream-source>) on this page — "+`#${e.id} is not subscribed to its presence stream`)},5000);let n=document.createElement("pgbus-stream-source");n.id=r,n.setAttribute("src",e.getAttribute("data-reactive-presence")),n.setAttribute("presence",""),n.hidden=!0,n.addEventListener("pgbus:connected",()=>{let o=c.get(e.id);if(!o||o.removing)return;v(o),A(o)}),m.add(n),R().appendChild(n)}function R(){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 g=()=>!!globalThis.customElements?.get?.("pgbus-stream-source");function A(e){clearTimeout(e.timer),e.timer=setTimeout(()=>{let r=e.root;if(e.removing||!r.isConnected)return;let t=r.ownerDocument.getElementById("reactive-presence-src-"+r.id)?.pgbusSeat;if(t&&h(r).has(t))return;console.warn(`[phlex-reactive] #${r.id} never appeared on its presence roster: config.streams_authorize returned no identity for this stream, or its presence_set request failed (logged above)`)},2500)}function v(e){let r=e.root;if(!e.core||!r.ownerDocument.getElementById("reactive-presence-src-"+r.id)?.getAttribute("connection-id"))return;e.core.proceed(r,"presence_set",JSON.stringify(e.state??{}))}function h(e){let r=e.getAttribute("data-reactive-presence-roster");if(r!=null)return new Set(r.split(" ").filter(Boolean));return new Set([...e.querySelectorAll("[data-reactive-presence-peer]")].map((t)=>t.getAttribute("data-reactive-presence-peer")))}function E(e){let r=e.root,t=r.ownerDocument.getElementById("reactive-presence-src-"+r.id)?.pgbusSeat,n=r.querySelectorAll("[data-reactive-presence-peer],[data-reactive-presence-member]"),o=h(r),a;for(let s of n)if(s.getAttribute("data-reactive-presence-peer")===t)a=s.getAttribute("data-reactive-presence-member");for(let s of n){let l=s.getAttribute("data-reactive-presence-peer")===t;s.toggleAttribute("data-reactive-presence-me",l),s.toggleAttribute("data-reactive-presence-same-member",!l&&a!=null&&s.getAttribute("data-reactive-presence-member")===a)}let i=e.seats;if(e.seats=o,!i)return;let u=[...o].filter((s)=>!i.has(s)),d=[...i].filter((s)=>!o.has(s));if(u.length||d.length)e.core.emit("reactive:presence",{joined:u,left:d,me:t??null})}export{S as connect,M as disconnect,w as set};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=DD955BD5A2C30BCA64756E2164756E21
|
|
4
4
|
//# sourceMappingURL=presence.min.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["features/presence.js"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"// phlex/reactive/features/presence — who is viewing a stream, one seat per\n// tab (issue #358). 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_presence and its helpers):\n//\n// data-reactive-presence=\"<signed src>\" the roster root; its stream\n// data-reactive-presence-roster=\"<seats>\" on the root: the seats rendered\n// data-reactive-presence-peer=\"<seat>\" a tab's element\n// data-reactive-presence-member=\"<id>\" a person's (or a tab's) element\n//\n// and what this module sets, for the app's CSS, after the first paint and\n// after every morph of the root:\n//\n// data-reactive-presence-me this tab's own peer element\n// data-reactive-presence-same-member this person's other elements\n//\n// The roster is rendered ONCE for every viewer (one broadcast), so the server\n// cannot say which row is \"me\": the module does, from the tab's seat.\n//\n// The subscription. The root carries its presence stream's signed src; the\n// module mints a <pgbus-stream-source presence> for it in <body>, with a\n// deterministic id (reactive-presence-src-<root id>), as the defer push lane\n// mints its one-shot source. pgbus's element mints the tab's seat (one per\n// document), sends it as ?seat= and exposes it as element.pgbusSeat: the\n// transport stores it on the tab's presence row, a reconnect (a new\n// connection id) keeps it, and this module reads it to find \"me\". A plain\n// replace of the root disconnects and\n// reconnects the controller: the source is removed only a microtask after a\n// disconnect, and kept when the same root id comes back.\n//\n// Announcing. On every pgbus:connected (the first one and each reconnect) the\n// module sends presence_set with the tab's last state ({} at first), through\n// the root's own pipeline (core.proceed): the signed token, the typed state,\n// the request queue. That write puts the tab on the roster for everyone (and\n// the actor's own reply shows it), and a reconnect, which is a fresh row with\n// no state, gets its state back. The runtime sends the source's connection id\n// as X-Reactive-Presence, so the write lands on this tab's row.\n//\n// Events. The module keeps the seats it last saw in the root (the root's\n// data-reactive-presence-roster list); when a morph changes them it dispatches\n// reactive:presence on the root with { joined, left, me } — from the DOM, so a\n// replayed frame that changes nothing fires nothing.\n//\n// This module never imports the core: it reaches a controller through the\n// `core` handle (core.proceed, core.emit).\n\nconst SOURCE = \"reactive-presence-src-\"\nconst PEER = \"data-reactive-presence-peer\"\nconst MEMBER = \"data-reactive-presence-member\"\nconst ROSTER = \"data-reactive-presence-roster\"\n\n// root id -> { root, core, state, seats, removing, off }: what a root's\n// connection left behind, kept across a replace of the root.\nconst roots = new Map()\n// The sources this module minted and listens to (a Turbo cache restore brings\n// back a clone without the listener).\nconst minted = new WeakSet()\n\nexport function connect(controller, core) {\n const root = controller.element\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { state: null, seats: null }))\n entry.root = root\n entry.core = core\n entry.removing = false\n subscribe(root)\n const morphed = (event) => event.target === root && queueMicrotask(() => paint(entry))\n // A failed announce (a network error, a 5xx) leaves the tab's row without\n // its note and state until the next js.presence or reconnect: say so.\n const failed = (event) =>\n event.target === root &&\n event.detail?.action === \"presence_set\" &&\n console.error(`[phlex-reactive] #${root.id}: its presence_set request failed`, event.detail)\n root.addEventListener(\"turbo:morph-element\", morphed)\n root.addEventListener(\"reactive:error\", failed)\n entry.off = () => {\n root.removeEventListener(\"turbo:morph-element\", morphed)\n root.removeEventListener(\"reactive:error\", failed)\n }\n paint(entry)\n}\n\nexport function disconnect(controller) {\n const root = controller.element\n const entry = roots.get(root.id)\n if (!entry || entry.root !== root) return\n entry.off()\n entry.removing = true\n queueMicrotask(() => {\n if (!entry.removing) return\n roots.delete(root.id)\n root.ownerDocument.getElementById(SOURCE + root.id)?.remove()\n })\n}\n\n// js.presence(**state): merge into the tab's state and send it. Before the\n// tab has a connection the state waits for the announce on connect.\nexport function set(root, state) {\n if (!root?.hasAttribute?.(\"data-reactive-presence\")) {\n console.warn(\"[phlex-reactive] js.presence ran outside a reactive_presence root — ignored\")\n return\n }\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { root, state: null, seats: null }))\n entry.state = { ...entry.state, ...state }\n send(entry)\n}\n\nfunction subscribe(root) {\n const id = SOURCE + root.id\n const found = document.getElementById(id)\n if (found && minted.has(found)) return\n found?.remove()\n // pgbus's client is an async module script, so this controller can connect\n // before <pgbus-stream-source> is defined: 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_presence needs the pgbus client (<pgbus-stream-source>) on this page — \" +\n `#${root.id} is not subscribed to its presence stream`,\n )\n }\n }, 5000)\n }\n const source = document.createElement(\"pgbus-stream-source\")\n source.id = id\n source.setAttribute(\"src\", root.getAttribute(\"data-reactive-presence\"))\n source.setAttribute(\"presence\", \"\")\n source.hidden = true\n source.addEventListener(\"pgbus:connected\", () => {\n const entry = roots.get(root.id)\n if (!entry || entry.removing) return\n send(entry)\n check(entry)\n })\n minted.add(source)\n document.body.appendChild(source)\n}\n\nconst defined = () => !!globalThis.customElements?.get?.(\"pgbus-stream-source\")\n\n// pgbus fires pgbus:connected only after the tab's seat has joined, so the\n// announce lands on its row. A tab still off its roster a moment later has no\n// identity (pgbus joins an anonymous connection to nothing), or its announce\n// failed: say so once. No retry (an accepted risk).\nfunction check(entry) {\n clearTimeout(entry.timer)\n entry.timer = setTimeout(() => {\n const root = entry.root\n if (entry.removing || !root.isConnected) return\n const mine = root.ownerDocument.getElementById(SOURCE + root.id)?.pgbusSeat\n if (mine && seatsOf(root).has(mine)) return\n console.warn(\n `[phlex-reactive] #${root.id} never appeared on its presence roster: config.streams_authorize ` +\n \"returned no identity for this stream, or its presence_set request failed (logged above)\",\n )\n }, 2500)\n}\n\n// Send the tab's state through presence_set — only once the source has a\n// connection (the row the write lands on). Until then the announce on\n// pgbus:connected sends it.\nfunction send(entry) {\n const root = entry.root\n if (!entry.core || !root.ownerDocument.getElementById(SOURCE + root.id)?.getAttribute(\"connection-id\")) return\n entry.core.proceed(root, \"presence_set\", JSON.stringify(entry.state ?? {}))\n}\n\n// The seats the root's render saw: the list the server names on the root\n// (data-reactive-presence-roster: a template need not render an element per\n// tab), or, without it, the tabs' own elements.\nfunction seatsOf(root) {\n const named = root.getAttribute(ROSTER)\n if (named != null) return new Set(named.split(\" \").filter(Boolean))\n return new Set([...root.querySelectorAll(`[${PEER}]`)].map((el) => el.getAttribute(PEER)))\n}\n\n// Mark \"me\" and this person's other elements, then report the seats that\n// joined or left since the last paint.\nfunction paint(entry) {\n const root = entry.root\n // The seat pgbus's element minted (undefined on a pgbus without seats).\n const mine = root.ownerDocument.getElementById(SOURCE + root.id)?.pgbusSeat\n const marked = root.querySelectorAll(`[${PEER}],[${MEMBER}]`)\n const seats = seatsOf(root)\n let member\n for (const el of marked) {\n if (el.getAttribute(PEER) === mine) member = el.getAttribute(MEMBER)\n }\n for (const el of marked) {\n const me = el.getAttribute(PEER) === mine\n el.toggleAttribute(\"data-reactive-presence-me\", me)\n el.toggleAttribute(\"data-reactive-presence-same-member\", !me && member != null && el.getAttribute(MEMBER) === member)\n }\n const before = entry.seats\n entry.seats = seats\n if (!before) return\n const joined = [...seats].filter((s) => !before.has(s))\n const left = [...before].filter((s) => !seats.has(s))\n if (joined.length || left.length) entry.core.emit(\"reactive:presence\", { joined, left, me: mine ?? null })\n}\n"
|
|
5
|
+
"// phlex/reactive/features/presence — who is viewing a stream, one seat per\n// tab (issue #358). 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_presence and its helpers):\n//\n// data-reactive-presence=\"<signed src>\" the roster root; its stream\n// data-reactive-presence-roster=\"<seats>\" on the root: the seats rendered\n// data-reactive-presence-peer=\"<seat>\" a tab's element\n// data-reactive-presence-member=\"<id>\" a person's (or a tab's) element\n//\n// and what this module sets, for the app's CSS, after the first paint and\n// after every morph of the root:\n//\n// data-reactive-presence-me this tab's own peer element\n// data-reactive-presence-same-member this person's other elements\n//\n// The roster is rendered ONCE for every viewer (one broadcast), so the server\n// cannot say which row is \"me\": the module does, from the tab's seat.\n//\n// The subscription. The root carries its presence stream's signed src; the\n// module mints a <pgbus-stream-source presence> for it in a hidden box at the\n// end of <body>, with a\n// deterministic id (reactive-presence-src-<root id>), as the defer push lane\n// mints its one-shot source. pgbus's element mints the tab's seat (one per\n// document), sends it as ?seat= and exposes it as element.pgbusSeat: the\n// transport stores it on the tab's presence row, a reconnect (a new\n// connection id) keeps it, and this module reads it to find \"me\". A plain\n// replace of the root disconnects and\n// reconnects the controller: the source is removed only a microtask after a\n// disconnect, and kept when the same root id comes back. A page refresh by\n// morph (Turbo 8) keeps the source and the tab keeps its seat (#474's review;\n// see box). Should a render drop it anyway while\n// the root stays connected, the module mints a fresh one, whose\n// pgbus:connected announces the tab again.\n//\n// Announcing. On every pgbus:connected (the first one and each reconnect) the\n// module sends presence_set with the tab's last state ({} at first), through\n// the root's own pipeline (core.proceed): the signed token, the typed state,\n// the request queue. That write puts the tab on the roster for everyone (and\n// the actor's own reply shows it), and a reconnect, which is a fresh row with\n// no state, gets its state back. The runtime sends the source's connection id\n// as X-Reactive-Presence, so the write lands on this tab's row.\n//\n// Events. The module keeps the seats it last saw in the root (the root's\n// data-reactive-presence-roster list); when a morph changes them it dispatches\n// reactive:presence on the root with { joined, left, me } — from the DOM, so a\n// replayed frame that changes nothing fires nothing.\n//\n// This module never imports the core: it reaches a controller through the\n// `core` handle (core.proceed, core.emit).\n\nconst SOURCE = \"reactive-presence-src-\"\nconst PEER = \"data-reactive-presence-peer\"\nconst MEMBER = \"data-reactive-presence-member\"\nconst ROSTER = \"data-reactive-presence-roster\"\n\n// root id -> { root, core, state, seats, removing, off }: what a root's\n// connection left behind, kept across a replace of the root.\nconst roots = new Map()\n// The sources this module minted and listens to (a Turbo cache restore brings\n// back a clone without the listener).\nconst minted = new WeakSet()\nlet listening = false\n\nexport function connect(controller, core) {\n if (!listening) {\n listening = true\n document.addEventListener(\"turbo:morph\", resubscribe)\n document.addEventListener(\"turbo:render\", resubscribe)\n }\n const root = controller.element\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { state: null, seats: null }))\n entry.root = root\n entry.core = core\n entry.removing = false\n subscribe(root)\n const morphed = (event) => event.target === root && queueMicrotask(() => paint(entry))\n // A failed announce (a network error, a 5xx) leaves the tab's row without\n // its note and state until the next js.presence or reconnect: say so.\n const failed = (event) =>\n event.target === root &&\n event.detail?.action === \"presence_set\" &&\n console.error(`[phlex-reactive] #${root.id}: its presence_set request failed`, event.detail)\n root.addEventListener(\"turbo:morph-element\", morphed)\n root.addEventListener(\"reactive:error\", failed)\n entry.off = () => {\n root.removeEventListener(\"turbo:morph-element\", morphed)\n root.removeEventListener(\"reactive:error\", failed)\n }\n paint(entry)\n}\n\nexport function disconnect(controller) {\n const root = controller.element\n const entry = roots.get(root.id)\n if (!entry || entry.root !== root) return\n entry.off()\n entry.removing = true\n queueMicrotask(() => {\n if (!entry.removing) return\n roots.delete(root.id)\n root.ownerDocument.getElementById(SOURCE + root.id)?.remove()\n })\n}\n\n// js.presence(**state): merge into the tab's state and send it. Before the\n// tab has a connection the state waits for the announce on connect.\nexport function set(root, state) {\n if (!root?.hasAttribute?.(\"data-reactive-presence\")) {\n console.warn(\"[phlex-reactive] js.presence ran outside a reactive_presence root — ignored\")\n return\n }\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { root, state: null, seats: null }))\n entry.state = { ...entry.state, ...state }\n send(entry)\n}\n\n// After a page render: a root still on the page whose source the render\n// dropped subscribes again (subscribe keeps a source that is still there).\nfunction resubscribe() {\n for (const entry of roots.values()) {\n if (entry.core && !entry.removing && entry.root.isConnected) subscribe(entry.root)\n }\n}\n\nfunction subscribe(root) {\n const id = SOURCE + root.id\n const found = document.getElementById(id)\n if (found && minted.has(found)) return\n found?.remove()\n // pgbus's client is an async module script, so this controller can connect\n // before <pgbus-stream-source> is defined: 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_presence needs the pgbus client (<pgbus-stream-source>) on this page — \" +\n `#${root.id} is not subscribed to its presence stream`,\n )\n }\n }, 5000)\n }\n const source = document.createElement(\"pgbus-stream-source\")\n source.id = id\n source.setAttribute(\"src\", root.getAttribute(\"data-reactive-presence\"))\n source.setAttribute(\"presence\", \"\")\n source.hidden = true\n source.addEventListener(\"pgbus:connected\", () => {\n const entry = roots.get(root.id)\n if (!entry || entry.removing) return\n send(entry)\n check(entry)\n })\n minted.add(source)\n box().appendChild(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\n// pgbus fires pgbus:connected only after the tab's seat has joined, so the\n// announce lands on its row. A tab still off its roster a moment later has no\n// identity (pgbus joins an anonymous connection to nothing), or its announce\n// failed: say so once. No retry (an accepted risk).\nfunction check(entry) {\n clearTimeout(entry.timer)\n entry.timer = setTimeout(() => {\n const root = entry.root\n if (entry.removing || !root.isConnected) return\n const mine = root.ownerDocument.getElementById(SOURCE + root.id)?.pgbusSeat\n if (mine && seatsOf(root).has(mine)) return\n console.warn(\n `[phlex-reactive] #${root.id} never appeared on its presence roster: config.streams_authorize ` +\n \"returned no identity for this stream, or its presence_set request failed (logged above)\",\n )\n }, 2500)\n}\n\n// Send the tab's state through presence_set — only once the source has a\n// connection (the row the write lands on). Until then the announce on\n// pgbus:connected sends it.\nfunction send(entry) {\n const root = entry.root\n if (!entry.core || !root.ownerDocument.getElementById(SOURCE + root.id)?.getAttribute(\"connection-id\")) return\n entry.core.proceed(root, \"presence_set\", JSON.stringify(entry.state ?? {}))\n}\n\n// The seats the root's render saw: the list the server names on the root\n// (data-reactive-presence-roster: a template need not render an element per\n// tab), or, without it, the tabs' own elements.\nfunction seatsOf(root) {\n const named = root.getAttribute(ROSTER)\n if (named != null) return new Set(named.split(\" \").filter(Boolean))\n return new Set([...root.querySelectorAll(`[${PEER}]`)].map((el) => el.getAttribute(PEER)))\n}\n\n// Mark \"me\" and this person's other elements, then report the seats that\n// joined or left since the last paint.\nfunction paint(entry) {\n const root = entry.root\n // The seat pgbus's element minted (undefined on a pgbus without seats).\n const mine = root.ownerDocument.getElementById(SOURCE + root.id)?.pgbusSeat\n const marked = root.querySelectorAll(`[${PEER}],[${MEMBER}]`)\n const seats = seatsOf(root)\n let member\n for (const el of marked) {\n if (el.getAttribute(PEER) === mine) member = el.getAttribute(MEMBER)\n }\n for (const el of marked) {\n const me = el.getAttribute(PEER) === mine\n el.toggleAttribute(\"data-reactive-presence-me\", me)\n el.toggleAttribute(\"data-reactive-presence-same-member\", !me && member != null && el.getAttribute(MEMBER) === member)\n }\n const before = entry.seats\n entry.seats = seats\n if (!before) return\n const joined = [...seats].filter((s) => !before.has(s))\n const left = [...before].filter((s) => !seats.has(s))\n if (joined.length || left.length) entry.core.emit(\"reactive:presence\", { joined, left, me: mine ?? null })\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "AA2DA,IAAM,EAAQ,IAAI,IAGZ,EAAS,IAAI,QACf,EAAY,GAET,SAAS,CAAO,CAAC,EAAY,EAAM,CACxC,GAAI,CAAC,EACH,EAAY,GACZ,SAAS,iBAAiB,cAAe,CAAW,EACpD,SAAS,iBAAiB,eAAgB,CAAW,EAEvD,IAAM,EAAO,EAAW,QACpB,EAAQ,EAAM,IAAI,EAAK,EAAE,EAC7B,GAAI,CAAC,EAAO,EAAM,IAAI,EAAK,GAAK,EAAQ,CAAE,MAAO,KAAM,MAAO,IAAK,CAAE,EACrE,EAAM,KAAO,EACb,EAAM,KAAO,EACb,EAAM,SAAW,GACjB,EAAU,CAAI,EACd,IAAM,EAAU,CAAC,IAAU,EAAM,SAAW,GAAQ,eAAe,IAAM,EAAM,CAAK,CAAC,EAG/E,EAAS,CAAC,IACd,EAAM,SAAW,GACjB,EAAM,QAAQ,SAAW,gBACzB,QAAQ,MAAM,qBAAqB,EAAK,sCAAuC,EAAM,MAAM,EAC7F,EAAK,iBAAiB,sBAAuB,CAAO,EACpD,EAAK,iBAAiB,iBAAkB,CAAM,EAC9C,EAAM,IAAM,IAAM,CAChB,EAAK,oBAAoB,sBAAuB,CAAO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O,IAAI,IAAI,EAAM,MAAM,GAAG,EAAE,OAAO,OAAO,CAAC,EAClE,OAAO,IAAI,IAAI,CAAC,GAAG,EAAK,iBAAiB,+BAAW,CAAC,EAAE,IAAI,CAAC,IAAO,EAAG,aA/J3D,6BA+J4E,CAAC,CAAC,EAK3F,SAAS,CAAK,CAAC,EAAO,CACpB,IAAM,EAAO,EAAM,KAEb,EAAO,EAAK,cAAc,eAxKnB,yBAwK2C,EAAK,EAAE,GAAG,UAC5D,EAAS,EAAK,iBAAiB,+DAAuB,EACtD,EAAQ,EAAQ,CAAI,EACtB,EACJ,QAAW,KAAM,EACf,GAAI,EAAG,aA5KE,6BA4Ke,IAAM,EAAM,EAAS,EAAG,aA3KrC,+BA2KwD,EAErE,QAAW,KAAM,EAAQ,CACvB,IAAM,EAAK,EAAG,aA/KL,6BA+KsB,IAAM,EACrC,EAAG,gBAAgB,4BAA6B,CAAE,EAClD,EAAG,gBAAgB,qCAAsC,CAAC,GAAM,GAAU,MAAQ,EAAG,aAhL1E,+BAgL6F,IAAM,CAAM,EAEtH,IAAM,EAAS,EAAM,MAErB,GADA,EAAM,MAAQ,EACV,CAAC,EAAQ,OACb,IAAM,EAAS,CAAC,GAAG,CAAK,EAAE,OAAO,CAAC,IAAM,CAAC,EAAO,IAAI,CAAC,CAAC,EAChD,EAAO,CAAC,GAAG,CAAM,EAAE,OAAO,CAAC,IAAM,CAAC,EAAM,IAAI,CAAC,CAAC,EACpD,GAAI,EAAO,QAAU,EAAK,OAAQ,EAAM,KAAK,KAAK,oBAAqB,CAAE,SAAQ,OAAM,GAAI,GAAQ,IAAK,CAAC",
|
|
8
|
+
"debugId": "DD955BD5A2C30BCA64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// phlex/reactive/features/subscribe — a root subscribed to its own broadcast
|
|
2
|
+
// stream (issue #474). One of the feature modules the core imports on demand
|
|
3
|
+
// (issue #275); in the default client it is part of the one file.
|
|
4
|
+
//
|
|
5
|
+
// The markup is the Ruby side's (reactive_subscribe):
|
|
6
|
+
//
|
|
7
|
+
// data-reactive-subscribe="<signed src>" the stream's signed SSE src
|
|
8
|
+
//
|
|
9
|
+
// While the root is on the page, the module keeps a <pgbus-stream-source> for
|
|
10
|
+
// that src in a hidden box at the end of <body> (marked
|
|
11
|
+
// data-reactive-subscribe-source); pgbus's element
|
|
12
|
+
// applies what arrives as Turbo Stream actions. Two roots with the same src
|
|
13
|
+
// share ONE source (an append lands once, not once per root): it closes when
|
|
14
|
+
// the last of them goes. A replace of the root disconnects and reconnects the
|
|
15
|
+
// controller: the source is released only a microtask after a disconnect, so
|
|
16
|
+
// the same root id coming back keeps the connection. A change of the src (a
|
|
17
|
+
// morph that re-keys the root) moves the root to the new stream; a morph that
|
|
18
|
+
// keeps it changes nothing. A page refresh by morph (Turbo 8) keeps the
|
|
19
|
+
// source, connection and all (see box). Should a render drop it anyway while
|
|
20
|
+
// the roots stay connected, the module mints a fresh source for every stream
|
|
21
|
+
// a live root still needs.
|
|
22
|
+
//
|
|
23
|
+
// This module never imports the core.
|
|
24
|
+
|
|
25
|
+
const ATTR = "data-reactive-subscribe"
|
|
26
|
+
const MARK = "data-reactive-subscribe-source"
|
|
27
|
+
|
|
28
|
+
// src -> { source, ids }: one source per stream, and the root ids on it.
|
|
29
|
+
const streams = new Map()
|
|
30
|
+
// root id -> { id, root, src, removing, observer }: kept across a replace.
|
|
31
|
+
// The id is the one the root had at connect: a root whose id changes while
|
|
32
|
+
// connected still releases what it holds (byRoot finds its entry).
|
|
33
|
+
const roots = new Map()
|
|
34
|
+
const byRoot = new WeakMap()
|
|
35
|
+
// The sources this module minted (a Turbo cache restore brings back clones).
|
|
36
|
+
const minted = new WeakSet()
|
|
37
|
+
let listening = false
|
|
38
|
+
|
|
39
|
+
export function connect(controller) {
|
|
40
|
+
if (!listening) {
|
|
41
|
+
listening = true
|
|
42
|
+
document.addEventListener("turbo:morph", rejoin)
|
|
43
|
+
document.addEventListener("turbo:render", rejoin)
|
|
44
|
+
}
|
|
45
|
+
const root = controller.element
|
|
46
|
+
let entry = roots.get(root.id)
|
|
47
|
+
if (!entry) roots.set(root.id, (entry = { id: root.id, src: null }))
|
|
48
|
+
byRoot.set(root, entry)
|
|
49
|
+
entry.observer?.disconnect()
|
|
50
|
+
entry.root = root
|
|
51
|
+
entry.removing = false
|
|
52
|
+
for (const clone of document.querySelectorAll(`pgbus-stream-source[${MARK}]`)) {
|
|
53
|
+
if (!minted.has(clone)) clone.remove()
|
|
54
|
+
}
|
|
55
|
+
follow(entry, root.getAttribute(ATTR))
|
|
56
|
+
entry.observer = new MutationObserver(() => follow(entry, root.getAttribute(ATTR)))
|
|
57
|
+
entry.observer.observe(root, { attributes: true, attributeFilter: [ATTR] })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function disconnect(controller) {
|
|
61
|
+
const root = controller.element
|
|
62
|
+
const entry = byRoot.get(root)
|
|
63
|
+
if (!entry || entry.root !== root) return
|
|
64
|
+
entry.observer.disconnect()
|
|
65
|
+
entry.removing = true
|
|
66
|
+
queueMicrotask(() => {
|
|
67
|
+
if (!entry.removing) return
|
|
68
|
+
roots.delete(entry.id)
|
|
69
|
+
follow(entry, null)
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Move the root to `src` (null: no stream): join the new one first, so a
|
|
74
|
+
// shared source is never closed and reopened, then leave the old.
|
|
75
|
+
function follow(entry, src) {
|
|
76
|
+
const previous = entry.src
|
|
77
|
+
entry.src = src || null
|
|
78
|
+
if (entry.src) join(entry.src, entry.id)
|
|
79
|
+
if (previous && previous !== entry.src) leave(previous, entry.id)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// After a page render, re-mint each source the page lost while a root on its
|
|
83
|
+
// stream is still on the page (a root that left releases it on its own).
|
|
84
|
+
function rejoin() {
|
|
85
|
+
for (const entry of roots.values()) {
|
|
86
|
+
if (entry.src && !entry.removing && entry.root.isConnected) join(entry.src, entry.id)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function join(src, id) {
|
|
91
|
+
let stream = streams.get(src)
|
|
92
|
+
if (!stream?.source.isConnected) streams.set(src, (stream = { source: mint(src), ids: stream?.ids ?? new Set() }))
|
|
93
|
+
stream.ids.add(id)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function leave(src, id) {
|
|
97
|
+
const stream = streams.get(src)
|
|
98
|
+
if (!stream) return
|
|
99
|
+
stream.ids.delete(id)
|
|
100
|
+
if (stream.ids.size) return
|
|
101
|
+
streams.delete(src)
|
|
102
|
+
stream.source.remove()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function mint(src) {
|
|
106
|
+
// pgbus's client is an async module script: mint the element anyway (it
|
|
107
|
+
// upgrades when pgbus defines it), and call a still-missing client a
|
|
108
|
+
// misconfiguration only after a grace period.
|
|
109
|
+
if (!defined()) {
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (!defined()) {
|
|
112
|
+
console.error(
|
|
113
|
+
"[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: " +
|
|
114
|
+
'import "pgbus/stream_source_element"',
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}, 5000)
|
|
118
|
+
}
|
|
119
|
+
const source = document.createElement("pgbus-stream-source")
|
|
120
|
+
source.setAttribute("src", src)
|
|
121
|
+
source.setAttribute(MARK, "")
|
|
122
|
+
source.hidden = true
|
|
123
|
+
minted.add(source)
|
|
124
|
+
box().appendChild(source)
|
|
125
|
+
return source
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The minted sources live in one hidden <reactive-stream-sources> at the end
|
|
129
|
+
// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps
|
|
130
|
+
// it, sources and connections included. A tag no server page renders, so
|
|
131
|
+
// Idiomorph never pairs it with (and drops) a node the refreshed page adds.
|
|
132
|
+
function box() {
|
|
133
|
+
let el = [...document.body.children].find((child) => child.localName === "reactive-stream-sources")
|
|
134
|
+
if (!el) {
|
|
135
|
+
el = document.createElement("reactive-stream-sources")
|
|
136
|
+
el.hidden = true
|
|
137
|
+
el.setAttribute("data-turbo-permanent", "")
|
|
138
|
+
document.body.appendChild(el)
|
|
139
|
+
}
|
|
140
|
+
return el
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const defined = () => !!globalThis.customElements?.get?.("pgbus-stream-source")
|
|
@@ -0,0 +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};
|
|
2
|
+
|
|
3
|
+
//# debugId=3B73B338E42109E564756E2164756E21
|
|
4
|
+
//# sourceMappingURL=subscribe.min.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["features/subscribe.js"],
|
|
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"
|
|
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,G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,OAEb,GADA,EAAO,IAAI,OAAO,CAAE,EAChB,EAAO,IAAI,KAAM,OACrB,EAAQ,OAAO,CAAG,EAClB,EAAO,OAAO,OAAO,EAGvB,SAAS,CAAI,CAAC,EAAK,CAIjB,GAAI,CAAC,EAAQ,EACX,WAAW,IAAM,CACf,GAAI,CAAC,EAAQ,EACX,QAAQ,MACN,uIAEF,GAED,IAAI,EAET,IAAM,EAAS,SAAS,cAAc,qBAAqB,EAM3D,OALA,EAAO,aAAa,MAAO,CAAG,EAC9B,EAAO,aA/FI,iCA+Fe,EAAE,EAC5B,EAAO,OAAS,GAChB,EAAO,IAAI,CAAM,EACjB,EAAI,EAAE,YAAY,CAAM,EACjB,EAOT,SAAS,CAAG,EAAG,CACb,IAAI,EAAK,CAAC,GAAG,SAAS,KAAK,QAAQ,EAAE,KAAK,CAAC,IAAU,EAAM,YAAc,yBAAyB,EAClG,GAAI,CAAC,EACH,EAAK,SAAS,cAAc,yBAAyB,EACrD,EAAG,OAAS,GACZ,EAAG,aAAa,uBAAwB,EAAE,EAC1C,SAAS,KAAK,YAAY,CAAE,EAE9B,OAAO,EAGT,IAAM,EAAU,IAAM,CAAC,CAAC,WAAW,gBAAgB,MAAM,qBAAqB",
|
|
8
|
+
"debugId": "3B73B338E42109E564756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -35,6 +35,7 @@ import * as compute from "phlex/reactive/features/compute"
|
|
|
35
35
|
import * as sortable from "phlex/reactive/features/sortable"
|
|
36
36
|
import * as drag from "phlex/reactive/features/drag"
|
|
37
37
|
import * as presence from "phlex/reactive/features/presence"
|
|
38
|
+
import * as subscribe from "phlex/reactive/features/subscribe"
|
|
38
39
|
import * as countdown from "phlex/reactive/features/countdown"
|
|
39
40
|
import * as effects from "phlex/reactive/features/effects"
|
|
40
41
|
import * as hints from "phlex/reactive/features/hints"
|
|
@@ -70,6 +71,7 @@ if (__SPLIT__) {
|
|
|
70
71
|
registerReactiveFeature("sortable", { connect: sortable.connect, disconnect: sortable.disconnect })
|
|
71
72
|
registerReactiveFeature("drag", { connect: drag.connect, disconnect: drag.disconnect })
|
|
72
73
|
registerReactiveFeature("presence", { connect: presence.connect, disconnect: presence.disconnect, set: presence.set })
|
|
74
|
+
registerReactiveFeature("subscribe", { connect: subscribe.connect, disconnect: subscribe.disconnect })
|
|
73
75
|
registerReactiveFeature("countdown", { connect: countdown.connect, disconnect: countdown.disconnect })
|
|
74
76
|
registerReactiveFeature("effects", { wrap: effects.wrap, sweep: effects.sweep })
|
|
75
77
|
registerReactiveFeature("hints", { optimistic: hints.optimistic, busy: hints.busy, resurrection: hints.resurrection })
|