-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1408 from grid-js/fix-multiple-reqs
Fix the multiple server-side requests bug
- Loading branch information
Showing
15 changed files
with
259 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns true if both objects are equal | ||
* @param a left object | ||
* @param b right object | ||
* @returns | ||
*/ | ||
export function deepEqual<A, B>(a: A, b: B) { | ||
return JSON.stringify(a) === JSON.stringify(b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
/** | ||
* Throttle a given function | ||
* @param fn Function to be called | ||
* @param wait Throttle timeout in milliseconds | ||
* @returns Throttled function | ||
*/ | ||
export const throttle = (fn: (...args) => void, wait = 100) => { | ||
let inThrottle: boolean; | ||
let lastFn: ReturnType<typeof setTimeout>; | ||
let lastTime: number; | ||
let timeoutId: ReturnType<typeof setTimeout>; | ||
let lastTime = Date.now(); | ||
|
||
const execute = (...args) => { | ||
lastTime = Date.now(); | ||
fn(...args); | ||
}; | ||
|
||
return (...args) => { | ||
if (!inThrottle) { | ||
fn(...args); | ||
lastTime = Date.now(); | ||
inThrottle = true; | ||
const currentTime = Date.now(); | ||
const elapsed = currentTime - lastTime; | ||
|
||
if (elapsed >= wait) { | ||
// If enough time has passed since the last call, execute the function immediately | ||
execute(args); | ||
} else { | ||
clearTimeout(lastFn); | ||
lastFn = setTimeout(() => { | ||
if (Date.now() - lastTime >= wait) { | ||
fn(...args); | ||
lastTime = Date.now(); | ||
} | ||
}, Math.max(wait - (Date.now() - lastTime), 0)); | ||
// If not enough time has passed, schedule the function call after the remaining delay | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
timeoutId = setTimeout(() => { | ||
execute(args); | ||
timeoutId = null; | ||
}, wait - elapsed); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.