2021-02-01 19:51:22 +01:00
|
|
|
<script>
|
2021-10-27 15:24:36 +02:00
|
|
|
import { getContext, setContext, onDestroy } from "svelte"
|
2021-09-01 12:41:48 +02:00
|
|
|
import { dataSourceStore, createContextStore } from "stores"
|
|
|
|
import { ActionTypes } from "constants"
|
2021-02-05 17:16:41 +01:00
|
|
|
import { generate } from "shortid"
|
2021-02-01 19:51:22 +01:00
|
|
|
|
|
|
|
export let data
|
|
|
|
export let actions
|
2021-02-05 12:44:33 +01:00
|
|
|
export let key
|
2021-02-01 19:51:22 +01:00
|
|
|
|
|
|
|
// Clone and create new data context for this component tree
|
|
|
|
const context = getContext("context")
|
|
|
|
const component = getContext("component")
|
2021-02-10 20:42:56 +01:00
|
|
|
const newContext = createContextStore(context)
|
2021-02-01 19:51:22 +01:00
|
|
|
setContext("context", newContext)
|
2021-02-09 13:41:21 +01:00
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
const providerKey = key || $component.id
|
2021-02-01 19:51:22 +01:00
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
// Generate a permanent unique ID for this component and use it to register
|
|
|
|
// any datasource actions
|
|
|
|
const instanceId = generate()
|
2021-02-09 13:41:21 +01:00
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
// Keep previous state around so we can avoid updating unless necessary
|
|
|
|
let lastDataKey
|
|
|
|
let lastActionsKey
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-06-25 16:04:27 +02:00
|
|
|
$: provideData(data)
|
|
|
|
$: provideActions(actions, instanceId)
|
|
|
|
|
|
|
|
const provideData = newData => {
|
|
|
|
const dataKey = JSON.stringify(newData)
|
|
|
|
if (dataKey !== lastDataKey) {
|
|
|
|
newContext.actions.provideData(providerKey, newData)
|
|
|
|
lastDataKey = dataKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const provideActions = newActions => {
|
|
|
|
const actionsKey = JSON.stringify(newActions)
|
|
|
|
if (actionsKey !== lastActionsKey) {
|
|
|
|
lastActionsKey = actionsKey
|
|
|
|
newActions?.forEach(({ type, callback, metadata }) => {
|
2021-02-05 17:16:41 +01:00
|
|
|
newContext.actions.provideAction(providerKey, type, callback)
|
|
|
|
|
|
|
|
// Register any "refresh datasource" actions with a singleton store
|
|
|
|
// so we can easily refresh data at all levels for any datasource
|
|
|
|
if (type === ActionTypes.RefreshDatasource) {
|
2021-03-16 14:54:34 +01:00
|
|
|
const { dataSource } = metadata || {}
|
|
|
|
dataSourceStore.actions.registerDataSource(
|
|
|
|
dataSource,
|
2021-02-05 17:16:41 +01:00
|
|
|
instanceId,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-01 19:51:22 +01:00
|
|
|
}
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-10-27 15:24:36 +02:00
|
|
|
onDestroy(() => {
|
2021-02-05 17:16:41 +01:00
|
|
|
// Unregister all datasource instances when unmounting this provider
|
2021-10-27 15:24:36 +02:00
|
|
|
dataSourceStore.actions.unregisterInstance(instanceId)
|
2021-02-05 17:16:41 +01:00
|
|
|
})
|
2021-02-01 19:51:22 +01:00
|
|
|
</script>
|
|
|
|
|
2021-02-10 20:42:56 +01:00
|
|
|
<slot />
|