budibase/packages/client/src/store/context.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

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