Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
function Wait() { | ||
// ID for each wrap | ||
var ID = 0; | ||
// waiting map for wrapped func | ||
var waiting = {}; | ||
// returns | ||
var returns = []; | ||
// seq for next func | ||
var seq = []; | ||
|
||
function wait(func, param1) { | ||
var id = ID++; | ||
waiting[id] = true; | ||
// return the wrapped func | ||
return function() { | ||
var ret; | ||
// ignore the timeouted or returned funcs | ||
if (waiting[id]) { | ||
|
||
if (typeof func == 'function') | ||
ret = func.apply(this, arguments); | ||
// else if ((func == 'arg' || func == 'argument' || func == 'arguments') && param1 !== undefined) | ||
// ret = arguments[param1]; | ||
|
||
delete waiting[id]; | ||
returns[id] = ret; | ||
} | ||
next(); | ||
return ret; | ||
}; | ||
} | ||
|
||
function next() { | ||
if (seq.length > 0 && Object.keys(waiting).length == 0) { | ||
// when nothing waiting => call next sequence function and pass down returns | ||
// keep the returns in tmp for passing to next sequence function | ||
var tmpReturns = returns; | ||
// reset all variables | ||
ID = 0; | ||
waiting = {}; | ||
returns = [] | ||
var thenTarget = seq.shift(); | ||
thenTarget(tmpReturns); | ||
next(); | ||
} | ||
} | ||
|
||
function firstCall() { | ||
// insert new functions into the sequence running head | ||
seq = Array.prototype.slice.call(arguments).concat(seq); | ||
next(); | ||
return this; | ||
} | ||
|
||
function then() { | ||
// queue new functions into the sequence running tail | ||
seq = seq.concat(Array.prototype.slice.call(arguments)); | ||
next(); | ||
return this; | ||
} | ||
|
||
// kill and sub-waits | ||
wait.children = []; | ||
wait.kill = function() { | ||
ID = 0; | ||
waiting = {}; | ||
seq = []; | ||
|
||
wait.children.forEach(function(child) { | ||
child.kill(); | ||
}); | ||
wait.killed = true; | ||
|
||
var tmpReturns = returns; | ||
returns = [] | ||
return tmpReturns; | ||
} | ||
|
||
wait.next = next; | ||
wait.firstCall = firstCall; | ||
wait.then = then; | ||
return wait; | ||
} | ||
|
||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = Wait; | ||
} | ||
exports.Wait = Wait; | ||
} else { | ||
this.Wait = Wait; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
Use after like this: | ||
Check out test.js | ||
|
||
// first parallel group | ||
after = after.fork(); | ||
after.append(function(){ | ||
|
||
setTimeout(after(function(){ | ||
out += '1<br/>'; | ||
setTimeout(after(function(){ | ||
out += '3<br/>'; | ||
}), Math.random()*100); | ||
out += '2<br/>'; | ||
after.insert(function(){ | ||
out += '4<br/>'; | ||
after.next(null, 'result'); | ||
}); | ||
}), Math.random()*100); | ||
|
||
}); | ||
|
||
after.append(function(){ | ||
console.log(arguments); | ||
out += '5<br/>'; | ||
}, function(err){ | ||
console.log(arguments); | ||
out += '6<br/>'; | ||
}); | ||
|
||
/* | ||
Will print out | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
*/ | ||
see index.html | ||
|
||
var Wait = require('./Wait.js'); | ||
|
||
//first parallel group | ||
var wait = new Wait(); | ||
|
||
setTimeout(wait(function() { | ||
|
||
console.log('A'); | ||
|
||
function loopAsyncCall(i, callback) { | ||
setTimeout(function() { | ||
console.log('Async Loop ' + i); | ||
callback(); | ||
}, 100); | ||
} | ||
|
||
for (var i = 0; i < 10; i++) { | ||
if (i % 2 == 0) { | ||
loopAsyncCall(i, wait()); | ||
} | ||
} | ||
|
||
// another async call | ||
setTimeout(wait(function() { | ||
console.log('Another Async call'); | ||
}), 100); | ||
|
||
console.log('B'); | ||
|
||
// firstCall | ||
wait.firstCall(function() { | ||
console.log('After All Async Calls'); | ||
}); | ||
|
||
}), 100); | ||
|
||
wait.then(function() { | ||
console.log('Finally .then() accept array of functions'); | ||
}, function() { | ||
console.log('All Done'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var Wait = require('./Wait.js'); | ||
|
||
//first parallel group | ||
var wait = new Wait(); | ||
|
||
setTimeout(wait(function() { | ||
|
||
console.log('A'); | ||
|
||
function loopAsyncCall(i, callback) { | ||
setTimeout(function() { | ||
console.log('Async Loop ' + i); | ||
callback(); | ||
}, 100); | ||
} | ||
|
||
for (var i = 0; i < 10; i++) { | ||
if (i % 2 == 0) { | ||
loopAsyncCall(i, wait()); | ||
} | ||
} | ||
|
||
// another async call | ||
setTimeout(wait(function() { | ||
console.log('Another Async call'); | ||
}), 100); | ||
|
||
console.log('B'); | ||
|
||
// firstCall | ||
wait.firstCall(function() { | ||
console.log('After All Async Calls'); | ||
}); | ||
|
||
}), 100); | ||
|
||
wait.then(function() { | ||
console.log('Finally .then() accept array of functions'); | ||
}, function() { | ||
console.log('All Done'); | ||
}); |