transaction
The transaction
method takes a function that makes multiple mutations to the store, buffering all calls to the relevant listeners until it completes.
transaction<Return>(actions: () => Return): Return
Type | Description | |
---|---|---|
actions | () => Return | The function to be executed as a transaction. |
returns | Return | Whatever value the provided transaction function returns. |
This method is useful for making bulk changes to the data in a Store
, and when you don't want listeners to be called as you make each change. Changes are made silently during the transaction, and listeners relevant to the changes you have made will instead only be called when the whole transaction is complete.
If multiple changes are made to a piece of Store
data throughout the transaction, a relevant listener will only be called with the final value (assuming it is different to the value at the start of the transaction), regardless of the changes that happened in between. For example, if a Cell
had a value 'b'
and then, within a transaction, it was changed to 'b'
and then 'c'
, any CellListener
registered for that cell would be called once as if there had been a single change from 'a'
to 'c'
.
Transactions can be nested. Relevant listeners will be called only when the outermost one completes.
Examples
This example makes changes to two Cells, first outside, and secondly within, a transaction. In the second case, the Row
listener is only called once.
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
store.addRowListener('pets', 'fido', () => console.log('Fido changed'));
store.setCell('pets', 'fido', 'color', 'brown');
store.setCell('pets', 'fido', 'sold', false);
// -> 'Fido changed'
// -> 'Fido changed'
store.transaction(() => {
store.setCell('pets', 'fido', 'color', 'walnut');
store.setCell('pets', 'fido', 'sold', true);
});
// -> 'Fido changed'
This example makes multiple changes to one Cell
. The Cell
listener is called once - and with the final value - only if there is a net overall change.
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
store.addCellListener(
'pets',
'fido',
'color',
(store, tableId, rowId, cellId, newCell) => console.log(newCell),
);
store.transaction(() => {
store.setCell('pets', 'fido', 'color', 'black');
store.setCell('pets', 'fido', 'color', 'brown');
store.setCell('pets', 'fido', 'color', 'walnut');
});
// -> 'walnut'
store.transaction(() => {
store.setCell('pets', 'fido', 'color', 'black');
store.setCell('pets', 'fido', 'color', 'walnut');
});
// -> undefined
// No net change during the transaction, so the listener is not called.