Skip to content

Instantly share code, notes, and snippets.

@eliseumds
Last active August 29, 2015 14:19
Show Gist options
  • Save eliseumds/1f01d9f017b356ba0286 to your computer and use it in GitHub Desktop.
Save eliseumds/1f01d9f017b356ba0286 to your computer and use it in GitHub Desktop.
Requests manager

Some reading before it: https://gist.github.com/eliseumds/8f970d8929e61b981438

How to cancel a bunch of requests?

var requestManager = new RequestManager();
var request1 = myApi.get('/resource1');
var request2 = myApi.get('/resource2');

requestManager.addAll([request1, request2]);

// you dont need them anymore? Cancel!
requestManager.abortAll();

You can identify bunches using KeyedRequestManager

var keyedRequestManager = new KeyedRequestManager();
var request1 = myApi.get('/resource1');
var request2 = myApi.get('/resource2');

var request3 = myApi.get('/resource3');
var request4 = myApi.get('/resource4');

keyedRequestManager.addAll('bunch1', [request1, request2]);
keyedRequestManager.addAll('bunch2', [request3, request4]);

// Just cancel bunch2
keyedRequestManager.abortAll('bunch2');
import _ from 'lodash';
class KeyedRequestManager {
// Map<String, Array<CancellablePromise>>
requests = {};
/*
* @param {string} key - bunch identifier
* @param {CancellablePromise} request
*/
abortAll(key) {
if (!(key in this.requests)) {
return;
}
this.requests[key].forEach(function(request) {
request.cancel();
});
delete this.requests[key];
}
/*
* @param {string} key - bunch identifier
* @param {CancellablePromise} request
*/
add(key, request) {
if (!(key in this.requests)) {
this.requests[key] = [];
}
this.requests[key].push(request);
var callback = this.remove.bind(this, key, request);
request.then(callback, callback);
}
/*
* @param {string} key - bunch identifier
* @param {Array<CancellablePromise>} requests
*/
addAll(key, requests) {
requests.forEach(this.add.bind(this, key));
}
/*
* @param {string} key - bunch identifier
* @return {number} number of requests that were added
*/
getCount(key) {
if (key in this.requests) {
return this.requests[key].length;
}
return 0;
}
/*
* @param {string} key - bunch identifier
* @param {CancellablePromise} request
*/
remove(key, request) {
if (!(key in this.requests)) {
return;
}
this.requests[key] = _.without(this.requests[key], request);
if (this.requests[key].length === 0) {
delete this.requests[key];
}
}
}
class RequestManager {
requests = [];
abortAll() {
this.requests.forEach(function(request) {
request.cancel();
});
this.requests = [];
}
/*
* @param {CancellablePromise} request
*/
add(request) {
this.requests.push(request);
var callback = this.remove.bind(this, request);
request.then(callback, callback);
}
/*
* @param {Array<CancellablePromise>} requests
*/
addAll(requests) {
requests.forEach(this.add, this);
}
/*
* @return {number} number of requests that were added
*/
getCount() {
return this.requests.length;
}
/*
* @param {CancellablePromise} request
*/
remove(request) {
this.requests = _.without(this.requests, request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment