@chg20 been thinking about the extension mechanism which a great idea. It seems to have some issues with the ajaxrequest call though.
I have written some psuedo code to start the discussion. I hope to start a dialog
// psuedo code approach
function revisedajax() {
// figure out what the "make 80% happy case is"
// or the "just works out of the box" case
var ks; // kitchensick
// implement the 80% here, for example this is the "normal" case now
// NORMALLY, we change verb to either "GET", "POST"
xhrverb = "GET" or "POST" depending on verb
// so add both to kitchensink
ks.verb = verb;
ks.xhrverb = xhrverb;
// NORMALLY, we change the url if GET
xhrurl = url + urlencodedparameters
// so add the original url and xhrurl
ks.url = url
ks.xhrurl = xhrurl
// NORMALLY, we add parameters to the body if POST
ks.xhrbody = create body with parameters if POST-like
ks.parameters = parameters
// NORMALLY, we set the mimetype to texthtml
ks.xhrMimeType = text/html
// NORMALLY, we set the header content type
// like urlencode
ks.xhrHeaders = set content type to ...
// give extension writer change to deviate from NORM
// the writer will know that changing variables that
// have xhr prefix will change
xhrchangeup( ks )
var xhr =new XMLHttpRequest();
// set xhr events (non changeable)
setheaders( xhr, ks.xhrHeaders);
if (ks.xhrMimeType) xhr.overrideMimeType( ks.xhrMimeType );
xhr.open( ks.xhrVerb, ks.xhrUrl, true);
xhr.send( ks.xhrBody);
}
define extension xhrchangeup( "json-enc", ks ) {
ks.xhrHeaders = change content type to ...
ks.xhrMimeType = ...
ks.xhrbody = encode parameters
// use original url
ks.xhrurl = ks.url
}
// maybe
define extension xhrchangeup( "actual-verb", ks ) {
ks.xhrHeaders = change content type to ...
// use the actual verb
ks.xhrverb = ks.verb
}
@chg20 I wrote a sample sse example with a flask server. I make the following change to:
function processSSETrigger(elt, verb, path, sseEventName) {
var sseSourceElt = getClosestMatch(elt, function (parent) {
return getInternalData(parent).sseEventSource != null;
});
if (sseSourceElt) {
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
var sseListener = function (event) {
if (!maybeCloseSSESource(sseSourceElt)) {
if (bodyContains(elt)) {
getInternalData(elt).sseEvent = event;
issueAjaxRequest(elt, verb, path);
} else {
sseEventSource.removeEventListener(sseEventName, sseListener);
}
}
noticed I assigned the event to internal data of the element, this may not be the best way to do it.
Can you make some recommendations?
795c787,788
< var sseListener = function () {
---
>
> var sseListener = function (event) {
797a791
> getInternalData(elt).sseEvent = event;
<ol hx-sse="swap on eventName" hx-swap="beforeend">Loading list...</ol>
<body id="thebody" hx-sse="connect /news_updates">
<h1>Receiving Messages:</h1>
<div id="sselist" hx-trigger="sse:new_news" hx-ext="sse-body"
hx-post="/news" hx-target="#nid" hx-swap="beforeend">
<ul id="nid">
</ul>
</div>
</body>
<body id="thebody" hx-sse="connect /news_updates">
<h1>Receiving Messages:</h1>
<ol hx-trigger="sse:new_news" hx-get="/news" hx-swap="beforeend">
loading list ...
</ol>
</body>