1// AjaxQ jQuery Plugin 2// Copyright (c) 2012 Foliotek Inc. 3// MIT License 4// https://github.com/Foliotek/ajaxq 5// Uses CommonJS, AMD or browser globals to create a jQuery plugin. 6 7(function (factory) { 8 if (typeof define === 'function' && define.amd) { 9 // AMD. Register as an anonymous module. 10 define(['jquery'], factory); 11 } else if (typeof module === 'object' && module.exports) { 12 // Node/CommonJS 13 module.exports = factory(require('jquery')); 14 } else { 15 // Browser globals 16 factory(jQuery); 17 } 18}(function ($) { 19 var queues = {}; 20 var activeReqs = {}; 21 22 // Register an $.ajaxq function, which follows the $.ajax interface, but allows a queue name which will force only one request per queue to fire. 23 // opts can be the regular $.ajax settings plainObject, or a callback returning the settings object, to be evaluated just prior to the actual call to $.ajax. 24 $.ajaxq = function(qname, opts) { 25 26 if (typeof opts === "undefined") { 27 throw ("AjaxQ: queue name is not provided"); 28 } 29 30 // Will return a Deferred promise object extended with success/error/callback, so that this function matches the interface of $.ajax 31 var deferred = $.Deferred(), 32 promise = deferred.promise(); 33 34 promise.success = promise.done; 35 promise.error = promise.fail; 36 promise.complete = promise.always; 37 38 // Check whether options are to be evaluated at call time or not. 39 var deferredOpts = typeof opts === 'function'; 40 // Create a deep copy of the arguments, and enqueue this request. 41 var clonedOptions = !deferredOpts ? $.extend(true, {}, opts) : null; 42 enqueue(function() { 43 // Send off the ajax request now that the item has been removed from the queue 44 var jqXHR = $.ajax.apply(window, [deferredOpts ? opts() : clonedOptions]); 45 46 // Notify the returned deferred object with the correct context when the jqXHR is done or fails 47 // Note that 'always' will automatically be fired once one of these are called: http://api.jquery.com/category/deferred-object/. 48 jqXHR.done(function() { 49 deferred.resolve.apply(this, arguments); 50 }); 51 jqXHR.fail(function() { 52 deferred.reject.apply(this, arguments); 53 }); 54 55 jqXHR.always(dequeue); // make sure to dequeue the next request AFTER the done and fail callbacks are fired 56 57 return jqXHR; 58 }); 59 60 return promise; 61 62 63 // If there is no queue, create an empty one and instantly process this item. 64 // Otherwise, just add this item onto it for later processing. 65 function enqueue(cb) { 66 if (!queues[qname]) { 67 queues[qname] = []; 68 var xhr = cb(); 69 activeReqs[qname] = xhr; 70 } 71 else { 72 queues[qname].push(cb); 73 } 74 } 75 76 // Remove the next callback from the queue and fire it off. 77 // If the queue was empty (this was the last item), delete it from memory so the next one can be instantly processed. 78 function dequeue() { 79 if (!queues[qname]) { 80 return; 81 } 82 var nextCallback = queues[qname].shift(); 83 if (nextCallback) { 84 var xhr = nextCallback(); 85 activeReqs[qname] = xhr; 86 } 87 else { 88 delete queues[qname]; 89 delete activeReqs[qname]; 90 } 91 } 92 }; 93 94 // Register a $.postq and $.getq method to provide shortcuts for $.get and $.post 95 // Copied from jQuery source to make sure the functions share the same defaults as $.get and $.post. 96 $.each( [ "getq", "postq" ], function( i, method ) { 97 $[ method ] = function( qname, url, data, callback, type ) { 98 99 if ( $.isFunction( data ) ) { 100 type = type || callback; 101 callback = data; 102 data = undefined; 103 } 104 105 return $.ajaxq(qname, { 106 type: method === "postq" ? "post" : "get", 107 url: url, 108 data: data, 109 success: callback, 110 dataType: type 111 }); 112 }; 113 });
114 115 var isQueueRunning = function(qname) { 116 return (queues.hasOwnProperty(qname) && queues[qname].length > 0) || activeReqs.hasOwnProperty(qname); 117 }; 118 119 var isAnyQueueRunning = function() { 120 for (var i in queues) { 121 if (isQueueRunning(i)) return true; 122 } 123 return false; 124 }; 125 126 $.ajaxq.isRunning = function(qname) { 127 if (qname) return isQueueRunning(qname); 128 else return isAnyQueueRunning(); 129 }; 130 131 $.ajaxq.getActiveRequest = function(qname) { 132 if (!qname) throw ("AjaxQ: queue name is required"); 133 134 return activeReqs[qname]; 135 }; 136 137 $.ajaxq.abort = function(qname) { 138 if (!qname) throw ("AjaxQ: queue name is required"); 139 140 var current = $.ajaxq.getActiveRequest(qname); 141 delete queues[qname]; 142 delete activeReqs[qname]; 143 if (current) current.abort(); 144 }; 145 146 $.ajaxq.clear = function(qname) { 147 if (!qname) { 148 for (var i in queues) { 149 if (queues.hasOwnProperty(i)) { 150 queues[i] = []; 151 } 152 } 153 } 154 else { 155 if (queues[qname]) { 156 queues[qname] = []; 157 } 158 } 159 }; 160 161}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.