astro/packages/webapi/src/lib/AnimationFrame.ts
2022-06-06 16:49:53 +00:00

41 lines
838 B
TypeScript

import {
clearTimeout as nodeClearTimeout,
setTimeout as nodeSetTimeout,
} from 'node:timers'
import * as _ from './utils.js'
const INTERNAL = { tick: 0, pool: new Map() }
export function requestAnimationFrame<
TArgs extends any[],
TFunc extends (...args: TArgs) => any
>(callback: TFunc): number {
if (!INTERNAL.pool.size) {
nodeSetTimeout(() => {
const next = _.__performance_now()
for (const func of INTERNAL.pool.values()) {
func(next)
}
INTERNAL.pool.clear()
}, 1000 / 16)
}
const func = _.__function_bind(callback, undefined)
const tick = ++INTERNAL.tick
INTERNAL.pool.set(tick, func)
return tick
}
export function cancelAnimationFrame(requestId: number): void {
const timeout = INTERNAL.pool.get(requestId)
if (timeout) {
nodeClearTimeout(timeout)
INTERNAL.pool.delete(requestId)
}
}