From abe348bcdc2a46f168c80ac9eca623de921ed918 Mon Sep 17 00:00:00 2001 From: xobotyi Date: Sun, 21 Oct 2018 21:00:52 +0300 Subject: [PATCH] build --- dist/util/LoopController.js | 191 ++++++++++++++++++++++-------------- 1 file changed, 115 insertions(+), 76 deletions(-) diff --git a/dist/util/LoopController.js b/dist/util/LoopController.js index 9006177..aab5b0d 100644 --- a/dist/util/LoopController.js +++ b/dist/util/LoopController.js @@ -3,93 +3,132 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = void 0; +exports.createLoopController = createLoopController; +exports.default = exports.LoopController = void 0; + +function LoopControllerClass() { + var _this = this; + + /** + * @typedef {Object} Scrollbar + * @property {function} update + */ + + /** + * @type {Scrollbar[]} + */ + var scrollbarsRegister = []; + /** + * true if loop is active + * @type {boolean} + */ + + var isActive = false; + /** + * ID of requested animation frame + * @type {null|number} + */ + + var animationFrameId = null; + /** + * Function that called in animation frame + */ + + var animationFrameCallback = function animationFrameCallback() { + if (!isActive) { + return; + } -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + for (var _i = 0; _i < scrollbarsRegister.length; _i++) { + var scrollbar = scrollbarsRegister[_i]; + scrollbar.update(); + } -function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } + requestAnimationFrame(animationFrameCallback); + }; + /** + * Stop the loop if it wasn't active + * @return {LoopControllerClass} + */ -function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } -var loopIsActive = false; -var animationFrame = null; -var loopRegister = []; + this.start = function () { + if (isActive) { + return _this; + } -var LoopController = -/*#__PURE__*/ -function () { - function LoopController() { - _classCallCheck(this, LoopController); + isActive = true; + animationFrameId && cancelAnimationFrame(animationFrameId); + requestAnimationFrame(animationFrameCallback); + }; + /** + * Stop the loop if it is active + * @return {LoopControllerClass} + */ - this.rafStep = this.rafStep.bind(this); - } - _createClass(LoopController, [{ - key: "getRegisteredItems", - value: function getRegisteredItems() { - return loopRegister.concat(); - } - }, { - key: "registerScrollbar", - value: function registerScrollbar(scrollbar) { - if (!loopRegister.includes(scrollbar)) { - loopRegister.push(scrollbar); - this.start(); - } - - return this; + this.stop = function () { + if (!isActive) { + return _this; } - }, { - key: "unregisterScrollbar", - value: function unregisterScrollbar(scrollbar) { - var index = loopRegister.indexOf(scrollbar); - if (index !== -1) { - loopRegister.length === 1 && this.stop(); - loopRegister.splice(index, 1); - } + isActive = false; + animationFrameId && cancelAnimationFrame(animationFrameId); + animationFrameId = null; + }; + /** + * Return the array pf registered scrollbars + * @return {Scrollbar[]} + */ - return this; - } - }, { - key: "start", - value: function start() { - if (!loopIsActive) { - loopIsActive = true; - animationFrame && cancelAnimationFrame(animationFrame); - animationFrame = requestAnimationFrame(this.rafStep); - } - - return this; - } - }, { - key: "rafStep", - value: function rafStep() { - if (!loopIsActive) { - return; - } - - for (var i = 0; i < loopRegister.length; i++) { - loopRegister[i].update(); - } - - animationFrame = requestAnimationFrame(this.rafStep); + + this.getRegisteredScrollbars = function () { + return scrollbarsRegister.concat(); + }; + /** + * Add the scrollbar to list to iterate each loop + * @param {Scrollbar} scrollbar + * @return {LoopControllerClass} + */ + + + this.registerScrollbar = function (scrollbar) { + if (scrollbarsRegister.indexOf(scrollbar) === -1) { + scrollbarsRegister.push(scrollbar); + + _this.start(); } - }, { - key: "stop", - value: function stop() { - if (loopIsActive) { - loopIsActive = false; - animationFrame && cancelAnimationFrame(animationFrame); - } - - return this; + + return _this; + }; + /** + * Remove the scrollbar from list to iterate each loop + * @param {Scrollbar} scrollbar + * @return {LoopControllerClass} + */ + + + this.unregisterScrollbar = function (scrollbar) { + var index = scrollbarsRegister.indexOf(scrollbar); + + if (index !== -1) { + scrollbarsRegister.splice(index, 1); } - }]); - return LoopController; -}(); + return _this; + }; +} + +var LoopController = new LoopControllerClass(); +exports.LoopController = LoopController; +var _default = LoopController; +/** + * Return new instance of LoopControllerClass + * @return {LoopControllerClass} + */ + +exports.default = _default; -var instance = new LoopController(); -var _default = instance; -exports.default = _default; \ No newline at end of file +function createLoopController() { + return new LoopControllerClass(); +} \ No newline at end of file