Skip to content

Commit

Permalink
loading-states: Consider request path in undo callback.
Browse files Browse the repository at this point in the history
When a loading states path filter exists, respect it when deciding
whether to process the associated undo callback.
  • Loading branch information
rekado committed Feb 9, 2024
1 parent 9388294 commit ce18b3c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/ext/loading-states.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
return htmx.closest(target, '[data-loading-states]') || document.body
}

function mayProcessUndoCallback(target, callback) {
if (document.body.contains(target)) {
function mayProcessUndoCallback(target, requestPath, callback) {
if (document.body.contains(target) && mayProcessLoadingStateByPath(target, requestPath)) {
callback()
}
}
Expand All @@ -28,18 +28,18 @@
const timeout = setTimeout(function () {
doCallback()

loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, undoCallback)
loadingStatesUndoQueue.push(function (requestPath) {
mayProcessUndoCallback(targetElt, requestPath, undoCallback)
})
}, delayInMilliseconds)

loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, function () { clearTimeout(timeout) })
loadingStatesUndoQueue.push(function (requestPath) {
mayProcessUndoCallback(targetElt, requestPath, function () { clearTimeout(timeout) })
})
} else {
doCallback()
loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, undoCallback)
loadingStatesUndoQueue.push(function (requestPath) {
mayProcessUndoCallback(targetElt, requestPath, undoCallback)
})
}
}
Expand Down Expand Up @@ -174,9 +174,9 @@
}

if (name === 'htmx:beforeOnLoad') {
while (loadingStatesUndoQueue.length > 0) {
loadingStatesUndoQueue.shift()()
}
loadingStatesUndoQueue = loadingStatesUndoQueue.filter(function (item) {
return (!item(evt.detail.pathInfo.requestPath));
});
}
},
})
Expand Down

0 comments on commit ce18b3c

Please sign in to comment.