-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsl.coffee
41 lines (34 loc) · 945 Bytes
/
dsl.coffee
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
40
41
_ = require 'underscore'
_.mixin require 'underscore.deferred'
___ = -> console.log arguments...
# promise to wait s seconds
wait = (s) ->
deferred = _.Deferred()
_.delay (-> deferred.resolve()), 1000*s
deferred.promise()
# promise to perform actions in parallel
par = (actions) ->
_.when [a() for a in actions]
# promise to perform actions in sequence
seq = (actions, deferred) ->
deferred ?= new _.Deferred()
if actions and actions.length
action = actions[0]?()
if action? and action.then
action.then -> seq actions[1..], deferred
else
seq actions[1..], deferred
else
deferred.resolve()
deferred.promise()
# promise to perform actions at given times
cron = (sch) ->
actions = for time, action of sch
((time, action) ->
-> seq [(-> ___ time), -> action()]) time, action
seq actions
ctx = window? || module.exports = {}
ctx.seq = seq
ctx.par = par
ctx.wait = wait
ctx.cron = cron