bal-util
Version:
Common utility functions for Node.js used and maintained by Benjamin Lupton
88 lines (70 loc) • 1.84 kB
text/coffeescript
{TaskGroup} = require('taskgroup')
typeChecker = require('typechecker')
eachr = require('eachr')
balUtilFlow =
wait: (delay,fn) ->
setTimeout(fn,delay)
flow: (args...) ->
if args.length is 1
{object,actions,action,args,tasks,next} = args[0]
else if args.length is 4
[] = args
else if args.length is 3
[] = args
if action? is false and actions? is false
throw new Error('balUtilFlow.flow called without any action')
# Create tasks group and cycle through it
actions ?= action.split(/[,\s]+/g)
object ?= null
tasks or= new TaskGroup().done(next)
actions.forEach (action) -> tasks.addTask (complete) ->
argsClone = (args or []).slice()
argsClone.push(complete)
fn = if typeChecker.isFunction(action) then action else object[action]
fn.apply(object,argsClone)
tasks.run()
@
createSnore: (message,opts) ->
opts or= {}
opts.delay ?= 5000
snore =
snoring: false
timer: setTimeout(
->
snore.clear()
snore.snoring = true
message?()
opts.delay
)
clear: ->
if snore.timer
clearTimeout(snore.timer)
snore.timer = false
return snore
suffixArray: (suffix, args...) ->
result = []
for arg in args
arg = [arg] unless typeChecker.isArray(arg)
for item in arg
result.push(item+suffix)
return result
module.exports = balUtilFlow