forked from Renerick/htmx-signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hx-signalr.js
380 lines (320 loc) · 10.8 KB
/
hx-signalr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
SignalR Extension
============================
This extension adds support for SignalR to htmx.
Based on WebSockets extension (https://github.com/bigskysoftware/htmx/blob/master/src/ext/ws.js)
and SSE extension (https://github.com/bigskysoftware/htmx/blob/master/src/ext/sse.js)
by bigskysoftware.
*/
(function () {
/** @type {import("../htmx").HtmxInternalApi} */
var api;
var signalRConnect = "signalr-connect";
var signalRSubscribe = "signalr-subscribe";
var signalRSend = "signalr-send";
htmx.defineExtension("signalr", {
/**
* init is called once, when this extension is first registered.
* @param {import("../htmx").HtmxInternalApi} apiRef
*/
init: function (apiRef) {
// Store reference to internal API
api = apiRef;
// Default function for creating new EventSource objects
if (htmx.createHubConnection == undefined) {
htmx.createHubConnection = createHubConnection;
}
},
/**
* onEvent handles all events passed to this extension.
*
* @param {string} name
* @param {Event} evt
*/
onEvent: function (name, evt) {
switch (name) {
// Try to remove hub connection when elements are removed
case "htmx:beforeCleanupElement":
var internalData = api.getInternalData(evt.target)
if (internalData.HubConnection != undefined) {
internalData.HubConnection.stop();
}
return;
// Try to create hub connections when elements are processed
case "htmx:beforeProcessNode":
var parent = evt.target;
forEach(queryAttributeOnThisOrChildren(parent, signalRConnect), function (child) {
ensureHubConnection(child)
});
forEach(queryAttributeOnThisOrChildren(parent, signalRSubscribe), function (child) {
ensureSubscription(child)
});
forEach(queryAttributeOnThisOrChildren(parent, signalRSend), function (child) {
ensureSending(child)
});
}
}
});
/**
* ensureHubConnection creates a new SignalR Hub Connection on the designated element, using
* the element's "signalr-connect" attribute.
* @param {HTMLElement} elt
* @returns
*/
function ensureHubConnection(elt) {
// If the element containing the connection no longer exists, then
// do not connect/reconnect the Hub.
if (!api.bodyContains(elt)) {
return;
}
if (!signalR) {
logError('SignalR object not found. Make sure to include SignalR script in the page scripts before this extension.');
return;
}
// Get the source straight from the element's value
var signalrHubUrl = api.getAttributeValue(elt, signalRConnect)
// Create a new HubConnection and event handlers
/** @type {HubConnection} */
var hubConnection = htmx.createHubConnection(signalrHubUrl);
hubConnection.start();
// Put the HubConnection into the HTML Element's custom data.
api.getInternalData(elt).HubConnection = hubConnection;
}
/**
* ensureSending attaches event listeners to elements with "signalr-send" attribute.
* @param {HTMLElement} elt
* @returns
*/
function ensureSending(elt) {
// If the element containing the connection no longer exists, then
// do not connect/reconnect the Hub.
if (!api.bodyContains(elt)) {
return;
}
if (!signalR) {
logError('SignalR object not found. Make sure to include SignalR script in the page scripts before this extension.');
return;
}
var hubElement = findParentWithHubConnection(elt);
if (!hubElement) {
return;
}
processHubConnectionSend(hubElement, elt);
}
/**
* ensureMethodHandler creates a listener that handles invocations of a method defined on the element
* by "signalr-method" attribute.
* @param {HTMLElement} elt
* @returns
*/
function ensureSubscription(elt) {
// If the element containing the connection no longer exists, then
// do not subscribe
if (!api.bodyContains(elt)) {
return;
}
if (!signalR) {
logError('SignalR object not found. Make sure to include SignalR script in the page scripts before this extension.');
return;
}
var hubElement = findParentWithHubConnection(elt);
if (!hubElement) {
return;
}
var hubConnection = api.getInternalData(hubElement).HubConnection;
var signalrSubscribeAttribute = api.getAttributeValue(elt, signalRSubscribe);
var signalrMethodNames = signalrSubscribeAttribute.split(",");
for (let i = 0; i < signalrMethodNames.length; i++) {
var method = signalrMethodNames[i].trim();
hubConnection.on(method, function handler(message) {
if (maybeCloseHubConnectionSource(hubElement)) {
hubConnection.off(method, handler)
return;
}
if (maybeUnsubscribe(hubElement, method, elt, handler)) {
return;
}
var target = api.getTarget(elt);
var settleInfo = api.makeSettleInfo(elt);
var messageSpec = {
message: message,
method: method,
target: target,
};
if (!api.triggerEvent(elt, 'htmx:signalr:message', messageSpec)) {
return;
}
// Other parts of htmx expect to have response object as a string
// So, we serialize it
if (typeof (messageSpec.message) === "object") {
messageSpec.message = JSON.stringify(messageSpec.message);
}
api.withExtensions(elt, function (extension) {
messageSpec.message = extension.transformResponse(messageSpec.message, null, elt);
});
var swapSpec = api.getSwapSpecification(elt);
api.selectAndSwap(swapSpec.swapStyle, messageSpec.target, elt, messageSpec.message, settleInfo);
settleInfo.elts.forEach(function (elt) {
if (elt.classList) {
elt.classList.add(htmx.config.settlingClass);
}
api.triggerEvent(elt, 'htmx:beforeSettle');
});
// Handle settle tasks (with delay if requested)
if (swapSpec.settleDelay > 0) {
setTimeout(doSettle(settleInfo), swapSpec.settleDelay);
} else {
doSettle(settleInfo)();
}
});
}
}
/**
* processHubConnectionSend adds event listeners to the <form> element so that
* messages can be sent to the HubConnection server when the form is submitted.
* @param {HTMLElement} hubElt
* @param {HTMLElement} sendElt
*/
function processHubConnectionSend(hubElt, sendElt) {
var nodeData = api.getInternalData(sendElt);
var triggerSpecs = api.getTriggerSpecs(sendElt);
triggerSpecs.forEach(function (ts) {
api.addTriggerHandler(sendElt, ts, nodeData, function (elt, evt) {
var HubConnection = api.getInternalData(hubElt).HubConnection;
var method = api.getAttributeValue(sendElt, signalRSend);
var headers = api.getHeaders(sendElt, hubElt);
var results = api.getInputValues(sendElt, 'post');
var errors = results.errors;
var rawParameters = results.values;
var expressionVars = api.getExpressionVars(sendElt);
var allParameters = api.mergeObjects(rawParameters, expressionVars);
var filteredParameters = api.filterValues(allParameters, sendElt);
filteredParameters['HEADERS'] = headers;
if (errors && errors.length > 0) {
api.triggerEvent(sendElt, 'htmx:validation:halted', errors);
return;
}
if (!api.triggerEvent(sendElt, 'htmx:signalr:beforeSend', { method: method, headers: headers, allParameters: allParameters, filteredParameters: filteredParameters })) {
return;
};
HubConnection.send(method, filteredParameters);
if (api.shouldCancel(evt, sendElt)) {
evt.preventDefault();
}
api.triggerEvent(sendElt, 'htmx:signalr:afterSend', { method: method, message: filteredParameters });
});
})
}
/**
* maybeCloseHubConnectionSource checks to the if the element that created the HubConnection
* still exists in the DOM. If NOT, then the HubConnection is closed and this function
* returns TRUE. If the element DOES EXIST, then no action is taken, and this function
* returns FALSE.
*
* @param {*} elt
* @returns
*/
function maybeCloseHubConnectionSource(elt) {
if (!api.bodyContains(elt)) {
api.getInternalData(elt).HubConnection.stop();
return true;
}
return false;
}
/**
* maybeUnsubscribe checks if the element that created the subscription to method
* still has matching subscription attribute. If NOT, then the subscription is removed and this function
* returns TRUE. If the element DOES EXIST, then no action is taken, and this function
* returns FALSE.
*
* @param {*} elt
* @returns
*/
function maybeUnsubscribe(hubElement, subscription, elt, handler) {
if (!api.bodyContains(elt)) {
api.getInternalData(hubElement).HubConnection.off(subscription, handler);
return true;
}
if (api.getAttributeValue(elt, signalRSubscribe).split(",").indexOf(subscription) == -1) {
api.getInternalData(hubElement).HubConnection.off(subscription, handler);
return true;
}
return false;
}
/**
* createHubConnection is the default method for creating new HubConnection objects.
* it is hoisted into htmx.createHubConnection to be overridden by the user, if needed.
*
* @param {string} url
* @returns HubConnection
*/
function createHubConnection(url) {
return new signalR.HubConnectionBuilder()
.withUrl(url)
.withAutomaticReconnect()
.build()
}
/**
* queryAttributeOnThisOrChildren returns all nodes that contain the requested attributeName, INCLUDING THE PROVIDED ROOT ELEMENT.
*
* @param {HTMLElement} elt
* @param {string} attributeName
*/
function queryAttributeOnThisOrChildren(elt, attributeName) {
var result = []
// If the parent element also contains the requested attribute, then add it to the results too.
if (api.hasAttribute(elt, attributeName)) {
result.push(elt);
}
// Search all child nodes that match the requested attribute
elt.querySelectorAll("[" + attributeName + "], [data-" + attributeName + "]").forEach(function (node) {
result.push(node)
})
return result
}
/**
* findParentWithHubConnection returns all nodes that contain the requested attributeName, INCLUDING THE PROVIDED ROOT ELEMENT.
*
* @param {HTMLElement} elt
*/
function findParentWithHubConnection(elt) {
var match = api.getClosestMatch(elt, hasHubConnection);
return match;
}
function hasHubConnection(node) {
var internalData = api.getInternalData(node);
return internalData.HubConnection != null;
}
/**
* @template T
* @param {T[]} arr
* @param {(T) => void} func
*/
function forEach(arr, func) {
if (arr) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
}
/**
* doSettle mirrors much of the functionality in htmx that
* settles elements after their content has been swapped.
* TODO: this should be published by htmx, and not duplicated here
* @param {import("../htmx").HtmxSettleInfo} settleInfo
* @returns () => void
*/
function doSettle(settleInfo) {
return function () {
settleInfo.tasks.forEach(function (task) {
task.call();
});
settleInfo.elts.forEach(function (elt) {
if (elt.classList) {
elt.classList.remove(htmx.config.settlingClass);
}
api.triggerEvent(elt, 'htmx:afterSettle');
});
}
}
})();