Skip to content

Commit

Permalink
feat(methodSummary hooking): add methodSummary hooking to set and call
Browse files Browse the repository at this point in the history
also refactors get methodSummary hooking for better consistency across
methods.
  • Loading branch information
benlesh committed Jan 27, 2017
1 parent ab60f3e commit 521fff3
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 93 deletions.
149 changes: 94 additions & 55 deletions src/router/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,112 @@ module.exports = function routerCall(callPath, args,
refPathsArg, thisPathsArg) {
var router = this;

var source = Observable.defer(function() {

var refPaths = normalizePathSets(refPathsArg || []);
var thisPaths = normalizePathSets(thisPathsArg || []);
var jsongCache = {};
var action = runCallAction(router, callPath, args,
refPaths, thisPaths, jsongCache);
var callPaths = [callPath];

if (getPathsCount(refPaths) +
getPathsCount(thisPaths) +
getPathsCount(callPaths) >
router.maxPaths) {
throw new MaxPathsExceededError();
var source = Observable.defer(function () {
var methodSummary;
if (router._methodSummaryHook) {
methodSummary = {
method: 'call',
start: router._now(),
callPath: callPath,
args: args,
refPaths: refPathsArg,
thisPaths: thisPathsArg
};
}

return recurseMatchAndExecute(router._matcher, action, callPaths, call,
router, jsongCache).
var innerSource = Observable.defer(function() {

// Take that
map(function(jsongResult) {
var reportedPaths = jsongResult.reportedPaths;
var jsongEnv = {
jsonGraph: jsongResult.jsonGraph
};
var refPaths = normalizePathSets(refPathsArg || []);
var thisPaths = normalizePathSets(thisPathsArg || []);
var jsongCache = {};
var action = runCallAction(router, callPath, args,
refPaths, thisPaths, jsongCache, methodSummary);
var callPaths = [callPath];

// Call must report the paths that have been produced.
if (reportedPaths.length) {
if (getPathsCount(refPaths) +
getPathsCount(thisPaths) +
getPathsCount(callPaths) >
router.maxPaths) {
throw new MaxPathsExceededError();
}

return recurseMatchAndExecute(router._matcher, action,
callPaths, call,
router, jsongCache).

// Take that
map(function(jsongResult) {
var reportedPaths = jsongResult.reportedPaths;
var jsongEnv = {
jsonGraph: jsongResult.jsonGraph
};

// Call must report the paths that have been produced.
if (reportedPaths.length) {
// Collapse the reported paths as they may be inefficient
// to send across the wire.
jsongEnv.paths = collapse(reportedPaths);
}
else {
jsongEnv.paths = [];
jsongEnv.jsonGraph = {};
}

// add the invalidated paths to the jsonGraph Envelope
var invalidated = jsongResult.invalidated;
if (invalidated && invalidated.length) {
jsongEnv.invalidated = invalidated;
}

// Calls are currently materialized.
materialize(router, reportedPaths, jsongEnv);
return jsongEnv;
}).
jsongEnv.paths = collapse(reportedPaths);
}
else {
jsongEnv.paths = [];
jsongEnv.jsonGraph = {};
}

// add the invalidated paths to the jsonGraph Envelope
var invalidated = jsongResult.invalidated;
if (invalidated && invalidated.length) {
jsongEnv.invalidated = invalidated;
}

// Calls are currently materialized.
materialize(router, reportedPaths, jsongEnv);
return jsongEnv;
}).

// For us to be able to chain call requests then the error that is
// caught has to be a 'function does not exist.' error. From that
// we will try the next dataSource in the line.
catch(function catchException(e) {
if (e instanceof CallNotFoundError && router._unhandled) {
return router._unhandled.
call(callPath, args, refPaths, thisPaths);
}
throw e;
});
catch(function catchException(e) {
if (e instanceof CallNotFoundError && router._unhandled) {
return router._unhandled.
call(callPath, args, refPaths, thisPaths);
}
throw e;
});
});

if (router._methodSummaryHook || router._errorHook) {
innerSource = innerSource.
do(function (response) {
if (router._methodSummaryHook) {
methodSummary.responses = methodSummary.responses || [];
methodSummary.responses.push(response);
}
}, function (err) {
if (router._methodSummaryHook) {
methodSummary.error = err;
methodSummary.end = router._now();
router._methodSummaryHook(methodSummary);
}
if (router._errorHook) {
router._errorHook(err);
}
}, function () {
if (router._methodSummaryHook) {
methodSummary.end = router._now();
router._methodSummaryHook(methodSummary);
}
});
}

return innerSource
});


if (router._errorHook) {
source = source.
do(null, function summaryHookErrorHandler(err) {
router._errorHook(err);
});
}

return rxNewToRxNewAndOld(source);




return rxNewToRxNewAndOld(source);
};
6 changes: 2 additions & 4 deletions src/router/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ module.exports = function routerGet(paths) {
});

if (router._methodSummaryHook || router._errorHook) {
var responses;
result = result.
do(function (response) {
if (router._methodSummaryHook) {
responses = responses || [];
responses.push(response);
methodSummary.responses = methodSummary.responses || [];
methodSummary.responses.push(response);
}
}, function (err) {
if (router._methodSummaryHook) {
Expand All @@ -98,7 +97,6 @@ module.exports = function routerGet(paths) {
}, function () {
if (router._methodSummaryHook) {
methodSummary.end = router._now();
methodSummary.responses = responses;
router._methodSummaryHook(methodSummary);
}
});
Expand Down
44 changes: 41 additions & 3 deletions src/router/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ module.exports = function routerSet(jsonGraph) {

var source = Observable.defer(function() {
var jsongCache = {};
var action = runSetAction(router, jsonGraph, jsongCache);

var methodSummary;
if (router._methodSummaryHook) {
methodSummary = {
method: 'set',
jsonGraph: jsonGraph,
start: router._now()
};
}

var action = runSetAction(router, jsonGraph, jsongCache, methodSummary);
jsonGraph.paths = normalizePathSets(jsonGraph.paths);

if (getPathsCount(jsonGraph.paths) > router.maxPaths) {
throw new MaxPathsExceededError();
}

return recurseMatchAndExecute(router._matcher, action, jsonGraph.paths,
set, router, jsongCache).
var innerSource = recurseMatchAndExecute(router._matcher, action,
jsonGraph.paths, set, router, jsongCache).

// Takes the jsonGraphEnvelope and extra details that comes out
// of the recursive matching algorithm and either attempts the
Expand Down Expand Up @@ -153,6 +163,34 @@ module.exports = function routerSet(jsonGraph) {
map(function(jsonGraphEnvelope) {
return materialize(router, jsonGraph.paths, jsonGraphEnvelope);
});

if (router._errorHook || router._methodSummaryHook) {
innerSource = innerSource.
do(
function (response) {
if (router._methodSummaryHook) {
methodSummary.responses =
methodSummary.responses || [];
methodSummary.responses.push(response);
}
}, function (err) {
if (router._methodSummaryHook) {
methodSummary.end = router._now();
methodSummary.error = err;
router._methodSummaryHook(methodSummary);
}
if (router._errorHook) {
router._errorHook(err)
}
}, function () {
if (router._methodSummaryHook) {
methodSummary.end = router._now();
router._methodSummaryHook(methodSummary);
}
}
);
}
return innerSource;
});

if (router._errorHook) {
Expand Down
36 changes: 30 additions & 6 deletions src/run/call/runCallAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ var Observable = require('../../RouterRx.js').Observable;
module.exports = outerRunCallAction;

function outerRunCallAction(routerInstance, callPath, args,
suffixes, paths, jsongCache) {
suffixes, paths, jsongCache, methodSummary) {
return function innerRunCallAction(matchAndPath) {
return runCallAction(matchAndPath, routerInstance, callPath,
args, suffixes, paths, jsongCache);
args, suffixes, paths, jsongCache, methodSummary);
};
}

function runCallAction(matchAndPath, routerInstance, callPath, args,
suffixes, paths, jsongCache) {
suffixes, paths, jsongCache, methodSummary) {

var match = matchAndPath.match;
var matchedPath = matchAndPath.path;
Expand All @@ -29,7 +29,7 @@ function runCallAction(matchAndPath, routerInstance, callPath, args,
// This is where things get interesting
out = Observable.
defer(function() {
var next;
var next;
try {
next = match.
action.call(
Expand All @@ -38,8 +38,32 @@ function runCallAction(matchAndPath, routerInstance, callPath, args,
e.throwToNext = true;
throw e;
}
return outputToObservable(next).
toArray();
var output = outputToObservable(next);

if (methodSummary) {
var route = {
start: routerInstance._now(),
route: matchAndPath.match.prettyRoute,
paths: matchAndPath.path
};
methodSummary.routes = methodSummary.routes || [];
methodSummary.routes.push(route);

output = output.do(
function (response) {
route.responses = route.responses || [];
route.responses.push(response);
},
function (err) {
route.error = err;
route.end = routerInstance._now();
},
function () {
route.end = routerInstance._now();
}
)
}
return output.toArray();
}).

// Required to get the references from the outputting jsong
Expand Down
34 changes: 12 additions & 22 deletions src/run/get/runGetAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,21 @@ function getAction(routerInstance, matchAndPath, jsongCache, methodSummary) {
if (methodSummary) {
var _out = out;
out = Observable.defer(function () {
var start = routerInstance._now();
var responses = [];
var route = {
start: routerInstance._now(),
route: matchAndPath.match.prettyRoute,
paths: matchAndPath.path
};
methodSummary.routes = methodSummary.routes || [];
methodSummary.routes.push(route);
return _out.do(function (response) {
responses.push(response);
route.responses = route.responses || [];
route.responses.push(response);
}, function (err) {
responses.push({ error: err });
var end = routerInstance._now();
methodSummary.routes = methodSummary.routes || [];
methodSummary.routes.push({
route: matchAndPath.match.prettyRoute,
start: start,
end: end,
error: err,
paths: matchAndPath.path
});
route.error = err;
route.end = routerInstance._now();
}, function () {
var end = routerInstance._now();
methodSummary.routes = methodSummary.routes || [];
methodSummary.routes.push({
route: matchAndPath.match.prettyRoute,
start: start,
end: end,
responses: responses,
paths: matchAndPath.path
});
route.end = routerInstance._now();
});
})
}
Expand Down
Loading

0 comments on commit 521fff3

Please sign in to comment.