injectTransaction

Signature
injectTransaction(store?: Store): (callback: (txn: Transaction) => any) => any

valdres-angular Batch updates — subscribers fire once

Returns a function that runs a callback as a transaction on the current store: all updates apply atomically and subscribers fire once at the end. The callback receives { set, get, reset, del, unset }. Call it in an injection context (field initializer or constructor).

Usage

import { Component } from "@angular/core"
import { atom } from "valdres"
import { injectTransaction } from "valdres-angular"

const firstNameAtom = atom("")
const lastNameAtom = atom("")

@Component({
    selector: "reset-form",
    template: `<button (click)="clear()">Clear</button>`,
})
export class ResetForm {
    txn = injectTransaction()
    clear() {
        this.txn(({ reset }) => {
            reset(firstNameAtom)
            reset(lastNameAtom)
        })
    }
}

Pass a store to target one explicitly: injectTransaction(myStore).

See also