Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tests and implemenation of group and defur functions

  • Loading branch information...
commit 9aa8c07704895ef2547e6481f39656c9dbf4c660 0 parents
@dominictarr authored
56 README.md
@@ -0,0 +1,56 @@
+#yet another nodejs async control flow lib#
+
+there are already a number of ndoejs control flow libs, notably, creationix/step and substack/seq and i've investigated them, but decided it's worth inventing your own, so that
+
+ 1. you really understand how it works.
+
+control flow is so fundamential... it best to deeply understand it. so, I'm writing my own, and I suspect you should too.
+
+##group##
+
+collect the results of a number of async functions:
+
+ var g = require('ctrlflow').group()
+
+ func1(args,g())//g() will return a function collect it's args for g.done
+ func2(args,g())
+ func3(args,g())
+
+ g.done(function (){
+ console.log("func1,2,3 called back")
+ })
+
+also, you can pass the calback directly into group():
+
+ var g = require('ctrlflow').group(function (){
+ console.log("func1,2,3 called back")
+ })
+
+ func1(args,g())//g() will return a function collect it's args for g.done
+ func2(args,g())
+ func3(args,g())
+
+##defer##
+
+sometimes you have a object that needs an async start up before certain methods are called.
+
+`ctrlflow.defer()` can be used to record async method calls, and play them back when the reciever is ready:
+
+ var async = {}//object to add defurred methods to,
+ var ready = ctrl.defer(async,['do'])//defer(obj,listOfMethodNames)
+ //-> returns ready(obj) method, for when functions can be executed.
+
+ //can call named methods on the object.
+ //they will not be executed yet.
+ async.do('hello',function (x){
+ console.log('.do() called back:',x)
+ })
+
+ var __async = {do: function (hi,funx){funx(hi)} }
+ //call ready(ObjectWhichHasRealFunctions) when it's time.
+ ready(__async)
+
+ //from now on, calls will be executed immediately.
+ async.do('goodbye',function (x){
+ console.log('.do() called back again:',x)
+ })
63 ctrlflow.js
@@ -0,0 +1,63 @@
+
+var curry = require('curry')
+/*
+ group: get the cb's of several functions.
+
+*/
+
+function findErr (funx){
+ for(var i in funx){
+ if(funx[i].arguments[0])
+ return funx[i].arguments[0]
+ }
+}
+
+exports.group = function (done){
+ var c = 0, i = 0, funx = [], args = [], called = 0, error = null
+ if(!done)
+ done = function (){
+ throw new Error("group was never given a 'done' callback")
+ }
+ var group =
+ function group (){
+ c++
+ var x = i ++
+ funx[x] = function (){
+ if(args[x])
+ throw new Error ('function called twice')
+ args[x] = arguments
+ error = error || arguments[0]
+ called ++
+ if(called == c){
+ console.log(funx)
+ done(error, args)
+ }
+ }
+ return funx[x]
+ }
+ group.done = function (_done){
+ done = _done
+ return group
+ }
+ return group
+}
+
+exports.defer = function (obj,commands){
+
+ var called = []
+ commands.forEach(function (name){
+ obj[name] = function (){
+ called.push({name: name,args: arguments})
+ }
+ })
+
+ return function (newObj){
+ commands.forEach(function (name){
+ obj[name] = curry(newObj[name],[],newObj)
+ })
+ called.forEach(function (cmd){
+ newObj[cmd.name].apply(newObj,cmd.args)
+ })
+ //execute commands, and then hook to the regular methods.
+ }
+}
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./ctrlflow')
53 test/ctrlflow.asynct.js
@@ -0,0 +1,53 @@
+
+var ctrl = require('ctrlflow')
+ , it = require('it-is')
+ , curry = require('curry')
+
+exports.group = function (test){
+
+ var g = ctrl.group(function (err,results){
+ it(err).equal(null)
+ it(results).property('length',3)
+ test.done()
+ })
+
+ setTimeout(g(),100)
+ setTimeout(g(),50)
+ setTimeout(g(),150)
+
+}
+
+exports['group with error'] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ test.done()
+ })
+
+ setTimeout(g(),100)
+ f = g()
+ setTimeout(function () {
+ f(error)
+ },50)
+ setTimeout(g(),150)
+}
+
+exports['group with args'] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ it(results).has([
+ [null,1,2,3]
+ , [it.equal(error),"message!"]
+ , [null,{obj: 54}]
+ ])
+ test.done()
+ })
+
+ setTimeout(curry(g(),[null,1,2,3]),100)
+ setTimeout(curry(g(),[error,"message!"]),50)
+ setTimeout(curry(g(),[null,{obj: 54}]),150)
+}
+
36 test/ctrlflow.defer.asynct.js
@@ -0,0 +1,36 @@
+
+var ctrl = require('ctrlflow')
+ , it = require('it-is')
+
+exports ['accept commands but lazy exec'] = function (test){
+ var async = {}
+ var ready = ctrl.defer(async,['do'])
+
+ async.do('hello',function (x){
+ it(x).equal('hello')
+ test.done()
+ })
+
+ var __async = {do: function (hi,funx){funx(hi)} }
+ ready(__async)
+}
+
+exports ['accept commands but lazy exec, non lazy when ready'] = function (test){
+ var async = {}
+ var ready = ctrl.defer(async,['do'])
+ var called = false
+
+ async.do('hello',function (x){
+ it(x).equal('hello')
+ called = true
+ })
+
+ var __async = {do: function (hi,funx){funx(hi)} }
+ ready(__async)
+ it(called).equal(true)
+ async.do('goodbye',function (x){
+ it(x).equal('goodbye')
+ test.done()
+ })
+
+}
105 test/ctrlflow.group.asynct.js
@@ -0,0 +1,105 @@
+
+var ctrl = require('ctrlflow')
+ , it = require('it-is')
+ , curry = require('curry')
+
+exports.group = function (test){
+ var g = ctrl.group(function (err,results){
+ it(err).equal(null)
+ it(results).property('length',3)
+ test.done()
+ })
+
+ setTimeout(g(),100)
+ setTimeout(g(),50)
+ setTimeout(g(),150)
+
+}
+
+exports['group with error'] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ test.done()
+ })
+
+ setTimeout(g(),100)
+ f = g()
+ setTimeout(function () {
+ f(error)
+ },50)
+ setTimeout(g(),150)
+}
+
+exports['group with args'] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ it(results).has([
+ [null,1,2,3]
+ , [it.equal(error),"message!"]
+ , [null,{obj: 54}]
+ ])
+ test.done()
+ })
+
+ setTimeout(curry(g(),[null,1,2,3]),100)
+ setTimeout(curry(g(),[error,"message!"]),50)
+ setTimeout(curry(g(),[null,{obj: 54}]),150)
+}
+
+
+exports.group2 = function (test){
+ var g = ctrl.group()
+
+ setTimeout(g(),100)
+ setTimeout(g(),50)
+ setTimeout(g(),150)
+
+ g.done(function (err,results){
+ it(err).equal(null)
+ it(results).property('length',3)
+ test.done()
+ })
+}
+
+exports['group with error 2'] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group()
+
+ setTimeout(g(),100)
+ f = g()
+ setTimeout(function () {
+ f(error)
+ },50)
+ setTimeout(g(),150)
+
+ g.done(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ test.done()
+ })
+}
+
+exports['group with args '] = function (test){
+ var error = new Error("ERR")
+ , g = ctrl.group()
+
+ setTimeout(curry(g(),[null,1,2,3]),100)
+ setTimeout(curry(g(),[error,"message!"]),50)
+ setTimeout(curry(g(),[null,{obj: 54}]),150)
+
+ g.done(function (err,results){
+ it(err).equal(error)
+ it(results).property('length',3)
+ it(results).has([
+ [null,1,2,3]
+ , [it.equal(error),"message!"]
+ , [null,{obj: 54}]
+ ])
+ test.done()
+ })
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.