summaryrefslogtreecommitdiffstatshomepage
path: root/src/ext/sse.js
diff options
context:
space:
mode:
authorBen Pate <ben@pate.org>2021-10-27 20:20:49 -0600
committerBen Pate <ben@pate.org>2021-10-27 20:20:49 -0600
commite3e83df5ad301656e426f60c299ea7d2d34a8b2e (patch)
treec544ce4e289521a1fe029495e8fb8fe87e2aeac4 /src/ext/sse.js
parentee26035e065ba583811948c03f8a8c6c14319aaf (diff)
downloadhtmx-e3e83df5ad301656e426f60c299ea7d2d34a8b2e.tar.gz
htmx-e3e83df5ad301656e426f60c299ea7d2d34a8b2e.zip
Implementing new API object
"api" reference moved from function calls into the new "init" constructor. - just checking in progress... no tests tested. I have no idea if this code works or not yet. (so assume it's still a dumpster fire)
Diffstat (limited to 'src/ext/sse.js')
-rw-r--r--src/ext/sse.js40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/ext/sse.js b/src/ext/sse.js
index 1489bbe5..a9bed457 100644
--- a/src/ext/sse.js
+++ b/src/ext/sse.js
@@ -7,17 +7,30 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
(function(){
+ /** @type {import("../htmx").HtmxInternalApi} */
+ var api
+
htmx.defineExtension("sse", {
/**
+ * Init saves the provided reference to the internal HTMX API.
+ *
+ * @param {import("../htmx").HtmxInternalApi} api
+ * @param {HTMLElement} elt
+ * @returns void
+ */
+ init: function(apiRef, _elt) {
+ api = apiRef
+ },
+
+ /**
* onEvent handles all events passed to this extension.
*
* @param {string} name
* @param {Event} evt
- * @param {import("../htmx").HtmxExtensionApi} api
* @returns void
*/
- onEvent: function(name, evt, api) {
+ onEvent: function(name, evt) {
switch (name) {
@@ -130,11 +143,10 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
* maybeCloseSSESource confirms that the parent element still exists.
* If not, then any associated SSE source is closed and the function returns true.
*
- * @param {import("../htmx").HtmxExtensionApi} api
* @param {HTMLElement} elt
* @returns boolean
*/
- function maybeCloseSSESource(api, elt) {
+ function maybeCloseSSESource(elt) {
if (!api.bodyContains(elt)) {
var source = api.getInternalData("sseEventSource")
if (source != undefined) {
@@ -152,7 +164,7 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
* @param {HTMLElement} elt
* @param {string} attributeName
*/
- function queryAttributeOnThisOrChildren(api, elt, attributeName) {
+ function queryAttributeOnThisOrChildren(elt, attributeName) {
var result = []
@@ -168,4 +180,22 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
return result
}
+
+ /**
+ * @param {HTMLElement} elt
+ * @param {string} content
+ */
+ function swap(elt, content) {
+ api.withExtensions(elt, function(extension){
+ content = extension.transformResponse(content, null, elt);
+ });
+
+ var swapSpec = api.getSwapSpecification(elt)
+ var target = api.getTarget(elt)
+ var settleInfo = api.makeSettleInfo(elt);
+
+ selectAndSwap(swapSpec.swapStyle, elt, target, content, settleInfo)
+ settleImmediately(settleInfo.tasks)
+ }
+ }
})(); \ No newline at end of file