2015-11-11 23:22:36 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
/**
|
|
|
|
* Network handler for echo notifications. Manages multiple APIHandlers
|
|
|
|
* according to their sources.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration object
|
|
|
|
* @cfg {string} [type="alert"] Notification type
|
|
|
|
* @cfg {Object} [baseParams] The base params to send to the
|
|
|
|
* APIs with every fetch notifications process.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NetworkHandler = function MwEchoDmNetworkHandler( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
this.type = config.type || 'alert';
|
|
|
|
this.baseParams = config.baseParams || {};
|
|
|
|
this.handlers = {};
|
|
|
|
|
|
|
|
// Add initial local handler
|
|
|
|
this.addApiHandler( 'local', {} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Setup */
|
|
|
|
|
|
|
|
OO.initClass( mw.echo.dm.NetworkHandler );
|
|
|
|
OO.mixinClass( mw.echo.dm.NetworkHandler, OO.EventEmitter );
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/* 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.dm.NetworkHandler.static.waitForAllPromises = function ( promiseArray ) {
|
|
|
|
var i,
|
|
|
|
promises = promiseArray.slice( 0 ),
|
|
|
|
counter = 0,
|
|
|
|
deferred = $.Deferred(),
|
|
|
|
countPromises = function () {
|
|
|
|
counter++;
|
|
|
|
if ( counter === promises.length ) {
|
|
|
|
deferred.resolve( promises );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-28 23:12:37 +00:00
|
|
|
if ( !promiseArray.length ) {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
for ( i = 0; i < promises.length; i++ ) {
|
|
|
|
promises[ i ].always( countPromises );
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* Fetch notifications from several sources
|
|
|
|
*
|
|
|
|
* @param {string[]} sourceArray Sources
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an array of promises for
|
|
|
|
* fetchNotifications per each given source in the source array.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NetworkHandler.prototype.fetchNotificationGroups = function ( sourceArray ) {
|
|
|
|
var i, promise,
|
|
|
|
promises = [];
|
|
|
|
|
|
|
|
for ( i = 0; i < sourceArray.length; i++ ) {
|
|
|
|
promise = this.getApiHandler( sourceArray[ i ] ).fetchNotifications();
|
|
|
|
promises.push( promise );
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.constructor.static.waitForAllPromises( promises );
|
|
|
|
};
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
/**
|
|
|
|
* Get the API handler that matches the symbolic name
|
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name of the API handler
|
|
|
|
* @return {mw.echo.dm.APIHandler|undefined} API handler, if exists
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NetworkHandler.prototype.getApiHandler = function ( name ) {
|
|
|
|
return this.handlers[ name ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an API handler
|
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name
|
|
|
|
* @param {Object} config Configuration details
|
2015-11-25 04:07:54 +00:00
|
|
|
* @param {boolean} isForeign Is a foreign API
|
2015-11-11 23:22:36 +00:00
|
|
|
* @throws {Error} If no URL was given for a foreign API
|
|
|
|
*/
|
2015-11-25 04:07:54 +00:00
|
|
|
mw.echo.dm.NetworkHandler.prototype.addApiHandler = function ( name, config, isForeign ) {
|
2015-11-11 23:22:36 +00:00
|
|
|
var apiConfig;
|
|
|
|
|
|
|
|
if ( !this.handlers[ name ] ) {
|
|
|
|
apiConfig = $.extend( true, {}, { baseParams: this.baseParams, type: this.getType() }, config );
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
if ( isForeign ) {
|
2015-11-11 23:22:36 +00:00
|
|
|
if ( !config.url ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
throw new Error( 'Foreign APIs must have a valid url.' );
|
2015-11-11 23:22:36 +00:00
|
|
|
}
|
|
|
|
this.addCustomApiHandler( name, new mw.echo.dm.ForeignAPIHandler( config.url, apiConfig ) );
|
|
|
|
} else {
|
|
|
|
this.addCustomApiHandler( name, new mw.echo.dm.LocalAPIHandler( apiConfig ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a custom API handler by passing in an instance of an mw.echo.dm.APIHandler subclass directly.
|
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name
|
|
|
|
* @param {mw.echo.dm.APIHandler} handler Handler object
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NetworkHandler.prototype.addCustomApiHandler = function ( name, handler ) {
|
|
|
|
if ( !this.handlers[ name ] ) {
|
|
|
|
this.handlers[ name ] = handler;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of notifications this network handler is associated with
|
|
|
|
*
|
|
|
|
* @return {string} Notification type
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NetworkHandler.prototype.getType = function () {
|
|
|
|
return this.type;
|
|
|
|
};
|
|
|
|
|
|
|
|
} )( mediaWiki, jQuery );
|