@uniono/state

0.1.2 • Public • Published

@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 })
	),
	...
}

More

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 })
	},
	...
}

More

Dependencies (0)

    Dev Dependencies (15)

    Package Sidebar

    Install

    npm i @uniono/state

    Weekly Downloads

    0

    Version

    0.1.2

    License

    ISC

    Unpacked Size

    44.5 kB

    Total Files

    29

    Last publish

    Collaborators

    • db-public