diff options
author | 2022-03-07 15:36:22 -0600 | |
---|---|---|
committer | 2022-03-07 15:36:22 -0600 | |
commit | f18ee36dc0abdc5c8ec87734de7962966d16fe65 (patch) | |
tree | c01a7034186cb0bbe5e1d042f4a5dd09bad21ed5 /packages/webapi/src/lib/AnimationFrame.ts | |
parent | 10a9c3412b4f6e8607687a74eafdb150d3222047 (diff) | |
download | astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.gz astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.zst astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.zip |
Add `@astrojs/webapi` package (#2729)@astrojs/webapi@0.11.0
* chore: add @astrojs/webapi
* chore: update package.json
* fix: update file case
* fix: remove lowercase file
* chore: update tests to use mocha
* chore: update LICENSE
Diffstat (limited to 'packages/webapi/src/lib/AnimationFrame.ts')
-rw-r--r-- | packages/webapi/src/lib/AnimationFrame.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/packages/webapi/src/lib/AnimationFrame.ts b/packages/webapi/src/lib/AnimationFrame.ts new file mode 100644 index 000000000..744c445bf --- /dev/null +++ b/packages/webapi/src/lib/AnimationFrame.ts @@ -0,0 +1,35 @@ +import { setTimeout as nodeSetTimeout, clearTimeout as nodeClearTimeout } 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) + } +} |