From 86065084c1c8689d096803243dfe8d1d8f9e3edd Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 17 Dec 2021 08:22:40 +0000 Subject: [PATCH] Update core component handling to use svelte function to manually control props in order to avoid triggering additional reactive statements and improve performance --- .../client/src/components/Component.svelte | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index d8cdbd3f53..2ce97ee5fd 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -19,6 +19,9 @@ export let isScreen = false export let isBlock = false + // Ref to the svelte component + let ref + // Component settings are the un-enriched settings for this component that // need to be enriched at this level. // Nested settings are the un-enriched block settings that are to be passed on @@ -129,6 +132,11 @@ editing, }) + // Set initial props when the component mounts + $: if (ref?.$set) { + assignAllSettings() + } + // Extracts all settings from the component instance const getRawSettings = instance => { let validSettings = {} @@ -267,16 +275,30 @@ const cacheSettings = (enriched, nested, conditional) => { const allSettings = { ...enriched, ...nested, ...conditional } if (!cachedSettings) { - cachedSettings = allSettings + cachedSettings = { ...allSettings } } else { Object.keys(allSettings).forEach(key => { - if (!propsAreSame(allSettings[key], cachedSettings[key])) { + const same = propsAreSame(allSettings[key], cachedSettings[key]) + if (!same) { cachedSettings[key] = allSettings[key] + assignSetting(key, allSettings[key]) } }) } } + // Assigns the full set of settings to this component + const assignAllSettings = () => { + ref?.$set?.({ ...cachedSettings }) + } + + // Assigns a certain setting to this component. + // We manually use the svelte $set function to avoid triggering additional + // reactive statements. + const assignSetting = (key, value) => { + ref?.$$set?.({ [key]: value }) + } + // Generates a key used to determine when components need to fully remount. // Currently only toggling editing requires remounting. const getRenderKey = (id, editing) => { @@ -299,7 +321,7 @@ data-id={id} data-name={name} > - + {#if children.length} {#each children as child (child._id)}