Improve styling, accessebility, usability and use mutation

This commit is contained in:
Chi Vinh Le
2018-08-03 15:57:14 +02:00
parent d05508d574
commit 1376ed255b
21 changed files with 202 additions and 147 deletions
+1
View File
@@ -1,2 +1,3 @@
export { default as timeout } from "./timeout";
export { default as pascalCase } from "./pascalCase";
export { default as oncePerFrame } from "./oncePerFrame";
+18
View File
@@ -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;