From 5101f62362ae5b65bb160fb90f4be960bc918511 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 24 Dec 2023 22:44:43 -0600 Subject: [PATCH] feat(util): add next frame helper --- src/lib/index.ts | 3 +++ src/lib/util.ts | 11 +++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/lib/index.ts create mode 100644 src/lib/util.ts diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..fb4a17f --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,3 @@ +import { nextFrame } from './util'; + +export { nextFrame }; diff --git a/src/lib/util.ts b/src/lib/util.ts new file mode 100644 index 0000000..fee8c0e --- /dev/null +++ b/src/lib/util.ts @@ -0,0 +1,11 @@ +/** + * Returns a promise that settles after the next frame renders. Useful for spacing synchronous + * work on the main thread. + */ +const nextFrame = () => { + return new Promise((resolve) => { + requestAnimationFrame(() => resolve()); + }); +}; + +export { nextFrame };