Skip to content

Commit

Permalink
fix(core): add setTimeout for first invoking #WIK-16088
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jul 16, 2024
1 parent 507762a commit 4cf4d52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-kangaroos-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/core': patch
---

correct debounce method #WIK-16088
21 changes: 11 additions & 10 deletions packages/core/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ export const throttleRAF = (board: PlaitBoard, key: string, fn: () => void) => {
};

export const debounce = <T>(func: (args?: T) => void, wait: number, options?: { leading: boolean }) => {
let timeout: any = null;
let timeoutId: any = null;
return (args?: T) => {
if (timeout !== null) {
clearTimeout(timeout);
timeout = setTimeout(() => {
if (timeoutId !== null) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func(args);
timeout = null;
timeoutId = null;
}, wait);
} else {
if (options?.leading) {
func(args);
} else {
timeout = setTimeout(() => {
func(args);
timeout = null;
}, wait);
}
timeoutId = setTimeout(() => {
timeoutId = null;
if (!options?.leading) {
func(args);
}
}, wait);
}
};
};
Expand Down

0 comments on commit 4cf4d52

Please sign in to comment.