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

Add leading and trailing options for debounce helper #2380 #2382

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ensconced
Copy link

@ensconced ensconced commented Jun 8, 2023

Q A
Fixed Issues? Fixes #2380
Patch: Bug Fix? No
Major: Breaking Change? No
Minor: New Feature? Yes
Tests Added + Pass? Yes
Any Dependency Changes? No

This adds leading and trailing options to the debounce helper, as suggested in the linked issue. The first parameter to debounce can now be an options object of format { delayLength: number; leading?: boolean; trailing?: boolean }, or for backwards compatibility can still just be a number specifying delayLength.

leading defaults to false and trailing defaults to true. The behaviour in this case should be unchanged compared to on main.

The behaviour of "leading" and "trailing" should essentially match how it works in lodash.

@changeset-bot
Copy link

changeset-bot bot commented Jun 8, 2023

⚠️ No Changeset found

Latest commit: f380c48

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@neurosnap neurosnap self-requested a review June 15, 2023 13:54
Copy link
Member

@neurosnap neurosnap left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this PR! I had one comment about the API and then we also need to ensure our types are updated to reflect this change.

See here for a starting place: https://github.com/redux-saga/redux-saga/blob/main/packages/core/types/ts3.6/effects.d.ts#L1220

@@ -1,8 +1,16 @@
import fsmIterator, { safeName } from './fsmIterator'
import { delay, fork, race, take } from '../io'

export default function debounceHelper(delayLength, patternOrChannel, worker, ...args) {
let action, raceOutput
export default function debounceHelper(delayLengthOrOptions, patternOrChannel, worker, ...args) {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if both leading and trailing are set to true?

Copy link
Author

Choose a reason for hiding this comment

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

The saga will run at the start of the wait period, and if any further actions occur during the wait period, it will run again at the end of the wait period. This matches the behavior of the lodash debounce function with leading: true, trailing: true. I imagine this will be a common use-case - it's how I will use it personally.

There is an example of this in the tests.

Copy link
Author

Choose a reason for hiding this comment

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

I have deployed a little example here https://heartfelt-semifreddo-a4737f.netlify.app/
(Hit any key on the keyboard to see the effect of a 1000ms debounce with the various options).

Source code is here

@ensconced
Copy link
Author

@neurosnap I just wanted to gently bump this - do you have any concerns about this change? Or are there any further changes required?

@neurosnap
Copy link
Member

Thanks for the bump. I haven't forgotten about this PR, just need to make time to fully review and test it. I'll try to get this reviewed by end of week.

Copy link
Member

@neurosnap neurosnap left a comment

Choose a reason for hiding this comment

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

leading = false
trailing = true
} else {
;({ delayLength, leading = false, trailing = true } = delayLengthOrOptions)
Copy link
Member

Choose a reason for hiding this comment

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

This syntax reads a little awkward, could we rework this?

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.

could we add leading/trailing edge options for debounce?
3 participants