Batcher.coffee
792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
parser = require "./parser"
Events = require "./Events"
class Batcher
defaults:
maxTime: null
maxSize: null
Promise: Promise
constructor: (@options={}) ->
parser.load @options, @defaults, @
@Events = new Events @
@_arr = []
@_resetPromise()
@_lastFlush = Date.now()
_resetPromise: ->
@_promise = new @Promise (res, rej) => @_resolve = res
_flush: ->
clearTimeout @_timeout
@_lastFlush = Date.now()
@_resolve()
@Events.trigger "batch", @_arr
@_arr = []
@_resetPromise()
add: (data) ->
@_arr.push data
ret = @_promise
if @_arr.length == @maxSize
@_flush()
else if @maxTime? and @_arr.length == 1
@_timeout = setTimeout =>
@_flush()
, @maxTime
ret
module.exports = Batcher