2021-02-01 19:51:22 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
|
2021-02-09 13:41:21 +01:00
|
|
|
export const createContextStore = () => {
|
|
|
|
const store = writable({})
|
2021-02-01 19:51:22 +01:00
|
|
|
|
|
|
|
// Adds a data context layer to the tree
|
2021-02-09 13:41:21 +01:00
|
|
|
const provideData = (providerId, context, data) => {
|
|
|
|
let newData = { ...context }
|
|
|
|
if (providerId && data !== undefined) {
|
|
|
|
newData[providerId] = data
|
2021-02-02 15:32:58 +01:00
|
|
|
|
2021-02-05 12:44:33 +01:00
|
|
|
// 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
|
|
|
|
// component ID.
|
2021-02-09 13:41:21 +01:00
|
|
|
newData.closestComponentId = providerId
|
|
|
|
}
|
|
|
|
store.set(newData)
|
2021-02-01 19:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an action context layer to the tree
|
2021-02-05 17:16:41 +01:00
|
|
|
const provideAction = (providerId, actionType, callback) => {
|
|
|
|
if (!providerId || !actionType) {
|
2021-02-05 12:44:33 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-01 19:51:22 +01:00
|
|
|
store.update(state => {
|
2021-02-05 17:16:41 +01:00
|
|
|
state[`${providerId}_${actionType}`] = callback
|
2021-02-01 19:51:22 +01:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { provideData, provideAction },
|
|
|
|
}
|
|
|
|
}
|