summaryrefslogtreecommitdiff
blob: 4875a0019b5ae1bdb392db069aba708b4658a42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
( function ( mw, $ ) {
	/**
	 * Network handler for echo notifications. Manages multiple APIHandlers
	 * according to their sources.
	 *
	 * @class
	 *
	 * @constructor
	 * @param {Object} config Configuration options
	 * @cfg {number} limit Number of notifications to fetch
	 */
	mw.echo.api.NetworkHandler = function MwEchoApiNetworkHandler( config ) {
		config = config || {};

		this.handlers = {};

		// Add initial local handler
		this.setApiHandler( 'local', new mw.echo.api.LocalAPIHandler( { limit: config.limit } ) );
	};

	/* 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 ) {
		var i,
			promises = promiseArray.slice( 0 ),
			counter = 0,
			deferred = $.Deferred(),
			countPromises = function () {
				counter++;
				if ( counter === promises.length ) {
					deferred.resolve( promises );
				}
			};

		if ( !promiseArray.length ) {
			deferred.resolve();
		}

		for ( i = 0; i < promises.length; i++ ) {
			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
	 * @return {mw.echo.api.APIHandler|undefined} API handler, if exists
	 */
	mw.echo.api.NetworkHandler.prototype.getApiHandler = function ( name ) {
		return this.handlers[ name ];
	};

	/**
	 * Set an API handler by passing in an instance of an mw.echo.api.APIHandler subclass directly.
	 *
	 * @param {string} name Symbolic name
	 * @param {mw.echo.api.APIHandler} handler Handler object
	 * @throws {Error} If handler already exists
	 */
	mw.echo.api.NetworkHandler.prototype.setApiHandler = function ( name, handler ) {
		this.handlers[ name ] = handler;
	};
}( mediaWiki, jQuery ) );