From 2da9c20c08be3103c66bc05775b95430b301c3a7 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 20 Jan 2022 11:49:59 -0500 Subject: [PATCH] Add ajaxQueue --- web/js/ajaxQueue.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 web/js/ajaxQueue.js diff --git a/web/js/ajaxQueue.js b/web/js/ajaxQueue.js new file mode 100644 index 000000000..c706fb978 --- /dev/null +++ b/web/js/ajaxQueue.js @@ -0,0 +1,48 @@ +// See https://github.com/gnarf/jquery-ajaxQueue for license and copyright + +(function($) { + +// jQuery on an empty object, we are going to use this as our Queue +var ajaxQueue = $({}); + +$.ajaxQueue = function( ajaxOpts ) { + var jqXHR, + dfd = $.Deferred(), + promise = dfd.promise(); + + // run the actual query + function doRequest( next ) { + jqXHR = $.ajax( ajaxOpts ); + jqXHR.done( dfd.resolve ) + .fail( dfd.reject ) + .then( next, next ); + } + + // queue our ajax request + ajaxQueue.queue( doRequest ); + + // add the abort method + promise.abort = function( statusText ) { + + // proxy abort to the jqXHR if it is active + if ( jqXHR ) { + return jqXHR.abort( statusText ); + } + + // if there wasn't already a jqXHR we need to remove from queue + var queue = ajaxQueue.queue(), + index = $.inArray( doRequest, queue ); + + if ( index > -1 ) { + queue.splice( index, 1 ); + } + + // and then reject the deferred + dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] ); + return promise; + }; + + return promise; +}; + +})(jQuery);