1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function(){
|
9 |
|
10 |
|
11 | var api;
|
12 |
|
13 | htmx.defineExtension("sse", {
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | init: function(apiRef) {
|
22 |
|
23 | api = apiRef;
|
24 |
|
25 |
|
26 | if (htmx.createEventSource == undefined) {
|
27 | htmx.createEventSource = createEventSource;
|
28 | }
|
29 | },
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | onEvent: function(name, evt) {
|
39 |
|
40 | switch (name) {
|
41 |
|
42 |
|
43 | case "htmx:beforeCleanupElement":
|
44 | var internalData = api.getInternalData(evt.target)
|
45 | if (internalData.sseEventSource) {
|
46 | internalData.sseEventSource.close();
|
47 | }
|
48 | return;
|
49 |
|
50 |
|
51 | case "htmx:afterProcessNode":
|
52 | createEventSourceOnElement(evt.target);
|
53 | }
|
54 | }
|
55 | });
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function createEventSource(url) {
|
70 | return new EventSource(url, {withCredentials:true});
|
71 | }
|
72 |
|
73 | function splitOnWhitespace(trigger) {
|
74 | return trigger.trim().split(/\s+/);
|
75 | }
|
76 |
|
77 | function getLegacySSEURL(elt) {
|
78 | var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
|
79 | if (legacySSEValue) {
|
80 | var values = splitOnWhitespace(legacySSEValue);
|
81 | for (var i = 0; i < values.length; i++) {
|
82 | var value = values[i].split(/:(.+)/);
|
83 | if (value[0] === "connect") {
|
84 | return value[1];
|
85 | }
|
86 | }
|
87 | }
|
88 | }
|
89 |
|
90 | function getLegacySSESwaps(elt) {
|
91 | var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
|
92 | var returnArr = [];
|
93 | if (legacySSEValue) {
|
94 | var values = splitOnWhitespace(legacySSEValue);
|
95 | for (var i = 0; i < values.length; i++) {
|
96 | var value = values[i].split(/:(.+)/);
|
97 | if (value[0] === "swap") {
|
98 | returnArr.push(value[1]);
|
99 | }
|
100 | }
|
101 | }
|
102 | return returnArr;
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function createEventSourceOnElement(elt, retryCount) {
|
114 |
|
115 | if (elt == null) {
|
116 | return null;
|
117 | }
|
118 |
|
119 | var internalData = api.getInternalData(elt);
|
120 |
|
121 |
|
122 | var sseURL = api.getAttributeValue(elt, "sse-connect");
|
123 |
|
124 |
|
125 | if (sseURL == undefined) {
|
126 | var legacyURL = getLegacySSEURL(elt)
|
127 | if (legacyURL) {
|
128 | sseURL = legacyURL;
|
129 | } else {
|
130 | return null;
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 | var source = htmx.createEventSource(sseURL);
|
136 | internalData.sseEventSource = source;
|
137 |
|
138 |
|
139 | source.onerror = function (err) {
|
140 |
|
141 |
|
142 | api.triggerErrorEvent(elt, "htmx:sseError", {error:err, source:source});
|
143 |
|
144 |
|
145 | if (maybeCloseSSESource(elt)) {
|
146 | return;
|
147 | }
|
148 |
|
149 |
|
150 | if (source.readyState === EventSource.CLOSED) {
|
151 | retryCount = retryCount || 0;
|
152 | var timeout = Math.random() * (2 ^ retryCount) * 500;
|
153 | window.setTimeout(function() {
|
154 | createEventSourceOnElement(elt, Math.min(7, retryCount+1));
|
155 | }, timeout);
|
156 | }
|
157 | };
|
158 |
|
159 |
|
160 | queryAttributeOnThisOrChildren(elt, "sse-swap").forEach(function(child) {
|
161 |
|
162 | var sseSwapAttr = api.getAttributeValue(child, "sse-swap");
|
163 | if (sseSwapAttr) {
|
164 | var sseEventNames = sseSwapAttr.split(",");
|
165 | } else {
|
166 | var sseEventNames = getLegacySSESwaps(child);
|
167 | }
|
168 |
|
169 | for (var i = 0 ; i < sseEventNames.length ; i++) {
|
170 | var sseEventName = sseEventNames[i].trim();
|
171 | var listener = function(event) {
|
172 |
|
173 |
|
174 | if (maybeCloseSSESource(elt)) {
|
175 | source.removeEventListener(sseEventName, listener);
|
176 | return;
|
177 | }
|
178 |
|
179 |
|
180 | swap(child, event.data);
|
181 | api.triggerEvent(elt, "htmx:sseMessage", event);
|
182 | };
|
183 |
|
184 |
|
185 | api.getInternalData(elt).sseEventListener = listener;
|
186 | source.addEventListener(sseEventName, listener);
|
187 | }
|
188 | });
|
189 |
|
190 |
|
191 | queryAttributeOnThisOrChildren(elt, "hx-trigger").forEach(function(child) {
|
192 |
|
193 | var sseEventName = api.getAttributeValue(child, "hx-trigger");
|
194 | if (sseEventName == null) {
|
195 | return;
|
196 | }
|
197 |
|
198 |
|
199 | if (sseEventName.slice(0, 4) != "sse:") {
|
200 | return;
|
201 | }
|
202 |
|
203 | var listener = function(event) {
|
204 |
|
205 |
|
206 | if (maybeCloseSSESource(elt)) {
|
207 | source.removeEventListener(sseEventName, listener);
|
208 | return;
|
209 | }
|
210 |
|
211 |
|
212 | htmx.trigger(child, sseEventName, event);
|
213 | htmx.trigger(child, "htmx:sseMessage", event);
|
214 | }
|
215 |
|
216 |
|
217 | api.getInternalData(elt).sseEventListener = listener;
|
218 | source.addEventListener(sseEventName.slice(4), listener);
|
219 | });
|
220 | }
|
221 |
|
222 | |
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | function maybeCloseSSESource(elt) {
|
230 | if (!api.bodyContains(elt)) {
|
231 | var source = api.getInternalData(elt).sseEventSource;
|
232 | if (source != undefined) {
|
233 | source.close();
|
234 |
|
235 | return true;
|
236 | }
|
237 | }
|
238 | return false;
|
239 | }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function queryAttributeOnThisOrChildren(elt, attributeName) {
|
248 |
|
249 | var result = [];
|
250 |
|
251 |
|
252 | if (api.hasAttribute(elt, attributeName) || api.hasAttribute(elt, "hx-sse")) {
|
253 | result.push(elt);
|
254 | }
|
255 |
|
256 |
|
257 | elt.querySelectorAll("[" + attributeName + "], [data-" + attributeName + "], [hx-sse], [data-hx-sse]").forEach(function(node) {
|
258 | result.push(node);
|
259 | });
|
260 |
|
261 | return result;
|
262 | }
|
263 |
|
264 | |
265 |
|
266 |
|
267 |
|
268 | function swap(elt, content) {
|
269 |
|
270 | api.withExtensions(elt, function(extension) {
|
271 | content = extension.transformResponse(content, null, elt);
|
272 | });
|
273 |
|
274 | var swapSpec = api.getSwapSpecification(elt);
|
275 | var target = api.getTarget(elt);
|
276 | var settleInfo = api.makeSettleInfo(elt);
|
277 |
|
278 | api.selectAndSwap(swapSpec.swapStyle, target, elt, content, settleInfo);
|
279 |
|
280 | settleInfo.elts.forEach(function (elt) {
|
281 | if (elt.classList) {
|
282 | elt.classList.add(htmx.config.settlingClass);
|
283 | }
|
284 | api.triggerEvent(elt, 'htmx:beforeSettle');
|
285 | });
|
286 |
|
287 |
|
288 | if (swapSpec.settleDelay > 0) {
|
289 | setTimeout(doSettle(settleInfo), swapSpec.settleDelay);
|
290 | } else {
|
291 | doSettle(settleInfo)();
|
292 | }
|
293 | }
|
294 |
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | function doSettle(settleInfo) {
|
303 |
|
304 | return function() {
|
305 | settleInfo.tasks.forEach(function (task) {
|
306 | task.call();
|
307 | });
|
308 |
|
309 | settleInfo.elts.forEach(function (elt) {
|
310 | if (elt.classList) {
|
311 | elt.classList.remove(htmx.config.settlingClass);
|
312 | }
|
313 | api.triggerEvent(elt, 'htmx:afterSettle');
|
314 | });
|
315 | }
|
316 | }
|
317 |
|
318 | })(); |
\ | No newline at end of file |