|
| 1 | +const pendingBatches = new Map() |
| 2 | +const activeBatches = new Map() |
| 3 | + |
| 4 | +class SolidObjectsBatchRefreshElement extends HTMLElement { |
| 5 | + connectedCallback() { |
| 6 | + if (this.dataset.started === "true") return |
| 7 | + |
| 8 | + this.dataset.started = "true" |
| 9 | + this.enqueue() |
| 10 | + } |
| 11 | + |
| 12 | + // Several observables changing in one commit produce several notifications |
| 13 | + // for the same batch and revision. Merging them in a microtask turns those |
| 14 | + // into a single request. |
| 15 | + enqueue() { |
| 16 | + const batch = this.dataset.batch |
| 17 | + const revision = this.dataset.revision |
| 18 | + const source = this.dataset.source |
| 19 | + if (!batch || !revision || !source) return this.remove() |
| 20 | + |
| 21 | + const key = `${batch}:${revision}` |
| 22 | + const pending = pendingBatches.get(key) |
| 23 | + if (pending) { |
| 24 | + pending.sources.add(source) |
| 25 | + this.remove() |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + const merged = { sources: new Set([ source ]) } |
| 30 | + pendingBatches.set(key, merged) |
| 31 | + queueMicrotask(() => { |
| 32 | + pendingBatches.delete(key) |
| 33 | + requestBatch(batch, revision, merged.sources) |
| 34 | + }) |
| 35 | + this.remove() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function requestBatch(batch, revision, sources) { |
| 40 | + const previous = activeBatches.get(batch) |
| 41 | + previous?.abort() |
| 42 | + |
| 43 | + const controller = new AbortController() |
| 44 | + activeBatches.set(batch, controller) |
| 45 | + |
| 46 | + try { |
| 47 | + const url = mergedUrl(sources) |
| 48 | + if (!url) return |
| 49 | + |
| 50 | + const response = await fetch(url, { |
| 51 | + credentials: "same-origin", |
| 52 | + headers: { Accept: "application/json" }, |
| 53 | + redirect: "error", |
| 54 | + signal: controller.signal |
| 55 | + }) |
| 56 | + if (!response.ok) return dispatchBatchError(batch, `http_${response.status}`) |
| 57 | + |
| 58 | + const body = await response.json() |
| 59 | + if (!Array.isArray(body?.frames)) { |
| 60 | + return dispatchBatchError(batch, "invalid_response") |
| 61 | + } |
| 62 | + |
| 63 | + body.frames.forEach(applyFrame) |
| 64 | + } catch (error) { |
| 65 | + if (error.name !== "AbortError") dispatchBatchError(batch, "request_failed") |
| 66 | + } finally { |
| 67 | + if (activeBatches.get(batch) === controller) activeBatches.delete(batch) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Every notification for one batch and revision carries the same endpoint and |
| 72 | +// differs only by which components changed, so the union of their tokens is the |
| 73 | +// complete set to render. |
| 74 | +function mergedUrl(sources) { |
| 75 | + const urls = [ ...sources ].map((source) => new URL(source, window.location.href)) |
| 76 | + const first = urls[0] |
| 77 | + if (!first || first.origin !== window.location.origin) return |
| 78 | + |
| 79 | + const tokens = new Set() |
| 80 | + urls.forEach((url) => { |
| 81 | + url.searchParams.getAll("tokens[]").forEach((token) => tokens.add(token)) |
| 82 | + }) |
| 83 | + first.searchParams.delete("tokens[]") |
| 84 | + tokens.forEach((token) => first.searchParams.append("tokens[]", token)) |
| 85 | + return first |
| 86 | +} |
| 87 | + |
| 88 | +function applyFrame(frame) { |
| 89 | + const target = document.getElementById(frame?.target) |
| 90 | + if (!target || !frame.html) return |
| 91 | + if (!newerRevision(frame.revision, target.dataset.solidObjectsRevision)) return |
| 92 | + |
| 93 | + const parsed = new DOMParser().parseFromString(frame.html, "text/html") |
| 94 | + const replacement = parsed.getElementById(frame.target) |
| 95 | + if (!replacement) return |
| 96 | + |
| 97 | + const stream = document.createElement("turbo-stream") |
| 98 | + stream.setAttribute("action", "replace") |
| 99 | + if (frame.refresh_method === "morph") stream.setAttribute("method", "morph") |
| 100 | + stream.setAttribute("target", frame.target) |
| 101 | + |
| 102 | + const template = document.createElement("template") |
| 103 | + template.content.append(document.importNode(replacement, true)) |
| 104 | + stream.append(template) |
| 105 | + document.documentElement.append(stream) |
| 106 | +} |
| 107 | + |
| 108 | +function newerRevision(candidate, current) { |
| 109 | + const candidateRevision = parseRevision(candidate) |
| 110 | + const currentRevision = parseRevision(current) |
| 111 | + if (!candidateRevision || !currentRevision) return false |
| 112 | + |
| 113 | + return candidateRevision[0] > currentRevision[0] || |
| 114 | + (candidateRevision[0] === currentRevision[0] && |
| 115 | + candidateRevision[1] > currentRevision[1]) |
| 116 | +} |
| 117 | + |
| 118 | +function parseRevision(revision) { |
| 119 | + if (!revision) return |
| 120 | + |
| 121 | + const values = String(revision).split(":").map(Number) |
| 122 | + if ( |
| 123 | + values.length !== 2 || |
| 124 | + values.some((value) => !Number.isSafeInteger(value) || value < 0) |
| 125 | + ) return |
| 126 | + |
| 127 | + return values |
| 128 | +} |
| 129 | + |
| 130 | +function dispatchBatchError(batch, reason) { |
| 131 | + document.dispatchEvent( |
| 132 | + new CustomEvent("solid-objects:batch-refresh-error", { |
| 133 | + bubbles: true, |
| 134 | + detail: { batch, reason } |
| 135 | + }) |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +if (!customElements.get("solid-objects-batch-refresh")) { |
| 140 | + customElements.define( |
| 141 | + "solid-objects-batch-refresh", |
| 142 | + SolidObjectsBatchRefreshElement |
| 143 | + ) |
| 144 | +} |
0 commit comments