1
0
Fork 0
mirror of synced 2024-06-30 03:50:37 +12:00

Ensure client data contexts are correctly updated if a parent context is updated

This commit is contained in:
Andrew Kingston 2021-02-09 12:41:21 +00:00
parent 465a240e87
commit a2a805b45b
3 changed files with 52 additions and 37 deletions

View file

@ -11,16 +11,21 @@
// Clone and create new data context for this component tree // Clone and create new data context for this component tree
const context = getContext("context") const context = getContext("context")
const component = getContext("component") const component = getContext("component")
const newContext = createContextStore($context) const newContext = createContextStore()
setContext("context", newContext) setContext("context", newContext)
let initiated = false
$: providerKey = key || $component.id $: providerKey = key || $component.id
// Add data context
$: {
newContext.actions.provideData(providerKey, $context, data)
initiated = true
}
// Instance ID is unique to each instance of a provider // Instance ID is unique to each instance of a provider
let instanceId let instanceId
// Add data context
$: data !== undefined && newContext.actions.provideData(providerKey, data)
// Add actions context // Add actions context
$: { $: {
if (instanceId) { if (instanceId) {
@ -51,4 +56,6 @@
}) })
</script> </script>
<slot /> {#if initiated}
<slot />
{/if}

View file

@ -1,22 +1,20 @@
import { writable } from "svelte/store" import { writable } from "svelte/store"
export const createContextStore = existingContext => { export const createContextStore = () => {
const store = writable({ ...existingContext }) const store = writable({})
// Adds a data context layer to the tree // Adds a data context layer to the tree
const provideData = (providerId, data) => { const provideData = (providerId, context, data) => {
if (!providerId) { let newData = { ...context }
return if (providerId && data !== undefined) {
} newData[providerId] = data
store.update(state => {
state[providerId] = data
// Keep track of the closest component ID so we can later hydrate a "data" prop. // Keep track of the closest component ID so we can later hydrate a "data" prop.
// This is only required for legacy bindings that used "data" rather than a // This is only required for legacy bindings that used "data" rather than a
// component ID. // component ID.
state.closestComponentId = providerId newData.closestComponentId = providerId
return state }
}) store.set(newData)
} }
// Adds an action context layer to the tree // Adds an action context layer to the tree
@ -32,7 +30,6 @@ export const createContextStore = existingContext => {
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,
update: store.update,
actions: { provideData, provideAction }, actions: { provideData, provideAction },
} }
} }

View file

@ -1,46 +1,57 @@
<script> <script>
import { onMount, getContext } from "svelte" import { onMount, getContext } from "svelte"
const { API, screenStore, routeStore, Provider, styleable } = getContext(
"sdk"
)
const component = getContext("component")
export let table export let table
const {
API,
screenStore,
routeStore,
Provider,
styleable,
ActionTypes,
} = getContext("sdk")
const component = getContext("component")
let headers = [] let headers = []
let row let row
async function fetchFirstRow() { const fetchFirstRow = async tableId => {
const rows = await API.fetchTableData(table) const rows = await API.fetchTableData(tableId)
return Array.isArray(rows) && rows.length ? rows[0] : { tableId: table } return Array.isArray(rows) && rows.length ? rows[0] : { tableId }
} }
async function fetchData() { const fetchData = async (rowId, tableId) => {
if (!table) { if (!tableId) {
return return
} }
const pathParts = window.location.pathname.split("/") const pathParts = window.location.pathname.split("/")
const routeParamId = $routeStore.routeParams.id
// if srcdoc, then we assume this is the builder preview // if srcdoc, then we assume this is the builder preview
if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && table) { if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && tableId) {
row = await fetchFirstRow() row = await fetchFirstRow(tableId)
} else if (routeParamId) { } else if (rowId) {
row = await API.fetchRow({ tableId: table, rowId: routeParamId }) row = await API.fetchRow({ tableId, rowId })
} else { } else {
throw new Error("Row ID was not supplied to RowDetail") throw new Error("Row ID was not supplied to RowDetail")
} }
} }
onMount(fetchData) $: actions = [
{
type: ActionTypes.RefreshDatasource,
callback: () => fetchData($routeStore.routeParams.id, table),
metadata: { datasource: { type: "table", tableId: table } },
},
]
onMount(() => fetchData($routeStore.routeParams.id, table))
</script> </script>
{#if row} {#if row}
<div use:styleable={$component.styles}> <Provider data={row} {actions}>
<Provider data={row}> <div use:styleable={$component.styles}>
<slot /> <slot />
</Provider> </div>
</div> </Provider>
{/if} {/if}