cuzi / RequestQueue

Published:

Version: 6+5d00a23 updated

Summary: A simple queue for GM.xmlHttpRequest, GM_xmlhttpRequests or other async functions

Homepage: https://github.com/cvzi/RequestQueue/

License: MIT

A simple queue for GM_xmlhttpRequests or other async functions

  • Version 6
  • Support for Greasemonkey version 4 and Firefox version 57
  • Version 5
  • New methods: resetTotal(), hasReachedTotal(), hasRunning()
  • Version 4
  • Fix for XMLHttpRequest.abort()
  • Version 3
  • Fix for Chrome
  • Version 2
  • Fix for Firefox 36.0
  • Version 1
  • initial version

new RequestQueue([maxParallel[, maxTotal]])

  • maxParallel
    defaults to 1 i.e. no paralllel requests
  • maxTotal
    defaults to unlimited

RequestQueue.prototype.add(req[, fun[, thisArg]])
Schedule a request

  • req
    An object that is the only argument of fun. This will be extended and therefore must be mutable.
    At least one of the following methods onload, onerror or onabort must be implemented.
  • fun
    defaults to GM_xmlhttpRequest
  • thisArg
    The value of this provided for the call to fun. Keep strict mode in mind!

Results in a call like this: thisArg.fun(req)

RequestQueue.prototype.abortRunning()
For all scheduled requests that are currently running: result = thisArg.fun(req)
this will call result.abort()
For GM_xmlhttpRequest this will subsequently fire an onabort event

RequestQueue.prototype.abortPending()
Clear the list of pending requests i.e. requests that were not sent yet.

RequestQueue.prototype.abort()
Abort both running and pending requests

Example:

var rq = new RequestQueue(1); // 1 -> Allow no parallel requests
rq.add({
  method: "GET",
  url: "http://www.example.org/page1",
  onload: function (response){
    console.log("onload 1");
  }
});
rq.add({
  method: "GET",
  url: "http://www.example.org/page2",
  onload: function (response){
    console.log("onload 2");
  }
});

The request to page 1 will be sent immediately.
The request to page 2 will be sent after the the first request has finished, but before the first onload event is called.

Rating: 3

3 Votes
-3