funcflow
Version:
Simplifies asynchronous control flow in javascript making making parallel code, synchronous code, and error handling simple
142 lines (127 loc) • 3.52 kB
text/coffeescript
str=(obj)->
if obj == null then "null"
else if typeof obj == "undefined" then "undefined"
else obj.toString()
class Test
constructor:( , )->
= 0
expect:(num)->
= num
equal:(arg1, arg2)->
--
if arg1 != arg2 then throw "'#{str(arg1)}' does not equal '#{str(arg2)}'"
ok:(bool)->
--
if not bool then throw "false was passed to ok"
done:()->
if != 0 then throw "#{str(@num)} more checks were expected before done was called"
run:()->
.call(this)
test=(name, func)->
t = new Test(name, func)
exports[name]=()->t.run()
exports.RunAll = ()->
for name of exports
if name != "RunAll"
try
exports[name]()
catch ex
console.log "Error in Test '#{name}'"
console.log ex
console.log ''
return
callMeBack=(func, args...)->
func.apply(null, args)
funcflow=require("../lib/funcflow")
test "Basic", ()->
steps = []
steps.push (step, err)=>
callMeBack(step.next, 1, 2, 3)
steps.push (step, err)=>
callMeBack(step.next)
funcflow(steps, )
test "CallbackParameters", ()->
funcflow([
(step, err)=>
callMeBack(step.next, 1, 2)
(step, err, arg1, arg2)=>
callMeBack(step.next)
], )
test "ParallelCode", ()->
funcflow([
(step, err)=>
callMeBack(step.spawn())
callMeBack(step.spawn())
callMeBack(step.spawn())
step.next()
], )
test "BasicErrorHandling", ()->
funcflow([
(step, err)=>
throw "some error"
(step, err)=>
step.next()
(step, err)=>
step.next()
], )
test "ParallelErrorHandling", ()->
funcflow([
(step, err)=>
callMeBack(step.spawn())
callMeBack(step.spawn())
throw "some error"
step.next()
(step, err)=>
step.next()
], )
test "BubbleErrorHandling", ()->
exThrown = false
try
funcflow([(step, err)->step.raise("some error")])
catch ex
exThrown = true
test "DontCatchExceptions", ()->
exThrown = false
try
funcflow([(step, err)->math.kj], {catchExceptions:false}, ()->)
catch ex
exThrown = true
test "NoCallback", ()->
funcflow([
(step, err)=>
])
test "BasicStateTest", ()->
sharedState = {
sharedstr: "not modified"
sharedfunc: ()->true
}
funcflow([
(step, err)=>
step.sharedstr = "modified"
step.sharedfunc = ()->false
step.next()
(step, err)=>
step.next()
], sharedState, )