package.lib.hooks.useCache.js Maven / Gradle / Ivy
The newest version!
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var React = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* Cache `value` related LabeledValue & options.
*/
var _default = exports.default = function _default(labeledValues, valueOptions) {
var cacheRef = React.useRef({
values: new Map(),
options: new Map()
});
var filledLabeledValues = React.useMemo(function () {
var _cacheRef$current = cacheRef.current,
prevValueCache = _cacheRef$current.values,
prevOptionCache = _cacheRef$current.options;
// Fill label by cache
var patchedValues = labeledValues.map(function (item) {
if (item.label === undefined) {
var _prevValueCache$get;
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item), {}, {
label: (_prevValueCache$get = prevValueCache.get(item.value)) === null || _prevValueCache$get === void 0 ? void 0 : _prevValueCache$get.label
});
}
return item;
});
// Refresh cache
var valueCache = new Map();
var optionCache = new Map();
patchedValues.forEach(function (item) {
valueCache.set(item.value, item);
optionCache.set(item.value, valueOptions.get(item.value) || prevOptionCache.get(item.value));
});
cacheRef.current.values = valueCache;
cacheRef.current.options = optionCache;
return patchedValues;
}, [labeledValues, valueOptions]);
var getOption = React.useCallback(function (val) {
return valueOptions.get(val) || cacheRef.current.options.get(val);
}, [valueOptions]);
return [filledLabeledValues, getOption];
};
© 2015 - 2024 Weber Informatics LLC | Privacy Policy