@railzai/bottleneck
Version:
Distributed task scheduler and rate limiter
40 lines (33 loc) • 792 B
text/coffeescript
parser = require "./parser"
Events = require "./Events"
class Batcher
defaults:
maxTime: null
maxSize: null
Promise: Promise
constructor: ( ={}) ->
parser.load , , @
= new Events @
= []
()
= Date.now()
_resetPromise: ->
= new (res, rej) => = res
_flush: ->
clearTimeout
= Date.now()
()
.trigger "batch",
= []
()
add: (data) ->
.push data
ret =
if .length ==
()
else if ? and .length == 1
= setTimeout =>
()
,
ret
module.exports = Batcher