Skip to content

Commit

Permalink
allow filters for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
carson committed Jul 6, 2021
1 parent c9936b4 commit 40ed7b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ return (function () {
if (trigger === "every") {
var every = {trigger: 'every'};
consumeUntil(tokens, NOT_WHITESPACE);
every.pollInterval = parseInterval(consumeUntil(tokens, WHITESPACE));
every.pollInterval = parseInterval(consumeUntil(tokens, /[,\[\s]/));
var eventFilter = maybeGenerateConditional(elt, tokens, "event");
if (eventFilter) {
every.eventFilter = eventFilter;
}
triggerSpecs.push(every);
} else if (trigger.indexOf("sse:") === 0) {
triggerSpecs.push({trigger: 'sse', sseEvent: trigger.substr(4)});
Expand Down Expand Up @@ -919,14 +923,16 @@ return (function () {
getInternalData(elt).cancelled = true;
}

function processPolling(elt, verb, path, interval) {
function processPolling(elt, verb, path, spec) {
var nodeData = getInternalData(elt);
nodeData.timeout = setTimeout(function () {
if (bodyContains(elt) && nodeData.cancelled !== true) {
issueAjaxRequest(verb, path, elt);
processPolling(elt, verb, getAttributeValue(elt, "hx-" + verb), interval);
if (!maybeFilterEvent(spec, makeEvent('hx:poll:trigger', {triggerSpec:spec}))) {
issueAjaxRequest(verb, path, elt);
}
processPolling(elt, verb, getAttributeValue(elt, "hx-" + verb), spec);
}
}, interval);
}, spec.pollInterval);
}

function isLocalLink(elt) {
Expand Down Expand Up @@ -1360,7 +1366,7 @@ return (function () {
loadImmediately(elt, verb, path, nodeData, triggerSpec.delay);
} else if (triggerSpec.pollInterval) {
nodeData.polling = true;
processPolling(elt, verb, path, triggerSpec.pollInterval);
processPolling(elt, verb, path, triggerSpec);
} else {
addEventListener(elt, verb, path, nodeData, triggerSpec);
}
Expand Down Expand Up @@ -2131,9 +2137,11 @@ return (function () {
var eltData = getInternalData(elt);
if (eltData.requestInFlight) {
var queueStrategy = 'last';
var eventData = getInternalData(event);
if (eventData && eventData.triggerSpec && eventData.triggerSpec.queue) {
queueStrategy = eventData.triggerSpec.queue;
if (event) {
var eventData = getInternalData(event);
if (eventData && eventData.triggerSpec && eventData.triggerSpec.queue) {
queueStrategy = eventData.triggerSpec.queue;
}
}
if (eltData.queuedRequests == null) {
eltData.queuedRequests = [];
Expand Down
15 changes: 15 additions & 0 deletions test/attributes/hx-trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ describe("hx-trigger attribute", function(){
}
})

it('can filter polling', function(complete){
this.server.respondWith("GET", "/test", "Called!");
window.foo = false;
var div = make('<div hx-get="/test" hx-trigger="every 5ms[foo]">Not Called</div>');
var div2 = make('<div hx-get="/test" hx-trigger="every 5ms">Not Called</div>');
this.server.autoRespond = true;
this.server.autoRespondAfter = 0;
setTimeout(function () {
div.innerHTML.should.equal("Not Called");
div2.innerHTML.should.equal("Called!");
delete window.foo;
complete();
}, 100);
})

it('bad condition issues error', function(){
this.server.respondWith("GET", "/test", "Called!");
var div = make('<div hx-get="/test" hx-trigger="evt[a.b]">Not Called</div>');
Expand Down

0 comments on commit 40ed7b0

Please sign in to comment.