falcor-router
Version:
A router DataSource constructor for falcor that allows you to model all your cloud data sources as a single JSON resource.
57 lines (49 loc) • 1.56 kB
JavaScript
var cloneArray = require('./../support/cloneArray');
var $ref = require('./../support/types').$ref;
var errors = require('./../exceptions');
/**
* performs the simplified cache reference follow. This
* differs from get as there is just following and reporting,
* not much else.
*
* @param {Object} cacheRoot
* @param {Array} ref
*/
module.exports = function followReference(cacheRoot, ref, maxRefFollow) {
var current = cacheRoot;
var refPath = ref;
var depth = -1;
var length = refPath.length;
var key, next, type;
var referenceCount = 0;
while (++depth < length) {
key = refPath[depth];
next = current[key];
type = next && next.$type;
if (!next || type && type !== $ref) {
current = next;
break;
}
// Show stopper exception. This route is malformed.
if (type && type === $ref && depth + 1 < length) {
var err = new Error(errors.innerReferences);
err.throwToNext = true;
throw err;
}
// potentially follow reference
if (depth + 1 === length) {
if (type === $ref) {
depth = -1;
refPath = next.value;
length = refPath.length;
next = cacheRoot;
referenceCount++;
}
if (referenceCount > maxRefFollow) {
throw new Error(errors.circularReference);
}
}
current = next;
}
return [current, cloneArray(refPath)];
};