summaryrefslogtreecommitdiff
blob: 76b69dca0273e70d2f98a1dddc52b05fb0c272ac (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
( function ( $ ) {
	'use strict';

	/**
	 * Call list of callbacks returning promises in serial order and returns a list of promises.
	 *
	 * @author Niklas Laxström, 2014
	 *
	 * @param {callable[]} list List of callbacks returning promises.
	 * @return {jQuery.promise}
	 */
	function ajaxDispatcher( list, maxRetries ) {
		maxRetries = maxRetries || 0;

		var deferred = $.Deferred();
		return $.when( helper( list, maxRetries ) )
			.then( function ( promises ) {
				return deferred.resolve( promises );
			} );
	}

	function helper( list, maxRetries ) {
		var first, rest, retries, retrier,
			deferred = $.Deferred();

		if ( list.length === 0 ) {
			deferred.resolve( [] );
			return deferred;
		}

		first = list.slice( 0, 1 )[0];
		rest = list.slice( 1 );

		retries = 0;
		retrier = function () {
			var promise = this;

			if ( promise.state() === 'rejected' ) {
				if ( retries < maxRetries ) {
					retries += 1;
					return first.call().always( retrier );
				}
			}

			if ( promise.state() !== 'pending' ) {
				helper( rest, maxRetries ).always( function ( promises ) {
					deferred.resolve( [].concat( promise, promises ) );
				} );
			}
		};

		first.call().always( retrier );

		return deferred;
	}

	$.extend( $, { ajaxDispatcher: ajaxDispatcher } );

}( jQuery ) );