transaction
Signature
transaction(store?: Store): (callback: TransactionFn, name?: string) => voidvaldres-svelte Batch multiple writes into a single commit
Returns a transaction runner bound to the current context store, captured at component initialization. That capture is what makes it safe to call from an event handler — Svelte throws lifecycle_outside_component if you call getContext inside a handler. Writes inside the callback commit together, so subscribers and selectors observe one atomic update. Forwards core's optional devtools name.
Usage
<script lang="ts">
import { transaction } from "valdres-svelte"
import { countAtom } from "$lib/state"
const txn = transaction()
const buyTwo = () =>
txn(({ get, set }) => set(countAtom, get(countAtom) + 2))
</script>
<button onclick={buyTwo}>Buy two</button>
Pass an explicit store to bind the runner to a specific store instead of the context store.
See also
- setValdresContext — provide the store the runner binds to
- scope — scoped child store