TinyBase logoTinyBase

addStartTransactionListener

The addStartTransactionListener method registers a listener function with the Store that will be called at the start of a transaction.

addStartTransactionListener(listener: TransactionListener<Store>): string
TypeDescription
listenerTransactionListener<Store>

The function that will be called at the start of a transaction.

returnsstring

A unique Id for the listener that can later be used to remove it.

The provided TransactionListener will receive a reference to the Store and two booleans to indicate whether Cell or Value data has been touched during the transaction. Since this is called at the start, they will both be false!

Note that a TransactionListener added to the Store with this method can mutate the Store, and its changes will be treated as part of the transaction that is starting.

Example

This example registers a listener that is called at start end of the transaction, just before its listeners will be called.

import {createStore} from 'tinybase';

const store = createStore()
  .setTables({
    pets: {fido: {species: 'dog', color: 'brown'}},
  })
  .setValues({open: true, employees: 3});
const listenerId = store.addStartTransactionListener(() => {
  console.log('Transaction started');
});

store.transaction(() =>
  store.setCell('pets', 'fido', 'color', 'brown').setValue('employees', 3),
);
// -> 'Transaction started'

store.callListener(listenerId);
// -> 'Transaction started'

store.delListener(listenerId);

Since

v3.2.0