UNPKG

falcor

Version:

A JavaScript library for efficient data fetching.

72 lines (58 loc) 1.82 kB
var isInternalKey = require("./../support/isInternalKey"); /** * decends and copies the cache. */ module.exports = function getCache(cache) { var out = {}; _copyCache(cache, out); return out; }; function cloneBoxedValue(boxedValue) { var clonedValue = {}; var keys = Object.keys(boxedValue); var key; var i; var l; for (i = 0, l = keys.length; i < l; i++) { key = keys[i]; if (!isInternalKey(key)) { clonedValue[key] = boxedValue[key]; } } return clonedValue; } function _copyCache(node, out, fromKey) { // copy and return Object. keys(node). filter(function(k) { // Its not an internal key and the node has a value. In the cache // there are 3 possibilities for values. // 1: A branch node. // 2: A $type-value node. // 3: undefined // We will strip out 3 return !isInternalKey(k) && node[k] !== undefined; }). forEach(function(key) { var cacheNext = node[key]; var outNext = out[key]; if (!outNext) { outNext = out[key] = {}; } // Paste the node into the out cache. if (cacheNext.$type) { var isObject = cacheNext.value && typeof cacheNext.value === "object"; var isUserCreatedcacheNext = !cacheNext.$_modelCreated; var value; if (isObject || isUserCreatedcacheNext) { value = cloneBoxedValue(cacheNext); } else { value = cacheNext.value; } out[key] = value; return; } _copyCache(cacheNext, outNext, key); }); }