bottleneck
Version:
Distributed task scheduler and rate limiter
28 lines (26 loc) • 865 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.apply {}, next.args.concat (args...) =>
--
()
next.cb?.apply {}, args
submit: (task, args..., cb) =>
.push {task, args, cb}
()
schedule: (task, args...) =>
wrapped = (args..., cb) ->
(task.apply {}, args)
.then (args...) -> cb.apply {}, Array::concat null, args
.catch (args...) -> cb.apply {}, args
new Promise (resolve, reject) =>
.apply {}, Array::concat wrapped, args, (args...) ->
(if args[0]? then reject else args.shift(); resolve).apply {}, args
module.exports = Sync