Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updates to not use call

  • Loading branch information...
commit 2aab46c6ef49e1ff4f1ca010f06e434bc624b8ce 1 parent e324218
@hassox authored
View
1  .gitignore
@@ -0,0 +1 @@
+*.log
View
23 examples/simple.js
@@ -1,20 +1,15 @@
process.mixin(require("sys"));
process.mixin(require('./../lib/link'));
-app = Link.Builder.chain([
- Link.Links.Responder,
- function(env) {
-
- env.finish(function() {
- puts('all done!')
- })
-
- env.call(function() {
- env.body += 'hey guys'
- env.done()
- })
- }
-])
+
+app = Link.Builder.chain([function(env) {
+ env.onFinish(function() {
+ puts('all done!')
+ })
+
+ env.body += "Hey Guys"
+ env.done()
+}])
Link.run(app);
View
52 examples/simple_chain.js
@@ -1,54 +1,51 @@
process.mixin(require("sys"));
process.mixin(require('./../lib/link'));
+/* A global auditor */
var Auditor = {
- handle : function(env){
- puts("Auditing the environment with uri " + env.request.uri.path);
- puts(inspect(arguments));
- args = Array.prototype.slice.call(arguments)
- args.shift();
+ handle : function(messages){
+ puts(new Date().toString() + " Auditing the environment with uri " + env.request.uri.path);
+ args = Array.prototype.slice.call(messages)
while(message = args.shift()){
puts(" " + message);
}
}
}
+Link.App.prototype.audit = function(){
+ this.emit("audit", arguments)
+}
+
Link.addGlobalCallback(function(app){
app.addListener("audit",function(env){
Auditor.handle(env);
});
});
+/* End the global auditor */
-
-var Responder = new Link.App("Responder", {
+var SomeMiddleware = new Link.App("SomeMiddleware", {
onRequest : function(env){
- var self = this;
- env.callback(function(){
- self.onResponse(env);
- });
+ var self = this
+ this.audit("A Message from the middleware")
+
+ env.onDone(function(){
+ puts("On the way out. I went through Some Middleware")
+ env.done();
+ })
- this.emit("audit", env, "Some Message from the responder");
- // route
- if(env.request.uri.path == "/test"){
- this.nextApp.emit("request", env);
+ if (env.request.uri.path == "/test"){
+ env.send(this.nextApp)
} else {
- SomeEndPoint.emit("request", env);
+ env.send(SomeEndPoint)
}
- },
-
- onResponse : function(env){
- var res = env.response;
- res.sendHeader(env.status, env.headers);
- res.sendBody(env.body);
- res.finish();
}
-});
+})
var SomeEndPoint = new Link.App("SomeEndPoint", {
onRequest : function(env){
env.body += "I'm In " + this.name;
env.done();
- this.emit("audit", env, "Hi, I'm in SomeEndPoint and I've got somethign to say");
+ this.audit("Hi, I'm in Some Endpoint and I've got something to say");
}
})
@@ -57,10 +54,11 @@ var EndPoint = new Link.App("EndPoint", {
env.body += "<br/>I'm in the endpoint now!";
env.headers["Content-Type"] = "text/plain";
env.done();
+ this.audit("I'm in the Endpoint. I'm done " )
}
});
-Link.Builder.chain([Responder, EndPoint]);
+app = Link.Builder.chain([SomeMiddleware, EndPoint]);
-Link.run(Responder);
+Link.run(app);
View
4 lib/link/app.js
@@ -3,6 +3,7 @@ sys = require("sys");
function App(name, obj){
if ( !(this instanceof arguments.callee) )
return new App(obj);
+
process.mixin(true, this, obj);
var self = this;
this.name = name;
@@ -15,7 +16,6 @@ function App(name, obj){
});
break;
case "globalCallbacks" :
- sys.puts(inspect(arguments[1]));
for(var i=0; i < arguments[1].length; i++){
arguments[1][i](self);
};
@@ -29,7 +29,7 @@ function AppPrototype(){
if (callback !== undefined) {
env.callback(callback)
}
-
+
if (this.nextApp !== undefined){
nextApp.emit("request", env)
} else {
View
10 lib/link/builder.js
@@ -1,30 +1,22 @@
sys = require("sys");
Builder = {
chain : function(stack){
- var events = {};
-
var firstApp = this.wrap(stack[0])
var app = firstApp
for(var i = 1; i < stack.length; i++){
var nextApp = this.wrap(stack[i])
app.nextApp = nextApp
-
- /* by getting all the events of the parent, all child
- * apps can be setup to re-emit unknown events back to the parent
- */
app = nextApp
}
return firstApp
},
+
wrap : function(app) {
- puts(typeof(app))
if (typeof(app) == 'function') {
- puts('wrapping')
return new Link.App('', {onRequest: app})
} else {
return app
}
-
}
}
View
6 lib/link/core.js
@@ -11,6 +11,7 @@ function Link(){
this.globalCallbacks = function(fn){ return globalCallbacks; }
this.run = function(masterApp, opts){
+ Link = this;
if(opts == undefined){ opts = {} }
var defaults = {
port : 8000
@@ -22,10 +23,15 @@ function Link(){
self.emit("start", "globalCallbacks", self.globalCallbacks());
http = require('http');
+
http.createServer(function(req, res){
env = new self.Environment(req, res);
+
+ env.onDone(Link.Links.responderCallback);
+
masterApp.onRequest.call(masterApp, env);
}).listen(opts.port);
+
sys.puts("Server running at http://127.0.0.1:" + opts.port.toString());
}
process.mixin(this, require('./app'));
View
33 lib/link/environment.js
@@ -12,31 +12,24 @@ Environment.prototype = {
status : 200,
headers : {},
body : "",
- _callbacks : [],
- call : function(nextApp, callback) {
- switch (arguments.length) {
- case 2:
- this.callback(arguments[1])
- arguments[0].emit("request", this)
- break
- case 1:
- if ('onRequest' in arguments[0]) {
- arguments[0].emit("request", this)
- } else {
- this.callback(arguments[0])
- this.done()
- }
- break
- }
+ _doneCallbacks : [],
+
+ send : function(nextApp, callback) {
+ if (arguments.length > 1)
+ this.onDone(arguments[1]);
+ nextApp.emit("request", this)
},
+
done : function() {
- if (this._callbacks.length != 0) this._callbacks.pop().call(this);
+ if (this._doneCallbacks.length != 0) this._doneCallbacks.pop()(this);
},
- finish : function(fn){
+
+ onFinish : function(fn){
this.response.addListener('sent', fn)
},
- callback : function(fn){
- this._callbacks.push(fn);
+
+ onDone : function(fn){
+ this._doneCallbacks.push(fn);
},
}
View
14 lib/link/links.js
@@ -1,11 +1,9 @@
Links = {
- Responder: function(env) {
- env.call(this.nextApp, function() {
- env.response.sendHeader(env.status, env.headers);
- env.response.sendBody(env.body);
- env.response.finish();
- })
- }
+ responderCallback: function(env) {
+ env.response.sendHeader(env.status, env.headers);
+ env.response.sendBody(env.body);
+ env.response.finish();
+ }
}
-exports.Links = Links;
+exports.Links = Links;
Please sign in to comment.
Something went wrong with that request. Please try again.