You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
import { Controller } from "@hotwired/stimulus"; |
|
|
|
export default class extends Controller { |
|
static values = { coordinate: String } |
|
static targets = ["list", "loading"] |
|
|
|
connect() { |
|
this._liveRoot = this.element.closest( |
|
'[data-controller~="live"]' |
|
); |
|
|
|
const hubUrl = window.MercureHubUrl || document.querySelector('meta[name="mercure-hub"]')?.content; |
|
if (!hubUrl) return; |
|
|
|
const url = new URL(hubUrl); |
|
url.searchParams.append('topic', `/comments/${this.coordinateValue}`); |
|
|
|
this.es = new EventSource(url.toString()); |
|
this.es.onmessage = (event) => this._pushToLive(event.data); |
|
} |
|
|
|
disconnect() { |
|
if (this.es) try { this.es.close(); } catch {} |
|
} |
|
|
|
_pushToLive(jsonString) { |
|
if (!this._liveRoot) return; |
|
// Find the hidden input bound to the LiveProp |
|
const input = this._liveRoot.querySelector('input[type="hidden"][data-model="payloadJson"]'); |
|
if (!input) return; |
|
|
|
// Set value and dispatch an 'input' event so Live updates & re-renders |
|
input.value = jsonString; |
|
input.dispatchEvent(new Event('change', { bubbles: true })); |
|
} |
|
}
|
|
|