Permalink
Browse files

new pause(), stop() and resume() APIs

  • Loading branch information...
1 parent f5c3c88 commit 8b6a8881ce298fb158f099f4585519b4a21c739b @pibi pibi committed Jun 21, 2011
Showing with 129 additions and 5 deletions.
  1. +10 −1 README.markdown
  2. +1 −1 package.json
  3. +34 −0 test/zen-http.test.js
  4. +34 −0 test/zen.test.js
  5. +23 −1 zen-http.js
  6. +27 −2 zen.js
View
@@ -68,6 +68,15 @@ errorHandler and resultHandler could be overridden by custom functions. As handl
Due its general purpose, _Zen_ does not provide any middleware modules of any kind. Take a look at http://github.com/pblabs/zen-garden
+## API
+
+`zen.errorHandler` : this is the default request handler and the called handler on errors. Must be a function.
+When an `errorHandler` throws exception, this is catched by the original errorHandler (that prints on console).
+`zen.resultHandler` : this is the result handler. When it throws execption this is catched by the `errorHandler`.
+`zen.pause` : pauses the engine and buffers the requests.
+`zen.stop` : stops the engine, requests will be forwarded to the errorHandler.
+`zen.resume` : resumes the engine and flushes the requests buffer on the engine.
+
# Triadic subscriptions
The Book <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code</a> states (on chapter #3):
@@ -83,7 +92,7 @@ the operation better. Mind the Javascript objects!!!
Keep classes and functions as small as possible, break it into multiple "modules"... it is usually easier to
understand what is going on.
-_zen-http_ is _Zen_ for triadic handlers, includes proper result and error handlers and default 404 response. Connect and Stack compatible, really faster in real world use cases.
+_zen-http_ is _Zen_ for triadic handlers. It includes proper HTTP result and error handlers and default 404 response. Connect and Stack compatible, really faster in real world use cases.
Use `next(err)` to push a 500 error message to the client, `next(null,result)` to send the result with status 200.
## Benchmarks
View
@@ -1,6 +1,6 @@
{
"name": "zen",
- "version": "0.1.2",
+ "version": "0.1.3",
"description": "is a simple, safe, basic, fast, general purpose module engine",
"keywords": ["engine", "stack", "module", "wsgi", "rack", "next"],
"repository": "git://github.com/pblabs/zen.git",
View
@@ -211,4 +211,38 @@ this.core = {
test.done();
},
+ '14. Zen should resolve paused requests on resume ': function (test) {
+ var hw=function(a,b,next){next(null,'result')};
+ var zapp=zen(hw);
+ zapp.pause();
+
+ zapp.errorHandler=function(a,b,err){test.ok(false, 'The errorHandler was executed');};
+ zapp.resultHandler=function(a,b,res){test.ok(true, 'The resultHandler was executed');};
+
+ zapp(1,1);
+ zapp(2,2);
+ zapp(3,3);
+ zapp.resume();
+
+ test.expect(3);
+ test.done();
+
+ },
+ '15. Zen should delegate to errorHandler when stopped ': function (test) {
+ var hw=function(a,b,next){next(null,'result')};
+ var zapp=zen(hw);
+
+ zapp.errorHandler=function(a,b,err){test.ok(true, 'The errorHandler was executed');};
+ zapp.resultHandler=function(a,b,res){test.ok(true, 'The resultHandler was executed');};
+
+ zapp(1,1);
+ zapp.stop();
+ zapp(2,2);
+ zapp.resume();
+ zapp(3,3);
+
+ test.expect(3);
+ test.done();
+
+ },
};
View
@@ -287,4 +287,38 @@ this.core = {
test.done();
},
+ '20. Zen should resolve paused requests on resume ': function (test) {
+ var hw=function(a,next){next(null,'result')};
+ var zapp=zen(hw);
+ zapp.pause();
+
+ zapp.errorHandler=function(a,err){test.ok(false, 'The errorHandler was executed');};
+ zapp.resultHandler=function(a,res){test.ok(true, 'The resultHandler was executed');};
+
+ zapp(1);
+ zapp(2);
+ zapp(3);
+ zapp.resume();
+
+ test.expect(3);
+ test.done();
+
+ },
+ '21. Zen should delegate to errorHandler when stopped ': function (test) {
+ var hw=function(a,next){next(null,'result')};
+ var zapp=zen(hw);
+
+ zapp.errorHandler=function(err){test.ok(true, 'The errorHandler was executed');};
+ zapp.resultHandler=function(res){test.ok(true, 'The resultHandler was executed');};
+
+ zapp(1);
+ zapp.stop();
+ zapp(2);
+ zapp.resume();
+ zapp(3);
+
+ test.expect(3);
+ test.done();
+
+ },
};
View
@@ -46,8 +46,15 @@ module.exports= function (/*handlers*/) {
};
var handlers=Array.prototype.slice.call(arguments).reverse();
var L=handlers.length-1;
+
+ var _engineRequests=[];
+ var _enginePaused=false;
+ var _engineStopped=false;
// The real Zen Engine
- var engine= function (a,b) {
+ var engine= function (a,b) {
+ if (_enginePaused===true) {_engineRequests.push([a,b]);return;}
+ if (_engineStopped===true) {return defaultHandler(a,b);}
+
var i=L;
try {
function next (err,res) {
@@ -72,5 +79,20 @@ module.exports= function (/*handlers*/) {
engine=defaultHandler; //default
engine.errorHandler = errorHandler;
engine.resultHandler = resultHandler;
+
+ /*EXTRA FEATURE*/
+ engine.pause=function (){_enginePaused=true;};
+ engine.stop =function (){_engineStopped=true;};
+ engine.resume=function(){
+ _enginePaused=_engineStopped=false;
+ var requests= Array.prototype.slice.call(_engineRequests);
+ _engineRequests=[];
+ var request=requests.shift();
+ while(typeof request !=='undefined'){
+ engine(request[0],request[1]);
+ request=requests.shift();
+ }
+ };
+
return engine;
};
View
29 zen.js
@@ -44,8 +44,16 @@ module.exports= function (/*handlers*/) {
};
var handlers=Array.prototype.slice.call(arguments).reverse();
var L=handlers.length-1;
+
+ var _engineRequests=[];
+ var _enginePaused=false;
+ var _engineStopped=false;
// The real Zen Engine
var engine= function (/*handleArgs*/) {
+
+ if (_enginePaused===true) {_engineRequests.push(Array.prototype.slice.call(arguments));return;}
+ if (_engineStopped===true) {return defaultHandler.apply(this,Array.prototype.slice.call(arguments));}
+
var i=L;
try {
var handleArgs=Array.prototype.slice.call(arguments);
@@ -94,17 +102,34 @@ module.exports= function (/*handlers*/) {
return handle(handlers[i]);
} catch (err) {
try {
+ console.log(err.stack);
handleArgs.push(err);
- return engine.errorHandler.apply(self,handleArgs);
+ return engine.errorHandler.apply(this,handleArgs);
} catch (ex) {
handleArgs[handleArgs.length-1]=ex;
- return errorHandler.apply(self,handleArgs);
+ return errorHandler.apply(this,handleArgs);
}
}
}
if (L<0)
engine=defaultHandler; //default
engine.errorHandler = errorHandler;
engine.resultHandler = resultHandler;
+
+ /*EXTRA FEATURE*/
+ engine.pause=function (){_enginePaused=true;};
+ engine.stop =function (){_engineStopped=true;};
+ engine.resume=function(){
+ _enginePaused=_engineStopped=false;
+ var requests= Array.prototype.slice.call(_engineRequests);
+ _engineRequests=[];
+ var request=requests.shift();
+ while(typeof request !=='undefined'){
+ engine.apply(this,request);
+ request=requests.shift();
+ }
+ };
+
+
return engine;
};

0 comments on commit 8b6a888

Please sign in to comment.