Skip to content

fix: honor the filter after the interval in every triggers - #4133

Open
giaBaoJS wants to merge 1 commit into
bigskysoftware:four-devfrom
giaBaoJS:fix/every-trigger-filter
Open

giaBaoJS wants to merge 1 commit into
bigskysoftware:four-devfrom
giaBaoJS:fix/every-trigger-filter

Conversation

@giaBaoJS

Copy link
Copy Markdown

Description

The hx-trigger reference and the polling pattern both show a polling filter written after the interval:

<div hx-get="/status" hx-trigger="every 2s [document.visibilityState === 'visible']">

On four-dev the filter is never evaluated. __parseTriggerSpecs takes every as the event name and passes 2s [document...] to HCON as modifiers, so the filter ends up as a stray modifier key and the element polls unconditionally. Without the space (every 2s[cond], the form the old v2 test used) it is worse: the interval key becomes 2s[cond], parseInterval returns undefined, and setInterval fires every few milliseconds.

The fix moves a filter that follows the every interval onto the event name before parsing, so every 2s [cond] and every 2s[cond] both parse to {name: 'every[cond]', '2s': true} and the filter runs through the same path as click[cond]. every[cond] 2s and plain every 2s parse the same as before.

Corresponding issue: none, the filter syntax is documented in the hx-trigger every section

Testing

  • Unit test in __parseTriggerSpecs.js for both spellings.
  • Test in hx-trigger.js that polls with a false filter for 100ms, expects no requests, then makes the filter true and expects a request. Without the change it fails with expected 31 to equal +0 (31 requests in 100ms from two 20ms pollers).
  • npm test passes in Chromium (1782 passed, 4 skipped).

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (four-dev, since this is htmx 4 source)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

hx-trigger="every 1s [cond]" is the documented way to filter polling,
but the parser read [cond] as a modifier, so the poll ran without the
filter. Written as "every 1s[cond]", the interval failed to parse and
the element polled every few milliseconds. Move the filter onto the
event name so it is evaluated like any other trigger filter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant