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
|
2021-02-05 12:44:33 +01:00
|
|
|
const provideData = (key, data) => {
|
|
|
|
if (!key) {
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 19:51:22 +01:00
|
|
|
store.update(state => {
|
2021-02-05 12:44:33 +01:00
|
|
|
state[key] = 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.
|
|
|
|
state.closestComponentId = key
|
2021-02-01 19:51:22 +01:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an action context layer to the tree
|
2021-02-05 12:44:33 +01:00
|
|
|
const provideAction = (key, actionType, callback) => {
|
|
|
|
if (!key || !actionType) {
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 19:51:22 +01:00
|
|
|
store.update(state => {
|
2021-02-05 12:44:33 +01:00
|
|
|
state[`${key}_${actionType}`] = callback
|
2021-02-01 19:51:22 +01:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
update: store.update,
|
|
|
|
actions: { provideData, provideAction },
|
|
|
|
}
|
|
|
|
}
|