Skip to content

Commit

Permalink
change to Wait
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfong committed Apr 12, 2014
1 parent 3cee024 commit 2538568
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 141 deletions.
92 changes: 92 additions & 0 deletions Wait.js
@@ -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;
}
57 changes: 0 additions & 57 deletions after.js

This file was deleted.

43 changes: 0 additions & 43 deletions index.html

This file was deleted.

83 changes: 42 additions & 41 deletions readme.md
@@ -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');
});
41 changes: 41 additions & 0 deletions test.js
@@ -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');
});

0 comments on commit 2538568

Please sign in to comment.