Observe context changes in client components immediately after the first enrichment

This commit is contained in:
Andrew Kingston 2024-02-09 16:44:11 +00:00
parent 9109681bbe
commit f52379853f
1 changed files with 17 additions and 8 deletions

View File

@ -113,6 +113,9 @@
// List of context keys which we use inside bindings // List of context keys which we use inside bindings
let knownContextKeyMap = {} let knownContextKeyMap = {}
// Cleanup function to stop observing context changes when unmounting
let unobserve
// Set up initial state for each new component instance // Set up initial state for each new component instance
$: initialise(instance) $: initialise(instance)
@ -311,6 +314,11 @@
// Force an initial enrichment of the new settings // Force an initial enrichment of the new settings
enrichComponentSettings(get(context), settingsDefinitionMap) enrichComponentSettings(get(context), settingsDefinitionMap)
// Start observing changes in context now that we are initialised
if (!unobserve) {
unobserve = context.actions.observeChanges(handleContextChange)
}
} }
// Extracts a map of all context keys which are required by action settings // Extracts a map of all context keys which are required by action settings
@ -567,8 +575,8 @@
} }
} }
// Register an unregister component instance
onMount(() => { onMount(() => {
// Register this component instance for external access
if ($appStore.isDevApp) { if ($appStore.isDevApp) {
if (!componentStore.actions.isComponentRegistered(id)) { if (!componentStore.actions.isComponentRegistered(id)) {
componentStore.actions.registerInstance(id, { componentStore.actions.registerInstance(id, {
@ -581,16 +589,17 @@
state: store, state: store,
}) })
} }
return () => { }
if (componentStore.actions.isComponentRegistered(id)) { return () => {
componentStore.actions.unregisterInstance(id) // Unregister component
} if (componentStore.actions.isComponentRegistered(id)) {
componentStore.actions.unregisterInstance(id)
} }
// Stop observing context changes
unobserve?.()
} }
}) })
// Observe changes to context
onMount(() => context.actions.observeChanges(handleContextChange))
</script> </script>
{#if constructor && initialSettings && (visible || inSelectedPath) && !builderHidden} {#if constructor && initialSettings && (visible || inSelectedPath) && !builderHidden}