@uniono/state
State management implementation
Stores
Mart
Marts allow to store pre-calculated data
// ToDo Example
{
input: '',
tab: 'all',
tasks: [],
editableTask: null,
visibleTasks: useUnion.mart(
({ tab, tasks }) => tasks
.map((task, index) => ({ ...task, index }))
.filter((task) => {
switch (tab) {
case 'all':
return true
case 'active':
return task.done === false
case 'completed':
return task.done === true
default:
throw new Error(`Unsupported tab ${JSON.stringify(tab)}`)
}
}),
({ tab, tasks }) => ({ tab, tasks })
),
...
}
Transaction
Transactions allow you to batch-change the state of arbitrary stores
// ToDo Example
{
...,
addTask: ({ value, mutations }) => {
const { input } = value()
const title = input.trim()
if (title.length === 0) {
return
}
mutations.input.setValue('')
mutations.tasks.unshift({ done: false, title })
},
...
}