Skip to content

Conversation

Han5991
Copy link
Contributor

@Han5991 Han5991 commented Jun 27, 2025

Fixes #7917

  • Fix leading=true to execute only immediately without trailing execution
  • Previously leading=true would execute immediately + after delay (incorrect)
  • Now leading=true executes immediately, ignores subsequent calls during delay
  • Leading state resets after delay period for next immediate execution
  • Update tests to reflect correct behavior expectations
  • Maintain flush() and cancel() method functionality
  • All existing tests pass with corrected behavior

Resolves issue where users expected leading=true to suppress trailing
execution but callback was being invoked again after the delay period

… suppress

  trailing execution (mantinedev#7917)

  - Fix leading=true to execute only immediately without trailing execution
  - Previously leading=true would execute immediately + after delay (incorrect)
  - Now leading=true executes immediately, ignores subsequent calls during delay
  - Leading state resets after delay period for next immediate execution
  - Update tests to reflect correct behavior expectations
  - Maintain flush() and cancel() method functionality
  - All existing tests pass with corrected behavior

  Resolves issue where users expected leading=true to suppress trailing
  execution but callback was being invoked again after the delay period
@Han5991 Han5991 force-pushed the feature/fix-use-debounced-callback branch from 76bd996 to 1c631ef Compare June 27, 2025 15:24
@rtivital rtivital requested a review from Copilot June 30, 2025 04:54
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the behavior of useDebouncedCallback when leading: true so that the callback only fires immediately and never again after the delay period.

  • Refactored callback logic to separate “first” and “subsequent” leading-edge branches and suppress trailing invocations
  • Introduced clearTimeoutAndLeadingRef() helper and reorganized flush/cancel assignment
  • Updated and added tests to assert no trailing execution when leading: true

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.ts Reworked leading/tailing branches, added helper, and adjusted timers
packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.test.ts Updated existing tests and added new scenario to verify suppression of trailing calls
Comments suppressed due to low confidence (3)

packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.ts:40

  • [nitpick] The definitions of flush and cancel are duplicated across the leading and trailing branches. Consider extracting these into shared helpers to reduce code duplication and simplify the flow.
        if (leading && isFirstCall) {

packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.ts:34

  • [nitpick] The name clearTimeoutAndLeadingRef is quite long and mixes concerns. Renaming it to something like resetDebounceState or resetLeadingState could improve clarity.
        function clearTimeoutAndLeadingRef() {

packages/@mantine/hooks/src/use-debounced-callback/use-debounced-callback.test.ts:337

  • Consider adding tests for the flush() and cancel() methods when leading: true to ensure they invoke or suppress the callback correctly under various timing scenarios.
  it('leading=true should suppress trailing execution', () => {

@rtivital rtivital merged commit 8ba15fb into mantinedev:master Jun 30, 2025
1 check passed
@rtivital
Copy link
Member

Thanks!

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.

useDebouncedCallback with leading: true is triggered again after the delay when called twice in quick succession
2 participants