Skip to content

Commit

Permalink
setting the prototype inside the constructor, will do this at each in…
Browse files Browse the repository at this point in the history
…stanciation. We don't need the context at that step so we can extract this loop and use a factory function to make our method gateways
  • Loading branch information
FGRibreau committed Oct 27, 2014
1 parent 581179a commit ea0f474
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ function Request(options, f, maxAttempts, retryDelay) {
this.f = _.once(f);
this._timeout = null;
this._req = null;

// expose _req methods from Request
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe'].forEach(function (methodName) {
Request.prototype[methodName] = function () {
this.exposeReqFunction(methodName, Array.prototype.slice.call(arguments, 0));
}.bind(this);
}.bind(this));
}

Request.request = request;
Expand Down Expand Up @@ -62,11 +55,16 @@ Request.prototype.abort = function () {
this.f(new Error('Aborted'));
};

Request.prototype.exposeReqFunction = function (name) {
if (this._req) {
this._req[name].apply(this._req, Array.prototype.slice.call(arguments, 1)[0]);
}
};
// expose request methods from RequestRetry
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe'].forEach(function (methodName) {
Request.prototype[methodName] = makeGateway(methodName);
});

function makeGateway(methodName) {
return function () {
return this._req[methodName].apply(this._req, Array.prototype.slice.call(arguments));
};
}

function Factory(options, f) {
f = _.isFunction(f) ? f : _.noop;
Expand Down

0 comments on commit ea0f474

Please sign in to comment.