2021-02-01 19:51:22 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
|
|
|
|
export const createContextStore = existingContext => {
|
|
|
|
const store = writable({ ...existingContext })
|
|
|
|
|
|
|
|
// Adds a data context layer to the tree
|
|
|
|
const provideData = (componentId, data) => {
|
|
|
|
store.update(state => {
|
|
|
|
if (componentId) {
|
|
|
|
state[componentId] = data
|
2021-02-02 15:32:58 +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-01 19:51:22 +01:00
|
|
|
state.closestComponentId = componentId
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an action context layer to the tree
|
|
|
|
const provideAction = (componentId, actionType, callback) => {
|
|
|
|
store.update(state => {
|
|
|
|
if (actionType && componentId) {
|
|
|
|
state[`${componentId}_${actionType}`] = callback
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
update: store.update,
|
|
|
|
actions: { provideData, provideAction },
|
|
|
|
}
|
|
|
|
}
|