2014-02-10 21:24:19 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function ( mw, $ ) {
|
|
|
|
var tqp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A queue which holds a list of tasks (functions). The tasks will be executed in order,
|
|
|
|
* each starting when the previous one has finished (or failed).
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-19 17:38:55 +00:00
|
|
|
* @class mw.mmv.model.TaskQueue
|
2014-02-10 21:24:19 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function TaskQueue() {
|
|
|
|
/**
|
|
|
|
* The list of functions to execute.
|
|
|
|
* @protected
|
|
|
|
* @property {Array.<function()>}
|
|
|
|
*/
|
|
|
|
this.queue = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* State of the task queue (running, finished etc)
|
|
|
|
* @protected
|
2014-02-19 17:38:55 +00:00
|
|
|
* @property {mw.mmv.model.TaskQueue.State}
|
2014-02-10 21:24:19 +00:00
|
|
|
*/
|
|
|
|
this.state = TaskQueue.State.NOT_STARTED;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A deferred which shows the state of the queue.
|
|
|
|
* @protected
|
2014-02-19 17:38:55 +00:00
|
|
|
* @property {jQuery.Deferred}
|
2014-02-10 21:24:19 +00:00
|
|
|
*/
|
|
|
|
this.deferred = $.Deferred();
|
|
|
|
}
|
|
|
|
|
|
|
|
tqp = TaskQueue.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a task. The task should be a function which returns a promise. (Other return values are
|
|
|
|
* permitted, and will be taken to mean that the task has finished already.) The next task will
|
|
|
|
* start when the promise resolves (or rejects).
|
|
|
|
*
|
|
|
|
* Tasks can only be added before the queue is first executed.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-10 21:24:19 +00:00
|
|
|
* @param {function()} task
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
tqp.push = function ( task ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
if ( this.state !== TaskQueue.State.NOT_STARTED ) {
|
2017-05-03 09:15:05 +00:00
|
|
|
throw new Error( 'Task queue already started!' );
|
2014-02-10 21:24:19 +00:00
|
|
|
}
|
|
|
|
this.queue.push( task );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the queue. The tasks will be performed in order. No more tasks can be added to the
|
|
|
|
* queue.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-10 21:24:19 +00:00
|
|
|
* @return {jQuery.Promise} a promise which will resolve when the queue execution is finished,
|
|
|
|
* or reject when it is cancelled.
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
tqp.execute = function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
if ( this.state === TaskQueue.State.NOT_STARTED ) {
|
|
|
|
this.state = TaskQueue.State.RUNNING;
|
|
|
|
this.runNextTask( 0, $.Deferred().resolve() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.deferred;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the next task once the current one has finished.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-10 21:24:19 +00:00
|
|
|
* @param {number} index
|
|
|
|
* @param {jQuery.Promise} currentTask
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
tqp.runNextTask = function ( index, currentTask ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = this;
|
|
|
|
|
|
|
|
function handleThen() {
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( !taskQueue.queue[ index ] ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.state = TaskQueue.State.FINISHED;
|
|
|
|
taskQueue.queue = []; // just to be sure there are no memory leaks
|
|
|
|
taskQueue.deferred.resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
taskQueue.runNextTask( index + 1, $.when( taskQueue.queue[ index ]() ) );
|
2014-02-10 21:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.state !== TaskQueue.State.RUNNING ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTask.then( handleThen, handleThen );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel the queue. No more tasks will be executed.
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
tqp.cancel = function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
this.state = TaskQueue.State.CANCELLED;
|
|
|
|
this.queue = []; // just to be sure there are no memory leaks
|
|
|
|
this.deferred.reject();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* State of the task queue (running, finished etc)
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-10 21:24:19 +00:00
|
|
|
* @enum {string} mw.mmv.model.TaskQueue.State
|
|
|
|
*/
|
|
|
|
TaskQueue.State = {
|
|
|
|
/** not executed yet, tasks can still be added */
|
|
|
|
NOT_STARTED: 'not_started',
|
|
|
|
|
|
|
|
/** some task is being executed */
|
|
|
|
RUNNING: 'running',
|
|
|
|
|
|
|
|
/** all tasks finished, queue can be discarded */
|
|
|
|
FINISHED: 'finished',
|
|
|
|
|
|
|
|
/** cancel() function has been called, queue can be discarded */
|
|
|
|
CANCELLED: 'cancelled'
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.mmv.model.TaskQueue = TaskQueue;
|
|
|
|
}( mediaWiki, jQuery ) );
|