Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache evaluated trigger specs #1540

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ return (function () {
methodsThatUseUrlParams: ["get"],
selfRequestsOnly: false,
ignoreTitle: false,
scrollIntoViewOnBoost: true
scrollIntoViewOnBoost: true,
triggerSpecsCache: null,
},
parseInterval:parseInterval,
_:internalEval,
Expand Down Expand Up @@ -1252,12 +1253,11 @@ return (function () {

/**
* @param {HTMLElement} elt
* @param {string} explicitTrigger
* @returns {import("./htmx").HtmxTriggerSpecification[]}
*/
function getTriggerSpecs(elt) {
var explicitTrigger = getAttributeValue(elt, 'hx-trigger');
function parseAndCacheTrigger(elt, explicitTrigger) {
var triggerSpecs = [];
if (explicitTrigger) {
var tokens = tokenizeString(explicitTrigger);
do {
consumeUntil(tokens, NOT_WHITESPACE);
Expand Down Expand Up @@ -1337,6 +1337,22 @@ return (function () {
}
consumeUntil(tokens, NOT_WHITESPACE);
} while (tokens[0] === "," && tokens.shift())
if (htmx.config.triggerSpecsCache) {
htmx.config.triggerSpecsCache[explicitTrigger] = triggerSpecs
}
return triggerSpecs
}

/**
* @param {HTMLElement} elt
* @returns {import("./htmx").HtmxTriggerSpecification[]}
*/
function getTriggerSpecs(elt) {
var explicitTrigger = getAttributeValue(elt, 'hx-trigger');
var triggerSpecs = [];
if (explicitTrigger) {
triggerSpecs = (htmx.config.triggerSpecsCache && htmx.config.triggerSpecsCache[explicitTrigger])
|| parseAndCacheTrigger(elt, explicitTrigger)
1cg marked this conversation as resolved.
Show resolved Hide resolved
}

if (triggerSpecs.length > 0) {
Expand Down