Skip to content

Commit

Permalink
new pause(), stop() and resume() APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
pibi committed Jun 21, 2011
1 parent f5c3c88 commit 8b6a888
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.markdown
Expand Up @@ -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 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 # Triadic subscriptions


The Book <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code</a> states (on chapter #3): The Book <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code</a> states (on chapter #3):
Expand All @@ -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 Keep classes and functions as small as possible, break it into multiple "modules"... it is usually easier to
understand what is going on. 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. Use `next(err)` to push a 500 error message to the client, `next(null,result)` to send the result with status 200.


## Benchmarks ## Benchmarks
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ {
"name": "zen", "name": "zen",
"version": "0.1.2", "version": "0.1.3",
"description": "is a simple, safe, basic, fast, general purpose module engine", "description": "is a simple, safe, basic, fast, general purpose module engine",
"keywords": ["engine", "stack", "module", "wsgi", "rack", "next"], "keywords": ["engine", "stack", "module", "wsgi", "rack", "next"],
"repository": "git://github.com/pblabs/zen.git", "repository": "git://github.com/pblabs/zen.git",
Expand Down
34 changes: 34 additions & 0 deletions test/zen-http.test.js
Expand Up @@ -211,4 +211,38 @@ this.core = {
test.done(); 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();

},
}; };
34 changes: 34 additions & 0 deletions test/zen.test.js
Expand Up @@ -283,6 +283,40 @@ this.core = {
}; };
test.doesNotThrow(function(){zapp();}); test.doesNotThrow(function(){zapp();});


test.expect(3);
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.expect(3);
test.done(); test.done();


Expand Down
24 changes: 23 additions & 1 deletion zen-http.js
Expand Up @@ -46,8 +46,15 @@ module.exports= function (/*handlers*/) {
}; };
var handlers=Array.prototype.slice.call(arguments).reverse(); var handlers=Array.prototype.slice.call(arguments).reverse();
var L=handlers.length-1; var L=handlers.length-1;

var _engineRequests=[];
var _enginePaused=false;
var _engineStopped=false;
// The real Zen Engine // 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; var i=L;
try { try {
function next (err,res) { function next (err,res) {
Expand All @@ -72,5 +79,20 @@ module.exports= function (/*handlers*/) {
engine=defaultHandler; //default engine=defaultHandler; //default
engine.errorHandler = errorHandler; engine.errorHandler = errorHandler;
engine.resultHandler = resultHandler; 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; return engine;
}; };
29 changes: 27 additions & 2 deletions zen.js
Expand Up @@ -44,8 +44,16 @@ module.exports= function (/*handlers*/) {
}; };
var handlers=Array.prototype.slice.call(arguments).reverse(); var handlers=Array.prototype.slice.call(arguments).reverse();
var L=handlers.length-1; var L=handlers.length-1;

var _engineRequests=[];
var _enginePaused=false;
var _engineStopped=false;
// The real Zen Engine // The real Zen Engine
var engine= function (/*handleArgs*/) { 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; var i=L;
try { try {
var handleArgs=Array.prototype.slice.call(arguments); var handleArgs=Array.prototype.slice.call(arguments);
Expand Down Expand Up @@ -94,17 +102,34 @@ module.exports= function (/*handlers*/) {
return handle(handlers[i]); return handle(handlers[i]);
} catch (err) { } catch (err) {
try { try {
console.log(err.stack);
handleArgs.push(err); handleArgs.push(err);
return engine.errorHandler.apply(self,handleArgs); return engine.errorHandler.apply(this,handleArgs);
} catch (ex) { } catch (ex) {
handleArgs[handleArgs.length-1]=ex; handleArgs[handleArgs.length-1]=ex;
return errorHandler.apply(self,handleArgs); return errorHandler.apply(this,handleArgs);
} }
} }
} }
if (L<0) if (L<0)
engine=defaultHandler; //default engine=defaultHandler; //default
engine.errorHandler = errorHandler; engine.errorHandler = errorHandler;
engine.resultHandler = resultHandler; 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; return engine;
}; };

0 comments on commit 8b6a888

Please sign in to comment.