| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
mw.echo.api.PromisePrioritizer = function MwEchoApiPromisePrioritizer() { |
| 15 |
this.deferred = $.Deferred(); |
| 16 |
this.promise = null; |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
OO.initClass( mw.echo.api.PromisePrioritizer ); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
mw.echo.api.PromisePrioritizer.prototype.prioritize = function ( promise ) { |
| 31 |
const previousPromise = this.promise; |
| 32 |
|
| 33 |
promise |
| 34 |
.then( |
| 35 |
this.setSuccess.bind( this, promise ), |
| 36 |
this.setFailure.bind( this, promise ) |
| 37 |
); |
| 38 |
this.promise = promise; |
| 39 |
|
| 40 |
if ( previousPromise && previousPromise.abort ) { |
| 41 |
previousPromise.abort(); |
| 42 |
} |
| 43 |
|
| 44 |
return this.deferred.promise(); |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
mw.echo.api.PromisePrioritizer.prototype.setSuccess = function ( promise ) { |
| 56 |
if ( this.promise === promise ) { |
| 57 |
this.promise.done( ( ...args ) => { |
| 58 |
this.deferred.resolve( ...args ); |
| 59 |
|
| 60 |
this.promise = null; |
| 61 |
this.deferred = $.Deferred(); |
| 62 |
} ); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
mw.echo.api.PromisePrioritizer.prototype.setFailure = function ( promise ) { |
| 75 |
if ( this.promise === promise ) { |
| 76 |
this.promise.fail( ( ...args ) => { |
| 77 |
this.deferred.reject( ...args ); |
| 78 |
|
| 79 |
this.promise = null; |
| 80 |
this.deferred = $.Deferred(); |
| 81 |
} ); |
| 82 |
} |
| 83 |
}; |
| 84 |
}() ); |