Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handler.prototype.setup() renamed to handlerSetup and changed behavio…

…r to allow to use it for handler factories, Resource now uses resolveHandler() method from Chain to prepare handlers
  • Loading branch information...
commit 67daf76f6c6a738bef555769bc874182191d8d1d 1 parent 410c318
@dimsmol authored
View
40 lib/handlers/chain.js
@@ -4,11 +4,9 @@ var Handler = require('./core/handler');
var Custom = require('./custom');
-var Chain = function (resource) {
+var Chain = function () {
Handler.call(this);
- this.resource = resource;
-
this.handlers = [];
this.info = { // can contain flags set by handlers
core: {} // reserved for apis handlers
@@ -24,19 +22,37 @@ Chain.prototype.handleRequest = function (ctx) {
};
Chain.prototype.add = function (handler) {
+ handler = Chain.resolveHandler(handler, this);
if (handler != null) {
- if (handler.constructor === Function) {
- handler = new Custom(handler);
- }
- var setupResult = handler.setup(this);
- if (setupResult !== false) {
- this.handlers.push(handler);
- }
+ this.handlers.push(handler);
}
};
-Chain.chain = function (resource) {
- return new Chain(resource);
+Chain.chain = function () {
+ return new Chain();
+};
+
+Chain.resolveHandler = function (handler, opt_container) {
+ var result = null;
+ if (handler != null) {
+ if (Array.isArray(handler)) {
+ result = new Chain();
+ for (var i = 0; i < handler.length; i++) {
+ result.add(handler[i]);
+ }
+ }
+ else if (handler.constructor === Function) {
+ result = new Custom(handler);
+ }
+ else {
+ result = handler;
+ }
+
+ if (result.handlerSetup != null) {
+ result = result.handlerSetup(opt_container);
+ }
+ }
+ return result;
};
View
5 lib/handlers/core/handler.js
@@ -25,8 +25,9 @@ Handler.prototype.handleError = function (ctx) {
ctx.next();
};
-Handler.prototype.setup = function (handlerContainer) {
- // NOTE returning false will skip including into chain
+Handler.prototype.handlerSetup = function (handlerContainer) {
+ // returning null will exclude handler
+ return this;
};
View
5 lib/handlers/custom.js
@@ -14,10 +14,13 @@ inherits(Custom, Handler);
Custom.prototype.name = 'Custom';
-Custom.prototype.setup = function (handlerContainer) {
+Custom.prototype.handlerSetup = function (handlerContainer) {
if (this.setupFunc) {
return this.setupFunc(handlerContainer);
}
+ else {
+ return this;
+ }
};
Custom.prototype.handleRequest = function (ctx) {
View
10 lib/handlers/impl.js
@@ -13,12 +13,12 @@ inherits(Impl, Handler);
Impl.prototype.name = 'Impl';
-Impl.prototype.setup = function (handlerContainer) {
+Impl.prototype.handlerSetup = function (handlerContainer) {
var found = false;
var info = handlerContainer.info;
- if (info && info.core) {
+ if (info != null && info.core != null) {
var ret = info.core.ret;
- if (ret) {
+ if (ret != null) {
found = true;
ret.setImpl(this.impl, this.isSync);
}
@@ -28,8 +28,8 @@ Impl.prototype.setup = function (handlerContainer) {
throw new Error('Impl requires Ret');
}
- // don't include in chain
- return false;
+ // exclude handler itself
+ return null;
};
View
12 lib/handlers/ret.js
@@ -31,20 +31,22 @@ Ret.prototype.setImpl = function (impl, isSync) {
this.implIsSync = isSync;
}
else {
- throw new Error('Impl already set');
+ throw new Error('Impl is already set');
}
};
-Ret.prototype.setup = function (handlerContainer) {
- var info = handlerContainer.info;
- if (info) {
- if (info.core.ret) {
+Ret.prototype.handlerSetup = function (handlerContainer) {
+ if (handlerContainer != null && handlerContainer.info != null) {
+ var info = handlerContainer.info;
+ if (info.core != null && info.core.ret != null) {
throw new Error('Multiple Ret within handler container');
}
else {
+ info.core = info.core || {};
info.core.ret = this;
}
}
+ return this;
};
Ret.prototype.handleRequest = function (ctx) {
View
21 lib/resources/resource.js
@@ -2,7 +2,6 @@
var valid = require('valid');
var MethodNotAllowed = require('../errors').MethodNotAllowed;
var Chain = require('../handlers/chain');
-var Custom = require('../handlers/custom');
var impls = require('../handlers/impl').impls;
var ret = require('../handlers/ret').ret;
var subpaths = require('../tools/path').subpaths;
@@ -53,24 +52,8 @@ Resource.prototype.createMethodMapper = function () {
return new MethodMapper(this.handlers);
};
-Resource.prototype.prepareHandler = function (handlers) {
- var result;
- if (handlers == null) {
- result = null;
- }
- else if (Array.isArray(handlers)) {
- result = new Chain(this);
- for (var i = 0; i < handlers.length; i++) {
- result.add(handlers[i]);
- }
- }
- else if (handlers.constructor === Function) {
- result = new Custom(handlers);
- }
- else {
- result = handlers;
- }
- return result;
+Resource.prototype.prepareHandler = function (handler) {
+ return Chain.resolveHandler(handler);
};
Resource.prototype.resolve = function (ctx) {
Please sign in to comment.
Something went wrong with that request. Please try again.