UNPKG

react-sequencer

Version:

Step based sequencer to give your components reliable states for transitions and animations.

66 lines 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var onNextTick; var cancelNextTick; if (typeof window !== 'undefined' && typeof window.requestAnimationFrame === 'function') { onNextTick = window.requestAnimationFrame; cancelNextTick = window.cancelAnimationFrame; } else if (typeof setTimeout === 'function') { onNextTick = function (func) { return setTimeout(func, 15); }; cancelNextTick = clearTimeout; } else { throw new Error('React sequencer depends on requestAnimationFrame, please use a polyfill if not available in the browser.'); } var Ticker = /** @class */ (function () { function Ticker() { var _this = this; this.startLoop = function () { if (!_this.isActive) { _this.isActive = true; _this.currentTimeStamp = Date.now(); _this.requestID = onNextTick(_this._onLoop); } }; this.stopLoop = function () { if (_this.isActive) { _this.isActive = false; cancelNextTick(_this.requestID); } }; this._onLoop = function () { _this.currentTimeStamp = Date.now(); for (var i = 0; i < _this.subscriptions.length; i++) { var fn = _this.subscriptions[i]; fn(_this.currentTimeStamp); } if (_this.isActive) { _this.requestID = onNextTick(_this._onLoop); } }; this.currentTimeStamp = Date.now(); this.isActive = false; this.requestID = null; this.subscriptions = []; } Ticker.prototype.onTick = function (fn) { if (this.subscriptions.length === 0) { this.startLoop(); } this.subscriptions.push(fn); }; Ticker.prototype.offTick = function (fn) { var index = this.subscriptions.indexOf(fn); if (index !== -1) { this.subscriptions.splice(index, 1); } if (this.subscriptions.length === 0) { this.stopLoop(); } }; return Ticker; }()); exports.default = Ticker; //# sourceMappingURL=ticker.js.map