UNPKG

victory-core

Version:
57 lines (56 loc) 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _d3Timer = require("victory-vendor/d3-timer"); 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((0, _d3Timer.now)() - s.startTime, s.duration); }); }; start() { if (!this.timer) { this.timer = (0, _d3Timer.timer)(this.loop); } } stop() { if (this.timer) { this.timer.stop(); this.timer = null; } } subscribe(callback, duration) { const subscriptionID = this.subscribers.push({ startTime: (0, _d3Timer.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(); } } } exports.default = Timer;