-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(useRequest): 修复runAsync在节流状态下不生效的问题 #2899
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ const useThrottlePlugin: Plugin<any, any[]> = ( | |
| if (throttleWait) { | ||
| const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance); | ||
|
|
||
| // Track the current promise and when it was created | ||
| let currentPromise: Promise<any> | null = null; | ||
| let promiseCreatedAt = 0; | ||
|
|
||
| throttledRef.current = throttle( | ||
| (callback) => { | ||
| callback(); | ||
|
|
@@ -33,13 +37,42 @@ const useThrottlePlugin: Plugin<any, any[]> = ( | |
| // throttle runAsync should be promise | ||
| // https://github.com/lodash/lodash/issues/4400#issuecomment-834800398 | ||
| fetchInstance.runAsync = (...args) => { | ||
| return new Promise((resolve, reject) => { | ||
| const now = Date.now(); | ||
|
|
||
| // If there's a current promise and it was created within the throttle window, | ||
| // return it to share the result | ||
| if (currentPromise && now - promiseCreatedAt < throttleWait) { | ||
| return currentPromise; | ||
| } | ||
|
Comment on lines
39
to
+46
|
||
|
|
||
| // Create a new promise | ||
| promiseCreatedAt = now; | ||
| currentPromise = new Promise((resolve, reject) => { | ||
| throttledRef.current?.(() => { | ||
| // Execute the request | ||
| _originRunAsync(...args) | ||
| .then(resolve) | ||
| .catch(reject); | ||
| .then((result) => { | ||
| resolve(result); | ||
| // Clear current promise after a delay to allow trailing calls | ||
| setTimeout(() => { | ||
| if (currentPromise && Date.now() - promiseCreatedAt >= throttleWait) { | ||
| currentPromise = null; | ||
| } | ||
| }, 0); | ||
|
Comment on lines
+56
to
+61
|
||
| }) | ||
| .catch((error) => { | ||
| reject(error); | ||
| // Clear current promise after a delay to allow trailing calls | ||
| setTimeout(() => { | ||
| if (currentPromise && Date.now() - promiseCreatedAt >= throttleWait) { | ||
| currentPromise = null; | ||
| } | ||
| }, 0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| return currentPromise; | ||
| }; | ||
|
|
||
| return () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coverage only exercises
throttleTrailing: falseforrunAsync. Given the plugin exposes boththrottleLeadingandthrottleTrailing, add a case forthrottleTrailing: true(and ideallythrottleLeading: false) asserting that the trailing invocation runs and uses the latest call's params; this would catch regressions in trailing behavior.