Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hayes/batch-queue
queue actions to be run in batches
https://github.com/hayes/batch-queue
Last synced: about 6 hours ago
JSON representation
queue actions to be run in batches
- Host: GitHub
- URL: https://github.com/hayes/batch-queue
- Owner: hayes
- License: mit
- Created: 2014-06-27T03:47:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-08-04T22:27:17.000Z (over 10 years ago)
- Last Synced: 2024-10-12T13:46:09.061Z (about 1 month ago)
- Language: JavaScript
- Size: 168 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
batch-queue
===========queue actions to be run in batches
```
var batch = require('batch-queue')(raf)
, thing = {}function raf() {
requestAnimationFrame(function() {
console.log(thing) // {}
batch.run()
console.log(thing) // {a: 1, c: 3}
})
}batch.queue(function() {
thing.a = 1
})var foo = batch.add(function(key) {
this[key] = 3 // context is preserved
})foo.call(thing, 'b') // wont be run because foo is called again
foo.call(thing, 'c')
```# API
### batch(ready, all) -> batch instance
ready will be called the first time something is queued after the queue has been drained. ideally it should be used in combination with a timeout or requestAnimationFrame.
if all is set to true, ready will be called even if the queue was not empty. useful if you want to batch by number of actions rather than time.### batch.queue(fn)
queue a function to be called next time the batch is run. the function is called without a context, use fn.bind if you need a specific context to be set### batch.add(fn) -> fn
works like a debaunce. batch.add will return a new function, when this function is called it will queue the original function to be called with the passed arguments and context. calling this function multiple times will result in the function only running once when the batch executes with the last set of arguments passed.### batch.run() -> boolean
runs any queued jobs, returns `true` if any jobs were run, otherwise returns `false`