falcor
Version:
A JavaScript library for efficient data fetching.
61 lines (49 loc) • 2.13 kB
JavaScript
var InvalidDerefInputError = require("./../errors/InvalidDerefInputError");
var getCachePosition = require("./../get/getCachePosition");
var CONTAINER_DOES_NOT_EXIST = "e";
var $ref = require("./../types/ref");
module.exports = function deref(boundJSONArg) {
var absolutePath = boundJSONArg && boundJSONArg.$__path;
var refPath = boundJSONArg && boundJSONArg.$__refPath;
var toReference = boundJSONArg && boundJSONArg.$__toReference;
var referenceContainer;
// We deref and then ensure that the reference container is attached to
// the model.
if (absolutePath) {
var validContainer = CONTAINER_DOES_NOT_EXIST;
if (toReference) {
validContainer = false;
referenceContainer = getCachePosition(this, toReference);
// If the reference container is still a sentinel value then compare
// the reference value with refPath. If they are the same, then the
// model is still valid.
if (refPath && referenceContainer &&
referenceContainer.$type === $ref) {
var containerPath = referenceContainer.value;
var i = 0;
var len = refPath.length;
validContainer = true;
for (; validContainer && i < len; ++i) {
if (containerPath[i] !== refPath[i]) {
validContainer = false;
}
}
}
}
// Signal to the deref'd model that it has been disconnected from the
// graph or there is no _fromWhenceYouCame
if (!validContainer) {
referenceContainer = false;
}
// The container did not exist, therefore there is no reference
// container and fromWhenceYouCame should always return true.
else if (validContainer === CONTAINER_DOES_NOT_EXIST) {
referenceContainer = true;
}
return this._clone({
_path: absolutePath,
_referenceContainer: referenceContainer
});
}
throw new InvalidDerefInputError();
};