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

useDebounceFn: Stop debouncing #3375

Open
4 tasks done
NikoGJ opened this issue Sep 5, 2023 · 6 comments · May be fixed by #4561
Open
4 tasks done

useDebounceFn: Stop debouncing #3375

NikoGJ opened this issue Sep 5, 2023 · 6 comments · May be fixed by #4561
Labels
enhancement New feature or request has pr

Comments

@NikoGJ
Copy link

NikoGJ commented Sep 5, 2023

Clear and concise description of the problem

I'd like to have the possibility of stopping the execution of a debounced function that is already planned to be executed in the future.

Example use case: I have an input control where typing would open a panel for autocompletions/suggestions. The typing keyboard events are debounced so that the panel shows after some delay (may be due to avoid the panel doing unecessary network fetches for example). I would also need the escape key to close the panel straight away and/or cancel the fetch, without waiting. But in that case, if they were some keys pressed just before the escape key, the debounce effect is already running and will ultimately re-opens the closed panel. Ideally, I'd like to cancel the planned debounce effect execution before it occurs.

Suggested solution

This would require breaking changes in the api.

// Before
const debounced = useDebounceFn(() => ..., 1000);

// After
const { debounced, stop } = useDebounceFn(() => ..., 1000);

// Call the debounced function (same as before)
debounced().then(...) // or await

// Stop the debounce if the execution of the function is planned but not yet started
stop() // This would eventually throw in the debounced() call depending on the already existing rejectOnCancel parameter.

Alternative

Current alternative is some custom code to track events in the timeline.

Additional context

No response

Validations

@NikoGJ NikoGJ added the enhancement New feature or request label Sep 5, 2023
@NikoGJ
Copy link
Author

NikoGJ commented Sep 5, 2023

For the ones interested, here is an incomplete an naive implementation of a wrapper of the existing useDebounceFn:

export function useDebounceFnStoppable<T extends (...args: any) => any>(fn: T, ms: number) {
	const stopping = ref(false)

	const debounce = useDebounceFn((...args) => {
		if (!stopping.value)
			return fn.apply(null, args)
	}, ms)

	return {
		debounced: (...args: Parameters<T>) => {
			stopping.value = false
			return debounce.call(null, ...args)
		},
		stop: () => stopping.value = true
	}
}

Copy link

stale bot commented Nov 4, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 4, 2023
@stale stale bot closed this as completed Nov 11, 2023
@adamreisnz
Copy link

A shame this issue got closed, it would be good to have a way to stop a debounce from happening. Example use case is a popper which has a debounced open/close. If open is called while a close debounce is to be executed, it would be good to cancel the close.

@4KDA
Copy link

4KDA commented Feb 1, 2024

Reopen, please

@ohylime
Copy link

ohylime commented Mar 13, 2024

Reopen please. We needed this specific functionality, so we need to stop using this now.

@RaulTrombin
Copy link

I was having this issue with forms sending command right after they fetch their values....
for now I did the following:

const debouncedSaveSettings = useDebounceFn(async (updatedSettings) => {
  if (isInitializing.value) {
    return;
  }
....
}


const isInitializing = ref(true);

....
    setTimeout(() => {
      isInitializing.value = false;
    }, 100);
....

@ilyaliao ilyaliao reopened this Feb 4, 2025
@RaulTrombin RaulTrombin linked a pull request Feb 5, 2025 that will close this issue
@ilyaliao ilyaliao added has pr and removed pr welcome labels Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request has pr
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants