@react-awesome-query-builder/core
Version:
User-friendly query builder for React. Core
187 lines (186 loc) • 8.05 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCommonMemo = exports.findExtendedConfigInAllMemos = exports.createConfigMemo = exports.areConfigsSame = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _pick = _interopRequireDefault(require("lodash/pick"));
var _configUtils = require("./configUtils");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var memoId = 0;
var configId = 0;
var commonMemo;
var memos = {};
var areConfigsSame = exports.areConfigsSame = function areConfigsSame(config1, config2) {
return _configUtils.configKeys.map(function (k) {
return config1[k] === config2[k];
}).filter(function (v) {
return !v;
}).length === 0;
};
var getCommonMemo = exports.getCommonMemo = function getCommonMemo(extendConfig) {
if (!commonMemo) {
commonMemo = createConfigMemo({
reactIndex: undefined,
maxSize: 3,
canCompile: undefined,
// default is true
extendConfig: extendConfig
});
}
return commonMemo;
};
var findExtendedConfigInAllMemos = exports.findExtendedConfigInAllMemos = function findExtendedConfigInAllMemos(config, needsToBeCompiled) {
var foundExtConfig;
for (var k in memos) {
var found = memos[k].findExtendedConfig(config, needsToBeCompiled);
if (found) {
foundExtConfig = found;
break;
}
}
return foundExtConfig;
};
var createConfigMemo = exports.createConfigMemo = function createConfigMemo() {
var meta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
reactIndex: undefined,
maxSize: 2,
// current and prev
canCompile: true,
extendConfig: undefined // should be passed!
};
var configStore = new Map();
var maxSize = meta.maxSize || 2;
var currentMemoId = ++memoId;
var currentMemo;
var isActive = true;
var pickConfig = function pickConfig(props) {
return (0, _pick["default"])(props, _configUtils.configKeys);
};
var extendAndStore = function extendAndStore(config) {
var extendedConfig = meta.extendConfig(config, ++configId, meta.canCompile);
storeConfigPair(config, extendedConfig);
return extendedConfig;
};
var getSize = function getSize() {
return configStore.size;
};
var storeConfigPair = function storeConfigPair(config, extendedConfig) {
if (configStore.size + 1 > maxSize) {
configStore["delete"](configStore.keys().next().value);
}
// Note: because of desctructing, strict find would not be possible
// (see commented line in `findExtended`)
// (see issue #1187)
configStore.set(_objectSpread({}, config), extendedConfig);
};
var findBasic = function findBasic(findConfig) {
var _iterator = _createForOfIteratorHelper(configStore.keys()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var basicConfig = _step.value;
var extConfig = configStore.get(basicConfig);
var found = areConfigsSame(extConfig, findConfig);
if (found) {
return basicConfig;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return findConfig;
};
var findExtended = function findExtended(findConfig, needsToBeCompiled) {
// strict find:
// return configStore.get(findConfig) || configStore.values().find(ec => ec === findConfig);
var _iterator2 = _createForOfIteratorHelper(configStore.keys()),
_step2;
try {
var _loop = function _loop() {
var savedConfig = _step2.value;
var foundParts = _configUtils.configKeys.filter(function (k) {
return savedConfig[k] === findConfig[k];
});
var found = foundParts.length === _configUtils.configKeys.length && (needsToBeCompiled ? savedConfig.__compliled : true);
if (found) {
return {
v: configStore.get(savedConfig)
};
}
},
_ret;
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
_ret = _loop();
if (_ret) return _ret.v;
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
var _iterator3 = _createForOfIteratorHelper(configStore.values()),
_step3;
try {
var _loop2 = function _loop2() {
var extendedConfig = _step3.value;
var foundParts = _configUtils.configKeys.filter(function (k) {
return extendedConfig[k] === findConfig[k];
});
var found = foundParts.length === _configUtils.configKeys.length && (needsToBeCompiled ? extendedConfig.__compliled : true);
if (found) {
return {
v: extendedConfig
};
}
},
_ret2;
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
_ret2 = _loop2();
if (_ret2) return _ret2.v;
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return null;
};
var findOrExtend = function findOrExtend(config) {
return findExtended(config) || extendAndStore(config);
};
var clearConfigMemo = function clearConfigMemo() {
isActive = false;
configStore.clear();
delete memos[currentMemoId];
if (commonMemo === currentMemo) {
commonMemo = undefined;
}
};
currentMemo = {
getExtendedConfig: function getExtendedConfig(props) {
return findOrExtend(pickConfig(props));
},
findExtendedConfig: findExtended,
getBasicConfig: findBasic,
clearConfigMemo: clearConfigMemo,
configId: configId,
storeConfigPair: storeConfigPair,
getSize: getSize,
configStore: configStore,
memoId: currentMemoId,
meta: meta
};
if (meta.reactIndex === undefined) {
commonMemo = currentMemo;
}
memos[currentMemoId] = currentMemo;
return currentMemo;
};