Skip to content

Commit

Permalink
Cache mapping rules
Browse files Browse the repository at this point in the history
  • Loading branch information
holubv committed Apr 21, 2021
1 parent e5cfe9a commit 7653a20
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/SmartComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ import Utils from "./Utils";

export default class SmartComponents {

static componentCache = {};

static _cached(q, form, componentName, mapRule) {

let cachedQuestion = SmartComponents.componentCache[q['@id']];
if (cachedQuestion === undefined) {
cachedQuestion = {};
SmartComponents.componentCache[q['@id']] = {};
}

let cachedValue = cachedQuestion[componentName];
if (cachedValue === undefined) {
cachedValue = mapRule(q, form);
SmartComponents.componentCache[q['@id']][componentName] = cachedValue;
}

return cachedValue;
}

static getComponentMapping() {
return [
{
Expand All @@ -30,14 +49,10 @@ export default class SmartComponents {
},
{
component: NullQuestion,
mapRule: (q, form) => {
mapRule: (q, form) => SmartComponents._cached(q, form, 'NullQuestion', () => {
const parent = Utils.findParent(form?.root, q['@id']);
if (parent && Utils.isReferencedBySibling(parent, q['@id'], Constants.HAS_UNIT_OF_MEASURE)) {
return true;
}

return false;
}
return !!(parent && Utils.isReferencedBySibling(parent, q['@id'], Constants.HAS_UNIT_OF_MEASURE));
})
},
{
component: QuestionWithUnit,
Expand Down

0 comments on commit 7653a20

Please sign in to comment.