Skip to content

Commit

Permalink
Batch multiple requests for the same resource from the same user
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Sep 4, 2014
1 parent 32e2043 commit b7d038e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
35 changes: 35 additions & 0 deletions batch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = function () {
var batch = {}

return {
/**
* Create or add a callback function to the end of a batch for a given key.
* @param key
* @param cb
*/
push: function (key, cb) {
batch[key] = batch[key] || []
batch[key].push(cb)
},

/**
* Determine if there is a batch for the given key.
* @param {String} key
* @returns {boolean}
*/
exists: function (key) {
return !!batch[key]
},

/**
* Call all the batched callback functions for a key and remove them.
* @param {String} key ID of the batch operation
* @param {Function} fn Function to call for each
*/
call: function (key, fn) {
var cbs = batch[key]
delete batch[key]
cbs.forEach(fn)
}
}
}
22 changes: 18 additions & 4 deletions manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var events = require("events")
, github = require("./github")
, githubUrl = require("github-url")
, depDiff = require("dep-diff")
, batch = require("./batch")()

module.exports = exports = new events.EventEmitter()

Expand Down Expand Up @@ -53,20 +54,31 @@ exports.getManifest = function (user, repo, authToken, cb) {
}

var gh = github.getInstance(authToken)
, batchKey = [user, repo, authToken].join("-")

if (batch.exists(batchKey)) {
return batch.push(batchKey, cb)
}

batch.push(batchKey, cb)

gh.repos.getContent({user: user, repo: repo, path: "package.json"}, function (er, resp) {
if (er) return cb(er)

if (manifest && manifest.expires > new Date()) {
console.log("Using cached private manifest", manifest.data.name, manifest.data.version)
return cb(null, JSON.parse(JSON.stringify(manifest.data)))
return batch.call(batchKey, function (cb) {
cb(null, JSON.parse(JSON.stringify(manifest.data)))
})
}

var packageJson = new Buffer(resp.content, resp.encoding).toString()
var data = parseManifest(packageJson)

if (!data) {
return cb(new Error("Failed to parse package.json: " + packageJson))
return batch.call(batchKey, function (cb) {
cb(new Error("Failed to parse package.json: " + packageJson))
})
}

console.log("Got manifest", data.name, data.version)
Expand All @@ -88,8 +100,10 @@ exports.getManifest = function (user, repo, authToken, cb) {

manifests[user] = manifests[user] || {}
manifests[user][repo] = manifest

cb(null, manifest.data)

batch.call(batchKey, function (cb) {
cb(null, manifest.data)
})

if (!oldManifest) {
exports.emit("retrieve", manifest.data, user, repo, repoData.private)
Expand Down

0 comments on commit b7d038e

Please sign in to comment.