Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(debounceFilter): invoke on maxWait with the latest invoker #4497

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

Merged
merged 5 commits into from
Jan 22, 2025

Conversation

43081j
Copy link
Collaborator

@43081j 43081j commented Jan 16, 2025

Fixes #3665
Closes #4473

A filter works by binding a watch callback and its args every time the watcher fires. This bound function is then held back until your "filter" calls its invoke parameter.

In debounceFilter, we setup a maxWait timer to fire after a certain amount of time. Once this fires, it calls invoke.

The problem here is that the invoke will be the paired cb/args from the point in time where we started the timer.

For example:

const cb = vi.fn();
const someRef = ref(0);

watchWithFilter(
  someRef,
  cb,
  {
    eventFilter: debounceFilter(100, {maxWait: 500})
  }
);

someRef.value = 1; // cb(1, 0) is held back, `maxWait` timer is kicked off _with `invoke = () => cb(1, 0)`_
someRef.value = 2; // cb(2, 0)
// somehow 500ms passes
// maxWait timer now calls `cb(1, 0)` rather than cb(2, 0)

To fix this, i've added lastInvoker, which is the last invoke function we created for this filter. Once the max timer fires, we will clear the regular timer, and call lastInvoker (which a regular timer originally set up).

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 16, 2025
@@ -42,6 +42,33 @@ describe('watchDebounced', () => {
num.value = 5
await vi.advanceTimersByTimeAsync(75)
expect(cb).toHaveBeenCalledTimes(2)
expect(cb).toHaveBeenCalledWith(4, 2, expect.anything())
expect(cb).toHaveBeenCalledWith(5, 4, expect.anything())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as i can tell, this test was already showing the bug but we just rolled with it

second pair of eyes on this test would be helpful

packages/shared/watchDebounced/index.test.ts Outdated Show resolved Hide resolved
Co-authored-by: Robin <37191683+OrbisK@users.noreply.github.com>
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 16, 2025
@antfu antfu added this pull request to the merge queue Jan 22, 2025
Merged via the queue into vueuse:main with commit 48e0a2e Jan 22, 2025
8 checks passed
@43081j 43081j deleted the debounce-wormholes branch January 22, 2025 08:01
@pavi2410
Copy link

pavi2410 commented Jan 22, 2025

Great work @43081j James! Thanks for the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debounce filter: defers the last debounced event until a new key is pressed
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.