mirror of
https://github.com/moodle/moodle.git
synced 2025-07-21 14:21:42 +02:00
MDL-56139 core: changes after peer review
- No longer use the Fibonacci sequence for delaying the timeout. It is too aggressive. - The backoff_timer AMD module now expects the callback AND the backoff function to be passed to the constructor. - Added ability to specify polling frequency in config.php. - Added helper function to return the cache key. - Reworded the parameters for clarity.
This commit is contained in:
@@ -25,92 +25,36 @@
|
||||
*/
|
||||
define(function() {
|
||||
|
||||
// Default to one second.
|
||||
var DEFAULT_TIME = 1000;
|
||||
|
||||
/**
|
||||
* The default back off function for the timer. It uses the Fibonacci
|
||||
* sequence to determine what the next timeout value should be.
|
||||
*
|
||||
* @param {(int|null)} time The current timeout value or null if none set
|
||||
* @param {array} previousTimes An array containing all previous timeout values
|
||||
* @return {int} The new timeout value
|
||||
*/
|
||||
var fibonacciBackOff = function(time, previousTimes) {
|
||||
if (!time) {
|
||||
return DEFAULT_TIME;
|
||||
}
|
||||
|
||||
if (previousTimes.length) {
|
||||
var lastTime = previousTimes[previousTimes.length - 1];
|
||||
return time + lastTime;
|
||||
} else {
|
||||
return DEFAULT_TIME;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for the back off timer.
|
||||
*
|
||||
* @param {function} callback The function to execute after each tick
|
||||
* @param {function} backoffFunction The function to determine what the next timeout value should be
|
||||
*/
|
||||
var Timer = function(callback) {
|
||||
this.reset();
|
||||
this.setCallback(callback);
|
||||
// Set the default backoff function to be the Fibonacci sequence.
|
||||
this.setBackOffFunction(fibonacciBackOff);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the callback function to be executed after each tick of the
|
||||
* timer.
|
||||
*
|
||||
* @method setCallback
|
||||
* @param {function} callback The callback function
|
||||
* @return {object} this
|
||||
*/
|
||||
Timer.prototype.setCallback = function(callback) {
|
||||
var BackoffTimer = function(callback, backoffFunction) {
|
||||
this.callback = callback;
|
||||
|
||||
return this;
|
||||
this.backOffFunction = backoffFunction;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the callback function for this timer.
|
||||
*
|
||||
* @method getCallback
|
||||
* @return {function}
|
||||
* @type {function} callback The function to execute after each tick
|
||||
*/
|
||||
Timer.prototype.getCallback = function() {
|
||||
return this.callback;
|
||||
};
|
||||
BackoffTimer.prototype.callback = null;
|
||||
|
||||
/**
|
||||
* Set the function to be used when calculating the back off time
|
||||
* for each tick of the timer.
|
||||
*
|
||||
* The back off function will be given two parameters: the current
|
||||
* time and an array containing all previous times.
|
||||
*
|
||||
* @method setBackOffFunction
|
||||
* @param {function} backOffFunction The function to calculate back off times
|
||||
* @return {object} this
|
||||
* @type {function} backoffFunction The function to determine what the next timeout value should be
|
||||
*/
|
||||
Timer.prototype.setBackOffFunction = function(backOffFunction) {
|
||||
this.backOffFunction = backOffFunction;
|
||||
|
||||
return this;
|
||||
};
|
||||
BackoffTimer.prototype.backOffFunction = null;
|
||||
|
||||
/**
|
||||
* Get the current back off function.
|
||||
*
|
||||
* @method getBackOffFunction
|
||||
* @return {function}
|
||||
* @type {int} time The timeout value to use
|
||||
*/
|
||||
Timer.prototype.getBackOffFunction = function() {
|
||||
return this.backOffFunction;
|
||||
};
|
||||
BackoffTimer.prototype.time = null;
|
||||
|
||||
/**
|
||||
* @type {numeric} timeout The timeout identifier
|
||||
*/
|
||||
BackoffTimer.prototype.timeout = null;
|
||||
|
||||
/**
|
||||
* Generate the next timeout in the back off time sequence
|
||||
@@ -122,13 +66,8 @@ define(function() {
|
||||
* @method generateNextTime
|
||||
* @return {int} The new timeout value (in milliseconds)
|
||||
*/
|
||||
Timer.prototype.generateNextTime = function() {
|
||||
var newTime = this.getBackOffFunction().call(
|
||||
this.getBackOffFunction(),
|
||||
this.time,
|
||||
this.previousTimes
|
||||
);
|
||||
this.previousTimes.push(this.time);
|
||||
BackoffTimer.prototype.generateNextTime = function() {
|
||||
var newTime = this.backOffFunction(this.time);
|
||||
this.time = newTime;
|
||||
|
||||
return newTime;
|
||||
@@ -140,9 +79,8 @@ define(function() {
|
||||
* @method reset
|
||||
* @return {object} this
|
||||
*/
|
||||
Timer.prototype.reset = function() {
|
||||
BackoffTimer.prototype.reset = function() {
|
||||
this.time = null;
|
||||
this.previousTimes = [];
|
||||
this.stop();
|
||||
|
||||
return this;
|
||||
@@ -154,7 +92,7 @@ define(function() {
|
||||
* @method stop
|
||||
* @return {object} this
|
||||
*/
|
||||
Timer.prototype.stop = function() {
|
||||
BackoffTimer.prototype.stop = function() {
|
||||
if (this.timeout) {
|
||||
window.clearTimeout(this.timeout);
|
||||
this.timeout = null;
|
||||
@@ -175,12 +113,12 @@ define(function() {
|
||||
* @method start
|
||||
* @return {object} this
|
||||
*/
|
||||
Timer.prototype.start = function() {
|
||||
BackoffTimer.prototype.start = function() {
|
||||
// If we haven't already started.
|
||||
if (!this.timeout) {
|
||||
var time = this.generateNextTime();
|
||||
this.timeout = window.setTimeout(function() {
|
||||
this.getCallback().call();
|
||||
this.callback();
|
||||
// Clear the existing timer.
|
||||
this.stop();
|
||||
// Start the next timer.
|
||||
@@ -198,9 +136,40 @@ define(function() {
|
||||
* @method restart
|
||||
* @return {object} this
|
||||
*/
|
||||
Timer.prototype.restart = function() {
|
||||
BackoffTimer.prototype.restart = function() {
|
||||
return this.reset().start();
|
||||
};
|
||||
|
||||
return Timer;
|
||||
/**
|
||||
* Returns an incremental function for the timer.
|
||||
*
|
||||
* @param {int} minamount The minimum amount of time we wait before checking
|
||||
* @param {int} incrementamount The amount to increment the timer by
|
||||
* @param {int} maxamount The max amount to ever increment to
|
||||
* @param {int} timeoutamount The timeout to use once we reach the max amount
|
||||
* @return {function}
|
||||
*/
|
||||
BackoffTimer.getIncrementalCallback = function(minamount, incrementamount, maxamount, timeoutamount) {
|
||||
|
||||
/**
|
||||
* An incremental function for the timer.
|
||||
*
|
||||
* @param {(int|null)} time The current timeout value or null if none set
|
||||
* @return {int} The new timeout value
|
||||
*/
|
||||
return function(time) {
|
||||
if (!time) {
|
||||
return minamount;
|
||||
}
|
||||
|
||||
// Don't go over the max amount.
|
||||
if (time + incrementamount > maxamount) {
|
||||
return timeoutamount;
|
||||
}
|
||||
|
||||
return time + incrementamount;
|
||||
};
|
||||
};
|
||||
|
||||
return BackoffTimer;
|
||||
});
|
||||
|
Reference in New Issue
Block a user