victory-core
Version:
50 lines • 1.11 kB
JavaScript
import { timer, now } from "victory-vendor/d3-timer";
export default class Timer {
constructor() {
this.shouldAnimate = true;
this.subscribers = [];
this.timer = null;
this.activeSubscriptions = 0;
}
bypassAnimation() {
this.shouldAnimate = false;
}
resumeAnimation() {
this.shouldAnimate = true;
}
loop = () => {
this.subscribers.forEach(s => {
s.callback(now() - s.startTime, s.duration);
});
};
start() {
if (!this.timer) {
this.timer = timer(this.loop);
}
}
stop() {
if (this.timer) {
this.timer.stop();
this.timer = null;
}
}
subscribe(callback, duration) {
const subscriptionID = this.subscribers.push({
startTime: now(),
callback,
duration: this.shouldAnimate ? duration : 0
});
this.activeSubscriptions++;
this.start();
return subscriptionID;
}
unsubscribe(id) {
if (id !== null && this.subscribers[id - 1]) {
delete this.subscribers[id - 1];
this.activeSubscriptions--;
}
if (this.activeSubscriptions === 0) {
this.stop();
}
}
}