falcor-router
Version:
A router DataSource constructor for falcor that allows you to model all your cloud data sources as a single JSON resource.
50 lines (40 loc) • 1.36 kB
JavaScript
var Observable = require('../../RouterRx.js').Observable;
var getExecutableMatches = require('./getExecutableMatches');
/**
* Sorts and strips the set of available matches given the pathSet.
*/
module.exports = function runByPrecedence(pathSet, matches, actionRunner) {
// Precendence matching
var sortedMatches = matches.
sort(function(a, b) {
if (a.precedence < b.precedence) {
return 1;
} else if (a.precedence > b.precedence) {
return -1;
}
return 0;
});
var execs = getExecutableMatches(sortedMatches, [pathSet]);
var setOfMatchedPaths = Observable.
from(execs.matchAndPaths).
flatMap(actionRunner).
// Note: We do not wait for each observable to finish,
// but repeat the cycle per onNext.
map(function(actionTuple) {
return {
match: actionTuple[0],
value: actionTuple[1]
};
});
if (execs.unhandledPaths) {
setOfMatchedPaths = setOfMatchedPaths.
concat(Observable.of({
match: {suffix: []},
value: {
isMessage: true,
unhandledPaths: execs.unhandledPaths
}
}));
}
return setOfMatchedPaths;
};