@infinite-canvas-tutorial/webcomponents
Version:
WebComponents UI implementation
59 lines • 1.84 kB
JavaScript
import { DOMAdapter } from '@infinite-canvas-tutorial/ecs';
export class AnimationFrameHandler {
constructor() {
this.targets = new WeakMap();
this.rafIds = new WeakMap();
}
register(key, callback) {
this.targets.set(key, { callback, stopped: true });
}
start(key) {
const target = this.targets.get(key);
if (!target) {
return;
}
if (this.rafIds.has(key)) {
return;
}
this.targets.set(key, Object.assign(Object.assign({}, target), { stopped: false }));
this.scheduleFrame(key);
}
stop(key) {
const target = this.targets.get(key);
if (target && !target.stopped) {
this.targets.set(key, Object.assign(Object.assign({}, target), { stopped: true }));
}
this.cancelFrame(key);
}
constructFrame(key) {
return (timestamp) => {
const target = this.targets.get(key);
if (!target) {
return;
}
const shouldAbort = this.onFrame(target, timestamp);
if (!target.stopped && !shouldAbort) {
this.scheduleFrame(key);
}
else {
this.cancelFrame(key);
}
};
}
scheduleFrame(key) {
const rafId = DOMAdapter.get().requestAnimationFrame(this.constructFrame(key));
this.rafIds.set(key, rafId);
}
cancelFrame(key) {
if (this.rafIds.has(key)) {
const rafId = this.rafIds.get(key);
DOMAdapter.get().cancelAnimationFrame(rafId);
}
this.rafIds.delete(key);
}
onFrame(target, timestamp) {
const shouldAbort = target.callback(timestamp);
return shouldAbort || false;
}
}
//# sourceMappingURL=animation-frame-handler.js.map