addWillSetCellCallback
The addWillSetCellCallback method registers a WillSetCellCallback that will be called before any Cell is set in the Store.
addWillSetCellCallback(callback: WillSetCellCallback): Middleware| Type | Description | |
|---|---|---|
callback | WillSetCellCallback | The |
| returns | Middleware | A reference to the |
The callback can transform the Cell value or return undefined to prevent the write. Multiple callbacks can be registered and they are called sequentially, each receiving the (possibly transformed) value from the previous callback.
Examples
This example registers a callback that upper-cases string Cell values in the 'pets' table.
import {createMiddleware, createStore} from 'tinybase';
const store = createStore();
const middleware = createMiddleware(store);
middleware.addWillSetCellCallback((tableId, rowId, cellId, cell) =>
tableId === 'pets' && typeof cell === 'string'
? cell.toUpperCase()
: cell,
);
store.setCell('pets', 'fido', 'species', 'dog');
console.log(store.getCell('pets', 'fido', 'species'));
// -> 'DOG'
middleware.destroy();
This example registers a callback that prevents writes to the 'species' table.
import {createMiddleware, createStore} from 'tinybase';
const store = createStore();
const middleware = createMiddleware(store);
middleware.addWillSetCellCallback((tableId, _rowId, _cellId, cell) =>
tableId === 'species' ? undefined : cell,
);
store.setCell('species', 'dog', 'legs', 4);
console.log(store.getTables());
// -> {}
middleware.destroy();
Since
v8.0.0