Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"backoff_timer.min.js","sources":["../src/backoff_timer.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * A timer that will execute a callback with decreasing frequency. Useful for\n * doing polling on the server without overwhelming it with requests.\n *\n * @module     core/backoff_timer\n * @copyright  2016 Ryan Wyllie <ryan@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(function() {\n\n    /**\n     * Constructor for the back off timer.\n     *\n     * @class\n     * @param {function} callback The function to execute after each tick\n     * @param {function} backoffFunction The function to determine what the next timeout value should be\n     */\n    var BackoffTimer = function(callback, backoffFunction) {\n        this.callback = callback;\n        this.backOffFunction = backoffFunction;\n    };\n\n    /**\n     * @property {function} callback The function to execute after each tick\n     */\n    BackoffTimer.prototype.callback = null;\n\n    /**\n     * @property {function} backoffFunction The function to determine what the next timeout value should be\n     */\n    BackoffTimer.prototype.backOffFunction = null;\n\n    /**\n     * @property {int} time The timeout value to use\n     */\n    BackoffTimer.prototype.time = null;\n\n    /**\n     * @property {numeric} timeout The timeout identifier\n     */\n    BackoffTimer.prototype.timeout = null;\n\n    /**\n     * Generate the next timeout in the back off time sequence\n     * for the timer.\n     *\n     * The back off function is called to calculate the next value.\n     * It is given the current value and an array of all previous values.\n     *\n     * @return {int} The new timeout value (in milliseconds)\n     */\n    BackoffTimer.prototype.generateNextTime = function() {\n        var newTime = this.backOffFunction(this.time);\n        this.time = newTime;\n\n        return newTime;\n    };\n\n    /**\n     * Stop the current timer and clear the previous time values\n     *\n     * @return {object} this\n     */\n    BackoffTimer.prototype.reset = function() {\n        this.time = null;\n        this.stop();\n\n        return this;\n    };\n\n    /**\n     * Clear the current timeout, if one is set.\n     *\n     * @return {object} this\n     */\n    BackoffTimer.prototype.stop = function() {\n        if (this.timeout) {\n            window.clearTimeout(this.timeout);\n            this.timeout = null;\n        }\n\n        return this;\n    };\n\n    /**\n     * Start the current timer by generating the new timeout value and\n     * starting the ticks.\n     *\n     * This function recurses after each tick with a new timeout value\n     * generated each time.\n     *\n     * The callback function is called after each tick.\n     *\n     * @return {object} this\n     */\n    BackoffTimer.prototype.start = function() {\n        // If we haven't already started.\n        if (!this.timeout) {\n            var time = this.generateNextTime();\n            this.timeout = window.setTimeout(function() {\n                this.callback();\n                // Clear the existing timer.\n                this.stop();\n                // Start the next timer.\n                this.start();\n            }.bind(this), time);\n        }\n\n        return this;\n    };\n\n    /**\n     * Reset the timer and start it again from the initial timeout\n     * values\n     *\n     * @return {object} this\n     */\n    BackoffTimer.prototype.restart = function() {\n        return this.reset().start();\n    };\n\n    /**\n     * Returns an incremental function for the timer.\n     *\n     * @param {int} minamount The minimum amount of time we wait before checking\n     * @param {int} incrementamount The amount to increment the timer by\n     * @param {int} maxamount The max amount to ever increment to\n     * @param {int} timeoutamount The timeout to use once we reach the max amount\n     * @return {function}\n     */\n     BackoffTimer.getIncrementalCallback = function(minamount, incrementamount, maxamount, timeoutamount) {\n\n        /**\n         * An incremental function for the timer.\n         *\n         * @param {(int|null)} time The current timeout value or null if none set\n         * @return {int} The new timeout value\n         */\n        return function(time) {\n            if (!time) {\n                return minamount;\n            }\n\n            // Don't go over the max amount.\n            if (time + incrementamount > maxamount) {\n                return timeoutamount;\n            }\n\n            return time + incrementamount;\n        };\n    };\n\n    return BackoffTimer;\n});\n"],"names":["define","BackoffTimer","callback","backoffFunction","backOffFunction","prototype","time","timeout","generateNextTime","newTime","this","reset","stop","window","clearTimeout","start","setTimeout","bind","restart","getIncrementalCallback","minamount","incrementamount","maxamount","timeoutamount"],"mappings":";;;;;;;;AAuBAA,6BAAO,eASCC,aAAe,SAASC,SAAUC,sBAC7BD,SAAWA,cACXE,gBAAkBD,wBAM3BF,aAAaI,UAAUH,SAAW,KAKlCD,aAAaI,UAAUD,gBAAkB,KAKzCH,aAAaI,UAAUC,KAAO,KAK9BL,aAAaI,UAAUE,QAAU,KAWjCN,aAAaI,UAAUG,iBAAmB,eAClCC,QAAUC,KAAKN,gBAAgBM,KAAKJ,kBACnCA,KAAOG,QAELA,SAQXR,aAAaI,UAAUM,MAAQ,uBACtBL,KAAO,UACPM,OAEEF,MAQXT,aAAaI,UAAUO,KAAO,kBACtBF,KAAKH,UACLM,OAAOC,aAAaJ,KAAKH,cACpBA,QAAU,MAGZG,MAcXT,aAAaI,UAAUU,MAAQ,eAEtBL,KAAKH,QAAS,KACXD,KAAOI,KAAKF,wBACXD,QAAUM,OAAOG,WAAW,gBACxBd,gBAEAU,YAEAG,SACPE,KAAKP,MAAOJ,aAGXI,MASXT,aAAaI,UAAUa,QAAU,kBACtBR,KAAKC,QAAQI,SAYvBd,aAAakB,uBAAyB,SAASC,UAAWC,gBAAiBC,UAAWC,sBAQ5E,SAASjB,aACPA,KAKDA,KAAOe,gBAAkBC,UAClBC,cAGJjB,KAAOe,gBARHD,YAYZnB"}