UNPKG

@exadel/esl

Version:

Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components

42 lines (41 loc) 1.28 kB
export class RAFMock { constructor() { this.handleCounter = 0; this.queue = new Map(); } requestAnimationFrame(callback) { const handle = this.handleCounter++; this.queue.set(handle, callback); return handle; } cancelAnimationFrame(handle) { this.queue.delete(handle); } triggerNextAnimationFrame(time = performance.now()) { const nextEntry = this.queue.entries().next().value; if (nextEntry === undefined) return; const [nextHandle, nextCallback] = nextEntry; nextCallback(time); this.queue.delete(nextHandle); } triggerAllAnimationFrames(time = performance.now()) { while (this.queue.size > 0) this.triggerNextAnimationFrame(time); } reset() { this.queue.clear(); this.handleCounter = 0; } static applyMock() { if (RAFMock.instance) return; const instance = new RAFMock(); window.requestAnimationFrame = instance.requestAnimationFrame.bind(instance); window.cancelAnimationFrame = instance.cancelAnimationFrame.bind(instance); window.rafMockImpl = instance; } static get instance() { return window.rafMockImpl; } }