Debian Codegraph @

extensions/Echo/modules/api/mw.echo.api.NetworkHandler.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Network handler for echo notifications. Manages multiple APIHandlers
4 * according to their sources.
5 *
6 * @class
7 *
8 * @constructor
9 * @param {Object} config Configuration options
10 * @param {number} config.limit Number of notifications to fetch
11 */
12 mw.echo.api.NetworkHandler = function MwEchoApiNetworkHandler( config ) {
13 config = config || {};
14
15 this.handlers = {};
16
17 // Add initial local handler
18 this.setApiHandler( 'local', new mw.echo.api.LocalAPIHandler( { limit: config.limit } ) );
19 };
20
21 /* Setup */
22
23 OO.initClass( mw.echo.api.NetworkHandler );
24
25 /* Static methods */
26 /**
27 * Wait for all promises to finish either with a resolve or reject and
28 * return them to the caller once they do.
29 *
30 * @param {jQuery.Promise[]} promiseArray An array of promises
31 * @return {jQuery.Promise} A promise that resolves when all the promises
32 * finished with some resolution or rejection.
33 */
34 mw.echo.api.NetworkHandler.static.waitForAllPromises = function ( promiseArray ) {
35 let counter = 0;
36 const promises = promiseArray.slice( 0 ),
37 deferred = $.Deferred(),
38 countPromises = function () {
39 counter++;
40 if ( counter === promises.length ) {
41 deferred.resolve( promises );
42 }
43 };
44
45 if ( !promiseArray.length ) {
46 deferred.resolve();
47 }
48
49 for ( let i = 0; i < promises.length; i++ ) {
50 promises[ i ].always( countPromises );
51 }
52
53 return deferred.promise();
54 };
55
56 /* Methods */
57
58 /**
59 * Get the API handler that matches the symbolic name
60 *
61 * @param {string} name Symbolic name of the API handler
62 * @return {mw.echo.api.APIHandler|undefined} API handler, if exists
63 */
64 mw.echo.api.NetworkHandler.prototype.getApiHandler = function ( name ) {
65 return this.handlers[ name ];
66 };
67
68 /**
69 * Set an API handler by passing in an instance of an mw.echo.api.APIHandler subclass directly.
70 *
71 * @param {string} name Symbolic name
72 * @param {mw.echo.api.APIHandler} handler Handler object
73 * @throws {Error} If handler already exists
74 */
75 mw.echo.api.NetworkHandler.prototype.setApiHandler = function ( name, handler ) {
76 this.handlers[ name ] = handler;
77 };
78 }() );