Skip to content

Instantly share code, notes, and snippets.

@salami162
Last active December 15, 2015 22:19
Show Gist options
  • Save salami162/5332700 to your computer and use it in GitHub Desktop.
Save salami162/5332700 to your computer and use it in GitHub Desktop.
How to implement batch request? How to user $.Deferred() to handle batch request limit? How to handle batch request collision?
/**
* If you have a widget on a page.
*/
var FacebookWidget = function (userName) {
this.username = userName;
this.user = new User(userName);
this.friends = new FriendList(userName);
this.activities = new ActivityList(userName);
}
var Limin = new FacebookWidget('limin');
// fetch user info
Limin.user.fetch();
// fetch friends info
limin.friends.fetch();
// fetch activity info
limin.activities.fetch();
// display all Limin's info
Limin.show();
/**
* Assume each model fetch() function return a Deferred Object,
* How can we display all the info together?
*/
var dfds = [];
dfds.push( Limin.user.fetch() );
dfds.push( limin.friends.fetch() );
dfds.push( limin.activities.fetch() );
$.when(dfds).done(function (data) {
Limin.user.set(data[0]);
Limin.friends.set(data[1]);
Limin.activities.set(data[2]);
Limin.show();
});
/**
* What if we can minimize request by batching them together?
*/
API.startBatch();
// fetch user info
Limin.user.fetch(API);
// fetch friends info
limin.friends.fetch(API);
// fetch activity info
limin.activities.fetch(API);
// display all Limin's info
API.endBatch();
function API = function () {
this.inBatch = false;
this.requests = [];
};
API.prototype.startBatch = function () {
this.inBatch = true;
}
API.prototype.endBatch = function () {
var self = this;
$.ajax({
type : 'GET',
url : 'http://facebook.com/batch',
data : this.requests.serialize()
})
.done(function (data) {
_(self.requests).foreach(function (request, index) {
request.resolve(data[index]);
});
});
this.inBatch = false;
}
/**
* What if the batch request has limit to 2.
*/
function API = function () {
this.inBatch = false;
this.requests = [];
this.limit = 2;
};
API.prototype.startBatch = function () {
this.inBatch = true;
}
API.prototype.endBatch = function () {
var self = this;
var batchNum = Math.ceiling(this.requests.length/2);
var batchDfds = [];
for (var i=0; i<batchNum; i++) {
var dfd = $.Deferred();
var sendRequests = Array.prototype.slice.call(this.requests, i, i*2-1);
$.ajax({
type : 'GET',
url : 'http://facebook.com/batch',
data : sendRequests.serialize()
})
.done(function (data) {
dfd.resolve(data);
});
batchDfds.push(dfd);
}
this.inBatch = false;
$.when(batchDfds).done(function (data) {
var results = [];
_(data).foreach(function(d) {
results = _.union(results, d);
});
_(self.requests).foreach(function (request, index) {
request.resolve(results[index]);
});
});
}
/**
* What if you have 2 widges on the save page?
*/
var Limin = new FacebookWidget('limin');
var Mike = new FacebookWidget('mike');
// fetch Limin's info
API.startBatch();
Limin.user.fetch(API);
limin.friends.fetch(API);
limin.activities.fetch(API);
API.endBatch();
// fetch Mike's info
API.startBatch();
mike.user.fetch(API);
mike.friends.fetch(API);
mike.activities.fetch(API);
API.endBatch();
// How to handle batch collision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment