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

Debounce submitCustom #1205

Merged
merged 15 commits into from
May 2, 2024
Merged

Debounce submitCustom #1205

merged 15 commits into from
May 2, 2024

Conversation

soundsbywise
Copy link
Contributor

@soundsbywise soundsbywise commented May 1, 2024

Overview

The submitCustom method can be called several times in rapid succession which leads to BE race conditions. To offset this we debounce the method so that each successive call has to wait 1 second.

Notes

  1. Updates are aggregated so that the latest key value pairs are sent to the BE when a call goes through.
  2. If the method is called and the form is about to be completed, we send the pending changes to the backend regardless of the wait time

Testing

Submitted the Book A Demo feathery form before and after changes - works the same.

@soundsbywise soundsbywise force-pushed the singh/debounce-submit-custom branch 2 times, most recently from db1b046 to aa025d4 Compare May 1, 2024 18:12
@soundsbywise soundsbywise force-pushed the singh/debounce-submit-custom branch from 7a68536 to 6fa1baf Compare May 1, 2024 18:18
@soundsbywise soundsbywise marked this pull request as ready for review May 1, 2024 18:19
this.flushPendingSubmitCustomUpdates = (override: boolean = true) => {
// we call the debounced method and then the flush to immediately submit changes
// see: https://github.com/lodash/lodash/issues/4185#issuecomment-462388355
this.debouncedSubmitCustom(override);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added: to immediately submit changes we must call the flush method in this way

@soundsbywise soundsbywise requested a review from bo-dun-1 May 2, 2024 02:11
// we call the debounced method and then the flush to immediately submit changes
// see: https://github.com/lodash/lodash/issues/4185#issuecomment-462388355
this.debouncedSubmitCustom(override ?? true);
return this.debouncedSubmitCustom.flush();
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm not familiar with the flush function. will this return the promise from the network request so it can be awaited by the client of this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, the return value of flush is the promise from the network request. When resolved, the value is the Response object. See below

flushed is the me logging the return value from flush():

image image

this._debouncedSubmitCustom.bind(this),
SUBMIT_CUSTOM_DEBOUNCE_WINDOW
);
this.flushPendingSubmitCustomUpdates = (override?: boolean) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm not sure this method is necessary actually. if there is data to flush, there will be a debounced, pending submitCustom call as well so no need to call this again or have this helper function. .flush() can be called directly.

Copy link
Contributor Author

@soundsbywise soundsbywise May 2, 2024

Choose a reason for hiding this comment

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

I think wrapping the flush call in this method creates for a cleaner API long term.

There's currently 3 instances where we flush. Since the lodash.flush implementation has this caveat that requires us to

  1. call the debounced function
  2. then call flush()

It's easier to debug and refactor the flush call if its defined in one place.

Otherwise, there's going to be 3 instances of:

this.debouncedSubmitCustom(override ?? true);
this.debouncedSubmitCustom.flush();

For that reason, my vote is to keep flushPendingSubmitCustomUpdates

@soundsbywise soundsbywise requested a review from bo-dun-1 May 2, 2024 19:42
@soundsbywise soundsbywise requested a review from bo-dun-1 May 2, 2024 21:15
@soundsbywise soundsbywise merged commit caa385d into master May 2, 2024
5 checks passed
@soundsbywise soundsbywise deleted the singh/debounce-submit-custom branch May 2, 2024 21:34
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.

None yet

2 participants