// A demo stream for a preview whose visitor is signed out and so cannot // subscribe to a real Live stream (the framework refuses guests). The element // stands in for the subscription inside its own island: the island's stream // status reads as current, and a bounded timer sends the island action the // visible button sends, so updates arrive as they would over a stream. // Nothing leaves the visitor's island. The stream ends when the visitor acts // on the island, so a notice they marked read or a feed they posted to stays // theirs; disconnecting the element cancels the timer, and a hidden document // does no work. // // Attributes: `action` names the island action a `live:click` button sends // (absent: the status alone), `every` is the interval in milliseconds // (10000 by default) and `limit` the number of sends (12 by default). const CURRENT = 'Updates current' const STATUS = '[data-live-stream-status]' class DemoStream extends HTMLElement { #timer = 0 // The sends left, read from the attribute once: a morph that moves this // preserved element reconnects it, and a reconnection must not start the // stream over or revive one the visitor ended. #remaining = -1 #observer = null connectedCallback() { if (this.#remaining < 0) this.#remaining = Number.parseInt(this.getAttribute('limit') ?? '12', 10) || 12 const island = this.#island() if (island === null) return this.#mark(island) // The server's re-render after each action carries the disconnected // default, and the runtime preserves a status only over a declared // stream, so the mark is restored after every morph. this.#observer = new MutationObserver(() => this.#mark(island)) this.#observer.observe(island, { childList: true, characterData: true, subtree: true }) island.addEventListener('click', this.#onClick) this.#schedule() } disconnectedCallback() { window.clearTimeout(this.#timer) this.#timer = 0 this.#observer?.disconnect() this.#observer = null this.#island()?.removeEventListener('click', this.#onClick) } // A morph that moves this element keeps its timer and listeners. connectedMoveCallback() {} // The stream's own sends are synthetic clicks; a trusted click on any // action button in the island is the visitor's, and ends the stream. #onClick = (event) => { if (!event.isTrusted) return if (!(event.target instanceof Element) || event.target.closest('[live\\:click]') === null) return this.#remaining = 0 window.clearTimeout(this.#timer) this.#timer = 0 } #island() { return this.closest('[data-suprnova-live-document-key]') } #mark(island) { for (const status of island.querySelectorAll(STATUS)) { if (status.textContent !== CURRENT) status.textContent = CURRENT } } #schedule() { const action = this.getAttribute('action') if (this.#timer || !this.isConnected || action === null || this.#remaining === 0) return const every = Number.parseInt(this.getAttribute('every') ?? '10000', 10) || 10000 this.#timer = window.setTimeout(() => { this.#timer = 0 if (!document.hidden) { const button = this.#island()?.querySelector(`[live\\:click="${CSS.escape(action)}"]`) if (button instanceof HTMLButtonElement && !button.disabled) { button.click() this.#remaining -= 1 } } this.#schedule() }, every) } } if (!customElements.get('demo-stream')) customElements.define('demo-stream', DemoStream)