Skip to content

Commit 1e47615

Browse files
committed
fix: update based on comments for proper isexecuting status
1 parent 8045dc9 commit 1e47615

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

packages/pluggableWidgets/rich-text-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@mendix/rollup-web-widgets": "workspace:*",
6868
"@mendix/run-e2e": "workspace:*",
6969
"@mendix/widget-plugin-component-kit": "workspace:*",
70+
"@mendix/widget-plugin-hooks": "workspace:*",
7071
"@mendix/widget-plugin-platform": "workspace:*",
7172
"@mendix/widget-plugin-test-utils": "workspace:*",
7273
"@rollup/plugin-alias": "^5.1.1",

packages/shared/widget-plugin-hooks/src/useDebounceWithStatus.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useRef } from "react";
1+
import { useCallback, useEffect, useRef } from "react";
22

33
// this function will directly trigger function if action is not currently executing.
44
// if there are executing actions, it will delay the execution
@@ -10,6 +10,11 @@ export const useDebounceWithStatus = <F extends (...args: any[]) => any>(
1010
): [F] => {
1111
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
1212
const pendingActions = useRef<Parameters<F>>();
13+
const canRun = useRef(isExecuting);
14+
15+
useEffect(() => {
16+
canRun.current = isExecuting;
17+
}, [isExecuting]);
1318

1419
const abort = useCallback((): void => {
1520
if (timeoutRef.current !== null) {
@@ -21,7 +26,7 @@ export const useDebounceWithStatus = <F extends (...args: any[]) => any>(
2126
const runPendingActions = useCallback(() => {
2227
abort();
2328
timeoutRef.current = setTimeout(() => {
24-
if (isExecuting) {
29+
if (canRun.current) {
2530
runPendingActions();
2631
} else {
2732
// only run the last action
@@ -33,18 +38,18 @@ export const useDebounceWithStatus = <F extends (...args: any[]) => any>(
3338
}
3439
}
3540
}, waitFor);
36-
}, [abort, func, waitFor, isExecuting]);
41+
}, [abort, func, waitFor]);
3742

3843
const debounced = useCallback(
3944
(...args: Parameters<F>): void => {
40-
if (isExecuting) {
45+
if (canRun.current) {
4146
pendingActions.current = args;
4247
runPendingActions();
4348
} else {
4449
func(...args);
4550
}
4651
},
47-
[runPendingActions, func, isExecuting]
52+
[runPendingActions, func]
4853
);
4954
return [debounced as F];
5055
};

0 commit comments

Comments
 (0)