2021-02-10 20:42:56 +01:00
|
|
|
import { writable, derived } from "svelte/store"
|
2021-02-01 19:51:22 +01:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
export const createContextStore = (oldContext) => {
|
2021-02-10 20:42:56 +01:00
|
|
|
const newContext = writable({})
|
|
|
|
const contexts = oldContext ? [oldContext, newContext] : [newContext]
|
2021-05-03 09:31:09 +02:00
|
|
|
const totalContext = derived(contexts, ($contexts) => {
|
2021-02-10 20:42:56 +01:00
|
|
|
return $contexts.reduce((total, context) => ({ ...total, ...context }), {})
|
|
|
|
})
|
2021-02-01 19:51:22 +01:00
|
|
|
|
|
|
|
// Adds a data context layer to the tree
|
2021-02-10 20:42:56 +01:00
|
|
|
const provideData = (providerId, data) => {
|
|
|
|
if (!providerId || data === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-03 09:31:09 +02:00
|
|
|
newContext.update((state) => {
|
2021-02-10 20:42:56 +01:00
|
|
|
state[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-10 20:42:56 +01:00
|
|
|
state.closestComponentId = providerId
|
|
|
|
|
|
|
|
return state
|
|
|
|
})
|
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-05-03 09:31:09 +02:00
|
|
|
newContext.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 {
|
2021-02-10 20:42:56 +01:00
|
|
|
subscribe: totalContext.subscribe,
|
2021-02-01 19:51:22 +01:00
|
|
|
actions: { provideData, provideAction },
|
|
|
|
}
|
|
|
|
}
|