@infinite-canvas-tutorial/webcomponents
Version:
WebComponents UI implementation
63 lines • 2.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnimationFrameHandler = void 0;
const ecs_1 = require("@infinite-canvas-tutorial/ecs");
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 = ecs_1.DOMAdapter.get().requestAnimationFrame(this.constructFrame(key));
this.rafIds.set(key, rafId);
}
cancelFrame(key) {
if (this.rafIds.has(key)) {
const rafId = this.rafIds.get(key);
ecs_1.DOMAdapter.get().cancelAnimationFrame(rafId);
}
this.rafIds.delete(key);
}
onFrame(target, timestamp) {
const shouldAbort = target.callback(timestamp);
return shouldAbort || false;
}
}
exports.AnimationFrameHandler = AnimationFrameHandler;
//# sourceMappingURL=animation-frame-handler.js.map