-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batch multiple requests for the same resource from the same user
- Loading branch information
Showing
2 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters