2026-09-08 15:59:26 +02:00
|
|
|
// 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')
|
|
|
|
|
|
2026-09-08 17:57:27 +02:00
|
|
|
// 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)
|
2026-09-08 15:59:26 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|