mirror of
https://github.com/wassname/talk.git
synced 2026-07-08 10:23:53 +08:00
Improve styling, accessebility, usability and use mutation
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export { default as timeout } from "./timeout";
|
||||
export { default as pascalCase } from "./pascalCase";
|
||||
export { default as oncePerFrame } from "./oncePerFrame";
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Function decorator that prevents calling `fn` more then once per frame.
|
||||
* If called more than once, the last return value gets returned.
|
||||
*/
|
||||
const oncePerFrame = <T extends (...args: any[]) => any>(fn: T) => {
|
||||
let toggledThisFrame = false;
|
||||
let lastResult: any = null;
|
||||
return ((...args: any[]) => {
|
||||
if (toggledThisFrame) {
|
||||
return lastResult;
|
||||
}
|
||||
toggledThisFrame = true;
|
||||
lastResult = fn(...args);
|
||||
setTimeout(() => (toggledThisFrame = false), 0);
|
||||
}) as T;
|
||||
};
|
||||
|
||||
export default oncePerFrame;
|
||||
Reference in New Issue
Block a user