From 69dae35e2ab5bf18056024adbb732d5dba24e51e Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 31 Jan 2024 16:06:20 +0000 Subject: [PATCH] Ensure component settings is always an array and improve performance by minimizing store retrievals --- .../src/stores/builder/components/index.js | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/builder/src/stores/builder/components/index.js b/packages/builder/src/stores/builder/components/index.js index 429fdfa062..9a4ffde6f6 100644 --- a/packages/builder/src/stores/builder/components/index.js +++ b/packages/builder/src/stores/builder/components/index.js @@ -1080,11 +1080,11 @@ export class ComponentStore extends BudiStore { * @param {object} definition * @example * '@budibase/standard-components/container' - * @returns {boolean} + * @returns {array} the settings */ cacheSettings(componentType, definition) { let settings = [] - if (definition && componentType) { + if (definition) { settings = definition.settings?.filter(setting => !setting.section) ?? [] definition.settings ?.filter(setting => setting.section) @@ -1096,14 +1096,15 @@ export class ComponentStore extends BudiStore { })) ) }) - this.update(state => ({ - ...state, - settingsCache: { - ...state.settingsCache, - [componentType]: settings, - }, - })) } + this.update(state => ({ + ...state, + settingsCache: { + ...state.settingsCache, + [componentType]: settings, + }, + })) + return settings } /** @@ -1129,13 +1130,15 @@ export class ComponentStore extends BudiStore { componentType = `@budibase/standard-components/${componentType}` } - if (this.isCached(componentType)) { - return get(this.store).settingsCache[componentType] - } else { - const def = this.getDefinition(componentType) - this.cacheSettings(componentType, def) - return get(this.store).settingsCache[componentType] + // Use cached value if possible + const cachedValue = get(this.store).settingsCache[componentType] + if (cachedValue) { + return cachedValue } + + // Otherwise cache and return new value + const def = this.getDefinition(componentType) + return this.cacheSettings(componentType, def) } }