requestNewFrame.mjs 595 B

123456789101112131415161718192021222324252627
  1. let clean = true;
  2. const frames = [];
  3. /**
  4. * Schedule a task to run in an animation frame on its own
  5. * This is useful for heavy tasks that may cause jank if all ran together
  6. */
  7. export function requestNewFrame(cb) {
  8. if (!clean || frames.length) {
  9. frames.push(cb);
  10. run();
  11. } else {
  12. clean = false;
  13. cb();
  14. run();
  15. }
  16. }
  17. let raf = -1;
  18. function run() {
  19. cancelAnimationFrame(raf);
  20. raf = requestAnimationFrame(() => {
  21. const frame = frames.shift();
  22. if (frame) frame();
  23. if (frames.length) run();else clean = true;
  24. });
  25. }
  26. //# sourceMappingURL=requestNewFrame.mjs.map