2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-01-15 22:11:33 +00:00
|
|
|
/**
|
|
|
|
* Network handler for echo notifications. Manages multiple APIHandlers
|
|
|
|
* according to their sources.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @constructor
|
2016-03-16 22:47:20 +00:00
|
|
|
* @param {Object} config Configuration options
|
2024-05-27 01:43:21 +00:00
|
|
|
* @param {number} config.limit Number of notifications to fetch
|
2016-01-15 22:11:33 +00:00
|
|
|
*/
|
2016-03-16 22:47:20 +00:00
|
|
|
mw.echo.api.NetworkHandler = function MwEchoApiNetworkHandler( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
2016-01-15 22:11:33 +00:00
|
|
|
this.handlers = {};
|
|
|
|
|
|
|
|
// Add initial local handler
|
2016-03-16 22:47:20 +00:00
|
|
|
this.setApiHandler( 'local', new mw.echo.api.LocalAPIHandler( { limit: config.limit } ) );
|
2016-01-15 22:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Setup */
|
|
|
|
|
|
|
|
OO.initClass( mw.echo.api.NetworkHandler );
|
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
/**
|
|
|
|
* Wait for all promises to finish either with a resolve or reject and
|
|
|
|
* return them to the caller once they do.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Promise[]} promiseArray An array of promises
|
|
|
|
* @return {jQuery.Promise} A promise that resolves when all the promises
|
|
|
|
* finished with some resolution or rejection.
|
|
|
|
*/
|
|
|
|
mw.echo.api.NetworkHandler.static.waitForAllPromises = function ( promiseArray ) {
|
2024-06-03 12:21:23 +00:00
|
|
|
let counter = 0;
|
|
|
|
const promises = promiseArray.slice( 0 ),
|
2016-01-15 22:11:33 +00:00
|
|
|
deferred = $.Deferred(),
|
|
|
|
countPromises = function () {
|
|
|
|
counter++;
|
|
|
|
if ( counter === promises.length ) {
|
|
|
|
deferred.resolve( promises );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( !promiseArray.length ) {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( let i = 0; i < promises.length; i++ ) {
|
2016-01-15 22:11:33 +00:00
|
|
|
promises[ i ].always( countPromises );
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the API handler that matches the symbolic name
|
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name of the API handler
|
2016-05-11 20:33:17 +00:00
|
|
|
* @return {mw.echo.api.APIHandler|undefined} API handler, if exists
|
2016-01-15 22:11:33 +00:00
|
|
|
*/
|
|
|
|
mw.echo.api.NetworkHandler.prototype.getApiHandler = function ( name ) {
|
|
|
|
return this.handlers[ name ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-03-16 22:47:20 +00:00
|
|
|
* Set an API handler by passing in an instance of an mw.echo.api.APIHandler subclass directly.
|
2016-01-15 22:11:33 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name
|
|
|
|
* @param {mw.echo.api.APIHandler} handler Handler object
|
2016-05-06 13:24:14 +00:00
|
|
|
* @throws {Error} If handler already exists
|
2016-01-15 22:11:33 +00:00
|
|
|
*/
|
2016-05-06 13:24:14 +00:00
|
|
|
mw.echo.api.NetworkHandler.prototype.setApiHandler = function ( name, handler ) {
|
|
|
|
this.handlers[ name ] = handler;
|
2016-01-15 22:11:33 +00:00
|
|
|
};
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|