bottleneck
Version:
Distributed task scheduler and rate limiter
28 lines (26 loc) • 793 B
text/coffeescript
DLList = require "./DLList"
class Sync
constructor: (, ) ->
= 0
= new DLList()
isEmpty: -> .length == 0
_tryToRun: ->
if ( < 1) and .length > 0
++
next = .shift()
next.task next.args..., (args...) =>
--
()
next.cb? args...
submit: (task, args..., cb) =>
.push {task, args, cb}
()
schedule: (task, args...) ->
wrapped = (args..., cb) ->
(task args...)
.then (args...) -> cb null, args...
.catch (args...) -> cb args...
new (resolve, reject) =>
wrapped, args..., (args...) ->
(if args[0]? then reject else args.shift(); resolve) args...
module.exports = Sync