dash-renderer
Version:
render dash components in react
103 lines (100 loc) • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.selectConfig = selectConfig;
exports.selectDashProps = void 0;
exports.selectDashPropsEqualityFn = selectDashPropsEqualityFn;
var _wrapping = require("./wrapping");
var _ramda = require("ramda");
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var previousHashes = {};
var isFirstLevelPropsChild = (updatedPath, strPath) => {
var updatedSegments = updatedPath.split(',');
var fullSegments = strPath.split(',');
// Check that strPath actually starts with updatedPath
var startsWithPath = fullSegments.every((seg, i) => updatedSegments[i] === seg);
if (!startsWithPath) return [false, []];
// Get the remaining path after the prefix
var remainingSegments = updatedSegments.slice(fullSegments.length);
var propsCount = remainingSegments.filter(s => s === 'props').length;
return [propsCount < 2, remainingSegments];
};
function determineChangedProps(state, strPath) {
var combinedHash = 0;
var renderType; // Default render type, adjust as needed
var changedProps = {};
Object.entries(state.layoutHashes).forEach(_ref => {
var _ref2 = _slicedToArray(_ref, 2),
updatedPath = _ref2[0],
pathHash = _ref2[1];
var _isFirstLevelPropsChi = isFirstLevelPropsChild(updatedPath, strPath),
_isFirstLevelPropsChi2 = _slicedToArray(_isFirstLevelPropsChi, 2),
descendant = _isFirstLevelPropsChi2[0],
remainingSegments = _isFirstLevelPropsChi2[1];
if (descendant) {
var previousHash = (0, _ramda.pathOr)({}, [updatedPath], previousHashes);
combinedHash += (0, _ramda.pathOr)(0, ['hash'], pathHash);
if (previousHash !== pathHash) {
if (updatedPath !== strPath) {
Object.assign(changedProps, {
[remainingSegments[1]]: true
});
renderType = 'components';
} else {
Object.assign(changedProps, (0, _ramda.pathOr)({}, ['changedProps'], pathHash));
renderType = (0, _ramda.pathOr)({}, ['renderType'], pathHash);
}
previousHashes[updatedPath] = pathHash;
}
}
});
return {
hash: combinedHash,
changedProps,
renderType
};
}
var selectDashProps = componentPath => state => {
var c = (0, _wrapping.getComponentLayout)(componentPath, state);
// Layout hashes records the number of times a path has been updated.
// sum with the parents hash (match without the last ']') to get the real hash
// Then it can be easily compared without having to compare the props.
var strPath = (0, _wrapping.stringifyPath)(componentPath);
var hash;
if ((0, _wrapping.checkDashChildrenUpdate)(c)) {
hash = determineChangedProps(state, strPath);
} else {
hash = state.layoutHashes[strPath];
}
var h = 0;
var changedProps = {};
var renderType = '';
if (hash) {
h = hash['hash'];
changedProps = hash['changedProps'];
renderType = hash['renderType'];
}
return [c, c === null || c === void 0 ? void 0 : c.props, h, changedProps, renderType];
};
exports.selectDashProps = selectDashProps;
function selectDashPropsEqualityFn(_ref3, _ref4) {
var _ref5 = _slicedToArray(_ref3, 3),
_ = _ref5[0],
__ = _ref5[1],
hash = _ref5[2];
var _ref6 = _slicedToArray(_ref4, 3),
___ = _ref6[0],
____ = _ref6[1],
previousHash = _ref6[2];
// Only need to compare the hash as any change is summed up
return hash === previousHash;
}
function selectConfig(state) {
return state.config;
}