Created
March 16, 2010 02:54
-
-
Save tmpvar/333587 to your computer and use it in GitHub Desktop.
meh, fork + join.. sort of
This file contains hidden or 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
console.clear(); | |
var node = { | |
first: function() { | |
console.log(this.hello); | |
this.hello++; | |
}, | |
second : function() { | |
console.log(this.hello); | |
this.hello+=100; | |
}, | |
merge : function(first,second) { | |
console.log("merged: " + (first.hello + second.hello)); | |
} | |
}; | |
var fork = function(handler) { | |
var scope = { | |
hello : 0, | |
run : function() { | |
handler.call(scope); | |
console.log("now:" + scope.hello); | |
} | |
}; | |
return scope; | |
}; | |
var join = function(firstHandle, secondHandle, cb) { | |
var scope = { | |
first : [firstHandle], | |
second : [secondHandle] | |
}; | |
var ret = function(handle) { | |
if (handle === scope.first[0]) { | |
scope.first.push(handle); | |
} else if (handle === scope.second[0]) { | |
scope.second.push(handle); | |
} | |
if (scope.first.length === 2 && scope.second.length === 2) { | |
cb.call(scope, scope.first[0], scope.second[0]); | |
} | |
return ret; | |
}; | |
return ret; | |
}; | |
var firstHandle = fork(node.first); | |
var secondHandle = fork(node.second); | |
firstHandle.run(); | |
firstHandle.run(); | |
firstHandle.run(); | |
join(firstHandle,secondHandle, node.merge)(secondHandle)(firstHandle); | |
secondHandle.run(); | |
secondHandle.run(); | |
secondHandle.run(); | |
join(firstHandle,secondHandle, node.merge)(secondHandle)(firstHandle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment