Merge "Allow for overriding parameters in fetch notifications API request"

This commit is contained in:
jenkins-bot 2016-05-26 20:26:28 +00:00 committed by Gerrit Code Review
commit 76a391220d
3 changed files with 57 additions and 15 deletions

View file

@ -49,6 +49,8 @@
* Fetch notifications from the API.
*
* @param {string} type Notification type
* @param {Object} [overrideParams] An object defining parameters to override in the API
* fetching call.
* @return {jQuery.Promise} A promise that resolves with an object containing the
* notification items
*/
@ -60,9 +62,14 @@
*
* @param {string} type Notification type
* @param {string[]} [sources] An array of sources to query
* @param {Object} [overrideParams] An object defining parameters to override in the API
* fetching call.
* @return {jQuery.Promise} Promise that is resolved when notifications are
* fetched from the API.
*/
mw.echo.api.APIHandler.prototype.createNewFetchNotificationPromise = function ( type, sources ) {
var fetchingSource = 'local',
mw.echo.api.APIHandler.prototype.createNewFetchNotificationPromise = function ( type, sources, overrideParams ) {
var apiErrState, fetchNotifPromise,
fetchingSource = 'local',
me = this,
params = $.extend( {
action: 'query',
@ -81,10 +88,24 @@
fetchingSource = 'foreign';
}
this.apiErrorState[ type ] = this.apiErrorState[ type ] || {};
this.apiErrorState[ type ][ fetchingSource ] = false;
// Initialize the nested value if it doesn't yet exist
this.fetchNotificationsPromise[ type ] = this.fetchNotificationsPromise[ type ] || {};
this.fetchNotificationsPromise[ type ][ fetchingSource ] = this.api.get( params )
me.apiErrorState[ type ] = me.apiErrorState[ type ] || {};
// Reset cached values
apiErrState = false;
this.fetchNotificationsPromise[ type ][ fetchingSource ] = null;
this.apiErrorState[ type ][ fetchingSource ] = false;
// Create the fetch promise
fetchNotifPromise = this.api.get( $.extend( true, params, overrideParams ) );
// Only cache promises that don't have override params in them
if ( !overrideParams ) {
this.fetchNotificationsPromise[ type ][ fetchingSource ] = fetchNotifPromise;
}
return fetchNotifPromise
.fail( function () {
// Mark API error state
me.apiErrorState[ type ][ fetchingSource ] = true;
@ -161,17 +182,20 @@
* Return the fetch notifications promise
*
* @param {string} type Notification type, 'alert', 'message' or 'all'
* @param {string|string[]} [sources] A name of a source or an array of sources to query
* @param {Object} [overrideParams] An object defining parameters to override in the API
* fetching call.
* @return {jQuery.Promise} Promise that is resolved when notifications are
* fetched from the API.
*/
mw.echo.api.APIHandler.prototype.getFetchNotificationPromise = function ( type, sources ) {
mw.echo.api.APIHandler.prototype.getFetchNotificationPromise = function ( type, sources, overrideParams ) {
var fetchingSource = 'local';
if ( Array.isArray( sources ) && sources.indexOf( 'local' ) === -1 ) {
fetchingSource = 'foreign';
}
if ( !this.fetchNotificationsPromise[ type ] || !this.fetchNotificationsPromise[ type ][ fetchingSource ] ) {
this.createNewFetchNotificationPromise( type, sources );
if ( overrideParams || !this.fetchNotificationsPromise[ type ] || !this.fetchNotificationsPromise[ type ][ fetchingSource ] ) {
this.createNewFetchNotificationPromise( type, sources, overrideParams );
}
return this.fetchNotificationsPromise[ type ][ fetchingSource ];
};

View file

@ -13,7 +13,7 @@
OO.initClass( mw.echo.api.EchoApi );
/**
* Register a set of sources.
* Register a set of foreign sources.
*
* @param {Object} sources Object mapping source names to config objects
*/
@ -24,6 +24,20 @@
}
};
/**
* Register a set of local sources.
*
* @param {string[]} sources An array of source names
*/
mw.echo.api.EchoApi.prototype.registerLocalSources = function ( sources ) {
var i,
localHandler = this.network.getApiHandler( 'local' );
for ( i = 0; i < sources.length; i++ ) {
this.network.setApiHandler( sources[ i ], localHandler );
}
};
/**
* Fetch notifications from the server based on type
*
@ -31,17 +45,19 @@
* @param {string|string[]} [sources] The source from which to fetch the notifications.
* If not given, the local notifications will be fetched.
* @param {boolean} [isForced] Force a refresh on the fetch notifications promise
* @param {Object} [overrideParams] An object defining parameters to override in the API
* fetching call.
* @return {jQuery.Promise} Promise that is resolved with all notifications for the
* requested types.
*/
mw.echo.api.EchoApi.prototype.fetchNotifications = function ( type, sources, isForced ) {
mw.echo.api.EchoApi.prototype.fetchNotifications = function ( type, sources, isForced, overrideParams ) {
sources = Array.isArray( sources ) ?
sources :
sources ?
[ sources ] :
null;
return this.network.getApiHandler( 'local' ).fetchNotifications( type, sources, isForced )
return this.network.getApiHandler( 'local' ).fetchNotifications( type, sources, isForced, overrideParams )
.then( function ( result ) {
return OO.getProp( result.query, 'notifications' );
} );

View file

@ -25,13 +25,15 @@
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.fetchNotifications = function ( type, source, isForced ) {
if ( isForced || this.isFetchingErrorState( type, source ) ) {
mw.echo.api.LocalAPIHandler.prototype.fetchNotifications = function ( type, source, isForced, overrideParams ) {
if ( overrideParams ) {
return this.createNewFetchNotificationPromise( type, source, overrideParams );
} else if ( overrideParams || this.isFetchingErrorState( type, source ) ) {
// Force new promise
this.createNewFetchNotificationPromise( type, source );
this.createNewFetchNotificationPromise( type, source, overrideParams );
}
return this.getFetchNotificationPromise( type, source );
return this.getFetchNotificationPromise( type, source, overrideParams );
};
/**