|
| 1 | +import getNodeScopedState from '#ehtml/getNodeScopedState.js' |
| 2 | +import evaluatedStringWithParamsFromState from '#ehtml/evaluatedStringWithParamsFromState.js' |
| 3 | +import evaluateActionsOnProgress from '#ehtml/evaluateActionsOnProgress.js' |
| 4 | +import evaluateActionsOnOpenConnection from '#ehtml/evaluateActionsOnOpenConnection.js' |
| 5 | +import evaluateActionsOnCloseConnection from '#ehtml/evaluateActionsOnCloseConnection.js' |
| 6 | + |
| 7 | +export default class ESse extends HTMLTemplateElement { |
| 8 | + constructor() { |
| 9 | + super() |
| 10 | + this.ehtmlActivated = false |
| 11 | + this.eventSourceName = null |
| 12 | + } |
| 13 | + |
| 14 | + connectedCallback() { |
| 15 | + this.addEventListener( |
| 16 | + 'ehtml:activated', |
| 17 | + this.onEHTMLActivated, |
| 18 | + { once: true } |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + disconnectedCallback() { |
| 23 | + if (this.eventSourceName) { |
| 24 | + window.__EHTML_SERVER_EVENT_SOURCES__[eventSourceName].close() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + onEHTMLActivated() { |
| 29 | + if (this.ehtmlActivated) { |
| 30 | + return |
| 31 | + } |
| 32 | + this.ehtmlActivated = true |
| 33 | + this.run() |
| 34 | + } |
| 35 | + |
| 36 | + run() { |
| 37 | + const state = getNodeScopedState(this) |
| 38 | + |
| 39 | + if (!this.hasAttribute('data-src')) { |
| 40 | + throw new Error('e-sse must have "data-src" attribute') |
| 41 | + } |
| 42 | + |
| 43 | + if (!this.hasAttribute('data-source-name')) { |
| 44 | + throw new Error('e-ws must have "data-event-source-name" attribute') |
| 45 | + } |
| 46 | + |
| 47 | + const eventSourceUrl = evaluatedStringWithParamsFromState( |
| 48 | + this.getAttribute('data-event-source-name'), |
| 49 | + state, |
| 50 | + this |
| 51 | + ) |
| 52 | + |
| 53 | + const eventSourceName = evaluatedStringWithParamsFromState( |
| 54 | + this.getAttribute('data-event-source-name'), |
| 55 | + state, |
| 56 | + this |
| 57 | + ) |
| 58 | + |
| 59 | + this.eventSourceName = eventSourceName |
| 60 | + |
| 61 | + const connectionIconSelector = this.getAttribute('data-connection-icon') |
| 62 | + const connectionIcon = connectionIconSelector |
| 63 | + ? document.querySelector(connectionIconSelector) |
| 64 | + : null |
| 65 | + |
| 66 | + if (connectionIcon) { |
| 67 | + connectionIcon.style.display = '' |
| 68 | + } |
| 69 | + |
| 70 | + const eventSource = new EventSource(eventSourceUrl) |
| 71 | + |
| 72 | + // global EHTML storage |
| 73 | + window.__EHTML_SERVER_EVENT_SOURCES__ = |
| 74 | + window.__EHTML_SERVER_EVENT_SOURCES__ || {} |
| 75 | + |
| 76 | + window.__EHTML_SERVER_EVENT_SOURCES__[eventSourceName] = eventSource |
| 77 | + |
| 78 | + eventSource.addEventListener('open', event => { |
| 79 | + if (connectionIcon) { |
| 80 | + connectionIcon.style.display = 'none' |
| 81 | + } |
| 82 | + |
| 83 | + if (this.hasAttribute('data-actions-on-open-connection')) { |
| 84 | + evaluateActionsOnOpenConnection( |
| 85 | + this.getAttribute('data-actions-on-open-connection'), |
| 86 | + event, |
| 87 | + this, |
| 88 | + state |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + // Replace <template is="e-ws"> with its content |
| 93 | + this.parentNode.replaceChild( |
| 94 | + this.content.cloneNode(true), |
| 95 | + this |
| 96 | + ) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +customElements.define('e-ws', EWs, { extends: 'template' }) |
0 commit comments