| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MultimediaViewer. |
| 3 | * |
| 4 | * MultimediaViewer is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * MultimediaViewer is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * A queue which holds a list of tasks (functions). The tasks will be executed in order, |
| 20 | * each starting when the previous one has finished (or failed). |
| 21 | */ |
| 22 | class TaskQueue { |
| 23 | constructor() { |
| 24 | /** |
| 25 | * The list of functions to execute. |
| 26 | * |
| 27 | * @protected |
| 28 | * @property {Array} |
| 29 | */ |
| 30 | this.queue = []; |
| 31 | |
| 32 | /** |
| 33 | * State of the task queue (running, finished etc) |
| 34 | * |
| 35 | * @protected |
| 36 | * @property {TaskQueue.State} |
| 37 | */ |
| 38 | this.state = TaskQueue.State.NOT_STARTED; |
| 39 | |
| 40 | /** |
| 41 | * A deferred which shows the state of the queue. |
| 42 | * |
| 43 | * @protected |
| 44 | * @property {jQuery.Deferred} |
| 45 | */ |
| 46 | this.deferred = $.Deferred(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Adds a task. The task should be a function which returns a promise. (Other return values are |
| 51 | * permitted, and will be taken to mean that the task has finished already.) The next task will |
| 52 | * start when the promise resolves (or rejects). |
| 53 | * |
| 54 | * Tasks can only be added before the queue is first executed. |
| 55 | * |
| 56 | * @param {function(): any} task |
| 57 | */ |
| 58 | push( task ) { |
| 59 | if ( this.state !== TaskQueue.State.NOT_STARTED ) { |
| 60 | throw new Error( 'Task queue already started!' ); |
| 61 | } |
| 62 | this.queue.push( task ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Execute the queue. The tasks will be performed in order. No more tasks can be added to the |
| 67 | * queue. |
| 68 | * |
| 69 | * @return {jQuery.Promise} a promise which will resolve when the queue execution is finished, |
| 70 | * or reject when it is cancelled. |
| 71 | */ |
| 72 | execute() { |
| 73 | if ( this.state === TaskQueue.State.NOT_STARTED ) { |
| 74 | this.state = TaskQueue.State.RUNNING; |
| 75 | this.runNextTask( 0, $.Deferred().resolve() ); |
| 76 | } |
| 77 | |
| 78 | return this.deferred; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Runs the next task once the current one has finished. |
| 83 | * |
| 84 | * @param {number} index |
| 85 | * @param {jQuery.Promise} currentTask |
| 86 | */ |
| 87 | runNextTask( index, currentTask ) { |
| 88 | const taskQueue = this; |
| 89 | |
| 90 | function handleThen() { |
| 91 | if ( !taskQueue.queue[ index ] ) { |
| 92 | taskQueue.state = TaskQueue.State.FINISHED; |
| 93 | taskQueue.queue = []; // just to be sure there are no memory leaks |
| 94 | taskQueue.deferred.resolve(); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | taskQueue.runNextTask( index + 1, $.when( taskQueue.queue[ index ]() ) ); |
| 99 | } |
| 100 | |
| 101 | if ( this.state !== TaskQueue.State.RUNNING ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | currentTask.then( handleThen, handleThen ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Cancel the queue. No more tasks will be executed. |
| 110 | */ |
| 111 | cancel() { |
| 112 | this.state = TaskQueue.State.CANCELLED; |
| 113 | this.queue = []; // just to be sure there are no memory leaks |
| 114 | this.deferred.reject(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * State of the task queue (running, finished etc) |
| 120 | * |
| 121 | * @enum {string} TaskQueue.State |
| 122 | */ |
| 123 | TaskQueue.State = { |
| 124 | /** not executed yet, tasks can still be added */ |
| 125 | NOT_STARTED: 'not_started', |
| 126 | |
| 127 | /** some task is being executed */ |
| 128 | RUNNING: 'running', |
| 129 | |
| 130 | /** all tasks finished, queue can be discarded */ |
| 131 | FINISHED: 'finished', |
| 132 | |
| 133 | /** cancel() function has been called, queue can be discarded */ |
| 134 | CANCELLED: 'cancelled' |
| 135 | }; |
| 136 | |
| 137 | module.exports = TaskQueue; |