Comparing version
33
index.js
@@ -8,3 +8,3 @@ /*@flow*/ | ||
/*:: | ||
type WaitFor_t = <F>(?F)=>(F); | ||
type WaitFor_t = { <F>(?F):(F), abort: ()=>void }; | ||
type NthenRet_t = { nThen: Nthen_t, orTimeout:((WaitFor_t)=>void, number)=>NthenRet_t }; | ||
@@ -18,12 +18,25 @@ type Nthen_t = ((WaitFor_t)=>void)=>NthenRet_t; | ||
var abort; | ||
var inNthen = 0; | ||
var callNext = function (arg) { | ||
inNthen++; | ||
funcs.shift()(arg); | ||
inNthen--; | ||
}; | ||
var waitFor = ((function(func) { | ||
calls++; | ||
return function() { | ||
if (func) { | ||
func.apply(null, arguments); | ||
var f = function () { | ||
if (func) { | ||
func.apply(null, arguments); | ||
} | ||
calls = (calls || 1) - 1; | ||
while (!calls && funcs.length && !abort) { | ||
callNext(waitFor); | ||
} | ||
}; | ||
if (inNthen) { | ||
setTimeout(f); | ||
} else { | ||
f(); | ||
} | ||
calls = (calls || 1) - 1; | ||
while (!calls && funcs.length && !abort) { | ||
funcs.shift()(waitFor); | ||
} | ||
}; | ||
@@ -39,3 +52,5 @@ }/*:any*/)/*:WaitFor_t*/); | ||
if (!calls) { | ||
inNthen++; | ||
next(waitFor); | ||
inNthen--; | ||
} else { | ||
@@ -53,5 +68,7 @@ funcs.push(next); | ||
while (funcs.shift() !== cto) { } | ||
inNthen++; | ||
func(waitFor); | ||
inNthen--; | ||
calls = (calls || 1) - 1; | ||
while (!calls && funcs.length) { funcs.shift()(waitFor); } | ||
while (!calls && funcs.length) { callNext(waitFor); } | ||
}, milliseconds); | ||
@@ -58,0 +75,0 @@ funcs.push(cto = function() { |
@@ -5,3 +5,3 @@ { | ||
"description": "Simple intuitive asynchronous control flow.", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"repository": { | ||
@@ -17,5 +17,4 @@ "type": "git", | ||
"devDependencies": { | ||
"flow-bin": "^0.62.0", | ||
"jshint": "^2.9.4", | ||
"tap": "~0.4.0" | ||
"flow-bin": "^0.106.2", | ||
"tap": "^14.6.1" | ||
}, | ||
@@ -22,0 +21,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
11191
4.17%2
-33.33%166
11.41%