# all files / src/mixins/ RequestManager.js

19.44% Statements 7/36
0% Branches 0/12
0% Functions 0/6
19.44% Lines 7/36

    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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126

    1×

    1×

    1×

    1×

    1×

    1×

    1×
     /**
     * RequestManager is a mixin that manages the lifecycle of a promise-backed request for a widget, such as
     * the {@link OO.ui.mixin.LookupElement}.
     *
     * @class
     * @abstract
     *
     * @constructor
     */
    OO.ui.mixin.RequestManager = function OoUiMixinRequestManager() {
    	this.requestCache = {};
    	this.requestQuery = null;
    	this.requestRequest = null;
    };

    /* Setup */

    OO.initClass( OO.ui.mixin.RequestManager );

    /**
     * Get request results for the current query.
     *
     * @return {jQuery.Promise} Promise object which will be passed response data as the first argument of
     *   the done event. If the request was aborted to make way for a subsequent request, this promise
     *   may not be rejected, depending on what jQuery feels like doing.
     */
    OO.ui.mixin.RequestManager.prototype.getRequestData = function () {
    	var widget = this,
    		value = this.getRequestQuery(),
    		deferred = $.Deferred(),
    		ourRequest;

    	this.abortRequest();
    	if ( Object.prototype.hasOwnProperty.call( this.requestCache, value ) ) {
    		deferred.resolve( this.requestCache[ value ] );
    	} else {
    		if ( this.pushPending ) {
    			this.pushPending();
    		}
    		this.requestQuery = value;
    		ourRequest = this.requestRequest = this.getRequest();
    		ourRequest
    			.always( function () {
    				// We need to pop pending even if this is an old request, otherwise
    				// the widget will remain pending forever.
    				// TODO: this assumes that an aborted request will fail or succeed soon after
    				// being aborted, or at least eventually. It would be nice if we could popPending()
    				// at abort time, but only if we knew that we hadn't already called popPending()
    				// for that request.
    				if ( widget.popPending ) {
    					widget.popPending();
    				}
    			} )
    			.done( function ( response ) {
    				// If this is an old request (and aborting it somehow caused it to still succeed),
    				// ignore its success completely
    				if ( ourRequest === widget.requestRequest ) {
    					widget.requestQuery = null;
    					widget.requestRequest = null;
    					widget.requestCache[ value ] = widget.getRequestCacheDataFromResponse( response );
    					deferred.resolve( widget.requestCache[ value ] );
    				}
    			} )
    			.fail( function () {
    				// If this is an old request (or a request failing because it's being aborted),
    				// ignore its failure completely
    				if ( ourRequest === widget.requestRequest ) {
    					widget.requestQuery = null;
    					widget.requestRequest = null;
    					deferred.reject();
    				}
    			} );
    	}
    	return deferred.promise();
    };

    /**
     * Abort the currently pending request, if any.
     *
     * @private
     */
    OO.ui.mixin.RequestManager.prototype.abortRequest = function () {
    	var oldRequest = this.requestRequest;
    	if ( oldRequest ) {
    		// First unset this.requestRequest to the fail handler will notice
    		// that the request is no longer current
    		this.requestRequest = null;
    		this.requestQuery = null;
    		oldRequest.abort();
    	}
    };

    /**
     * Get the query to be made.
     *
     * @protected
     * @method
     * @abstract
     * @return {string} query to be used
     */
    OO.ui.mixin.RequestManager.prototype.getRequestQuery = null;

    /**
     * Get a new request object of the current query value.
     *
     * @protected
     * @method
     * @abstract
     * @return {jQuery.Promise} jQuery AJAX object, or promise object with an .abort() method
     */
    OO.ui.mixin.RequestManager.prototype.getRequest = null;

    /**
     * Pre-process data returned by the request from #getRequest.
     *
     * The return value of this function will be cached, and any further queries for the given value
     * will use the cache rather than doing API requests.
     *
     * @protected
     * @method
     * @abstract
     * @param {Mixed} response Response from server
     * @return {Mixed} Cached result data
     */
    OO.ui.mixin.RequestManager.prototype.getRequestCacheDataFromResponse = null;