// Ported from nanoraf 3.1.0 (MIT) — https://github.com/choojs/nanoraf // Only call requestAnimationFrame when needed. import { ok, equal } from './assert.js' export default function nanoraf (render, raf) { equal(typeof render, 'function', 'nanoraf: render should be a function') ok(typeof raf === 'function' || typeof raf === 'undefined', 'nanoraf: raf should be a function or undefined') // Wrap rather than alias: calling an extracted requestAnimationFrame // with no receiver throws 'Illegal invocation' in strict-mode ESM // (the CJS original survived only thanks to sloppy-mode this-patching). if (!raf) raf = (cb) => globalThis.requestAnimationFrame(cb) let redrawScheduled = false let args = null return function frame (...frameArgs) { if (args === null && !redrawScheduled) { redrawScheduled = true raf(function redraw () { redrawScheduled = false const _args = args args = null render(..._args) }) } args = frameArgs } }