Skip to content
Browse files

finished testing, works great!

	modified:   README.md
	modified:   index.js
	modified:   test.js
  • Loading branch information...
1 parent a74d9a5 commit 012fa93dfc32adfd0e490e10d07d1f7a8beb96a7 @shimondoodkin committed
Showing with 189 additions and 47 deletions.
  1. +14 −4 README.md
  2. +43 −19 index.js
  3. +132 −24 test.js
View
18 README.md
@@ -78,6 +78,7 @@ pages can share functions between them. for example:
this.flow; // inflow.flow shortcut
this.parallel; // inflow.parallel shortcut
this.each; // inflow.each shortcut
+ this.while; // inflow.each shortcut
this.args //arguments of previously called next(arg1,arg2) function in sequential flow
this.results //arguments of all called next(arg1,arg2) functions in parallel
@@ -86,6 +87,9 @@ pages can share functions between them. for example:
this.value //
this.items //
+ return this.continue() //availible in a while_function, in inflow.while, this.continue=this.next
+ return this.break() //availible in a while_function, in inflow.while, call the callback and finish the loop.
+
//also you can do:
this(); or this.next(); //those are the same.
};
@@ -156,7 +160,7 @@ pages can share functions between them. for example:
## Methods:
-### function flow(shared,steps[,debug])
+### function flow(shared,steps [,debug])
calls each step functions one after an other.
@@ -174,7 +178,7 @@ also available inside the called function:
this.steps.push(some_function);
this.steps.push([some_function,[arg1,arg2]]); // with arguments
-###function parallel(shared,steps,callback[,debug])
+###function parallel(shared,steps[,callback] [,debug])
calls all the steps functions and when all done it calls the callback function.
@@ -192,7 +196,9 @@ also available inside the callback function:
this.results // array of all next arguments
-###function each(shared,items,each_function,callback [,debug])
+###function each(shared,items,each_function[,callback] [,debug])
+
+ a for each function, it can have an object as input for items
each_function: (as in Array.forEach callback)
@@ -221,8 +227,12 @@ output:
222222221234 - Avi
done
+/*
+ it seems to me that the shared object in forEach and in while is use less.
+ tell me what you think.
+*/
-### function while(shared,loop_function,callback,debug)
+### function while(shared,loop_function[,callback] [,debug])
async while with a shared object,
the way you use this while is this, this while is basicaly while(true) and, you break and it with a break statment.
you can define the position of while condition, where you want it to be: at the begining or at the end. there you put a break statment.
View
62 index.js
@@ -20,6 +20,7 @@ var self=this;
// one simple function
function flow(shared,steps,debug, currentstep,args,called)
{
+ if(!steps)steps=[];
if(arguments.length==2) debug = false;
if(!currentstep) currentstep=0;
if(debug){ if(steps.timeout) clearTimeout(steps.timeout); if(!called) called=[]; }
@@ -70,24 +71,29 @@ function flow(shared,steps,debug, currentstep,args,called)
function parallel(shared,steps,callback,debug, currentstep,args)
{
- var callbackcount=0;
+ var callback_count=0;
var status=[];
var results=[];
var timeout=false;
function gonext(i,result)
{
- if(status[i])
+
+ if(i!==false)
{
- if(debug)
- console.log(debug_trace(' called more then once '));
+ if(status[i])
+ {
+ if(debug)
+ {
+ console.log(debug_trace(' called more then once '));
+ return;
+ }
+ }
+ callback_count++;
+ status[i]=true;
+ results[i]=result;
}
- else
- return;
- callback_count++;
- status[i]=true;
- results[i]=result;
- if( callback_count == steps.length )
+ if( callback_count == steps.length )
{
var next= function(result){ }; // this inside gonext so this does nothing
next.next=next;
@@ -99,14 +105,27 @@ function parallel(shared,steps,callback,debug, currentstep,args)
next.each=self.each;
next.results=results;
next.while=self.while;
- if(typeof callback==='object' && callback instanceof Array)
- callback[0].apply(next, callback[1]);
- else
- callback.call(next,results);
-
+ if(callback)
+ {
+ if(typeof callback==='object' && callback instanceof Array)
+ callback[0].apply(next, callback[1]);
+ else
+ callback.call(next,results);
+ }
if(timeout)clearTimeout(timeout);//of debug
}
}
+ if(!steps)
+ {
+ steps=[];
+ gonext(false,[]);
+ }
+ else if(steps.length==0)
+ {
+ gonext(false,[]);
+ }
+ else
+ {
for(var i=0;i<steps.length;i++)
{
var next= function(result){ gonext(i,result); };
@@ -135,6 +154,7 @@ function parallel(shared,steps,callback,debug, currentstep,args)
});
}
}
+ }
if(debug)
{
if(report_uncalled_callbeck_after!=0)
@@ -149,6 +169,7 @@ function each(shared,steps,each_function,callback,debug, currentstep,args,
//results,
keys,called,timer)
{
+ if(!steps)steps=[];
//console.log(require('sys').inspect(steps));
if(typeof keys==='undefined')
{
@@ -188,10 +209,13 @@ function each(shared,steps,each_function,callback,debug, currentstep,args,
next.while=self.while;
next.args=args;
//next.results=results;
- if(typeof callback==='object' && callback instanceof Array)
- callback[0].apply(next, callback[1]);
- else
- callback.call(next);
+ if(callback)
+ {
+ if(typeof callback==='object' && callback instanceof Array)
+ callback[0].apply(next, callback[1]);
+ else
+ callback.call(next);
+ }
});
return;
}
View
156 test.js
@@ -1,7 +1,10 @@
-// to test specific case comment with /* */ the other ones
+// to test specific case comment with /* */ the other ones
+
var inflow = require('./index');
var sys = require('sys');
+var undef;
+
var i=0;
function foo()
{
@@ -26,55 +29,160 @@ function should_not_called_each_function(value,key,array){
}
-function done_callback(value){
+function done_callback(value,callback){
i++;console.log(i+' done "'+(value?value:'')+'", shared='+sys.inspect(this.shared).split("\n").join('')+', args='+sys.inspect(this.args).split("\n").join(''));
+ if(callback)callback();
this.next(i+' done "'+(value?value:'')+'" ok');
}
-var n=0
-function while_function(){
- //console.log('before condition - while');
- if(!(n<10)) return this.break();
- i++;console.log(i+' while, shared='+sys.inspect(this.shared).split("\n").join('')+', args='+sys.inspect(this.args).split("\n").join(''));
-
- n++;
- if(n==5)
- {
- console.log('n='+n+' continue...');
- return this.continue();
- }
+function get_while_function()
+{
+ var n=0;
+ return (
+ function while_function(){
+ //console.log('before condition - while');
+ if(!(n<10)) return this.break();
+ i++;console.log(i+' while, shared='+sys.inspect(this.shared).split("\n").join('')+', args='+sys.inspect(this.args).split("\n").join(''));
- console.log('whiling '+n);
-
- this.next(i+' while ok');
+ n++;
+ if(n==5)
+ {
+ console.log('n='+n+' continue...');
+ return this.continue();
+ }
+
+ console.log('whiling '+n);
+
+ this.next(i+' while ok');
+ });
}
var shared={};
+var tests=[
+
+function (){
+console.log('\n // flow\n');
console.log('\n flow:\n');
-inflow.flow(shared,[foo,bar,[done_callback,['flow (array)']]]);
+inflow.flow(shared,[foo,bar,[done_callback,['flow (array)',this]]]);
+
+}
+,
+function (){
+
+console.log('\n flow (empty):\n');
+inflow.flow(shared,[]);
+this();
+}
+,
+function (){
+console.log('\n // parallel\n');
console.log('\n parallel:\n');
-inflow.parallel(shared,[foo,bar],[done_callback,['parallel']]);
+inflow.parallel(shared,[foo,bar],[done_callback,['parallel',this]]);
+
+}
+,
+function (){
+
+console.log('\n parallel (empty):\n');
+inflow.parallel(shared,[],[done_callback,['parallel',this]]);
+
+}
+,
+function (){
+console.log('\n // each\n');
console.log('\n each (array):\n');
-inflow.each(shared,['foo (array)','bar (array)'],each_function,[done_callback,['each (array)']]);
+inflow.each(shared,['foo (array)','bar (array)'],each_function,[done_callback,['each (array)',this]]);
+
+}
+,
+function (){
console.log('\n each (array):\n');
-inflow.each(shared,{'foo':'foo (object)','bar':'bar (object)'},each_function,[done_callback,['each (object)']]);
+inflow.each(shared,{'foo':'foo (object)','bar':'bar (object)'},each_function,[done_callback,['each (object)',this]]);
+
+}
+,
+function (){
console.log('\n each (empty array):\n');
-inflow.each(shared,[],should_not_called_each_function,[done_callback,['each (empty array)']]);
+inflow.each(shared,[],should_not_called_each_function,[done_callback,['each (empty array)',this]]);
+
+}
+,
+function (){
console.log('\n each (empty object):\n');
-inflow.each(shared,{},should_not_called_each_function,[done_callback,['each (empty object)']]);
+inflow.each(shared,{},should_not_called_each_function,[done_callback,['each (empty object)',this]]);
+
+}
+,
+function (){
+console.log('\n // while\n');
console.log('\n while:\n');
-inflow.while(shared,while_function,[done_callback,['while']]);
+inflow.while(shared,get_while_function(),[done_callback,['while',this]]);
+}
+,
+function (){
+console.log('\n // undefined input\n');
+
+
+console.log('\n flow (undefined):\n');
+inflow.flow(shared,undef);
+
+setTimeout(this,100);
+
+}
+,
+function (){
+
+console.log('\n parallel (undefined):\n');
+inflow.parallel(shared,undef,this);
+
+}
+,
+function (){
+
+console.log('\n each (undefined):\n');
+inflow.each(shared,undef,each_function,this);
+
+}
+,
+function (){
+console.log('\n ///no callback\n');
+console.log('\n no callback-parallel:\n');
+inflow.parallel(shared,[foo,bar]);
+
+setTimeout(this,100);
+}
+,
+
+function (){
+
+console.log('\n no callback-each (array):\n');
+inflow.each(shared,['foo (array)','bar (array)'],each_function);
+
+setTimeout(this,100);
+}
+
+,
+
+function (){
+
+console.log('\n no callback-while:\n');
+
+inflow.while(shared,get_while_function());
+setTimeout(this,100);
+}
+];
+inflow.flow(shared,tests);

0 comments on commit 012fa93

Please sign in to comment.
Something went wrong with that request. Please try again.