TinyBase logoTinyBase

addWillApplyChangesCallback

The addWillApplyChangesCallback method registers a WillApplyChangesCallback that will be called before Changes are applied to the Store via the applyChanges method.

addWillApplyChangesCallback(callback: WillApplyChangesCallback): Middleware
TypeDescription
callbackWillApplyChangesCallback

The WillApplyChangesCallback to register.

returnsMiddleware

A reference to the Middleware object, for chaining.

This callback receives the Changes object and can return it (to allow), return a modified Changes object (to transform), or return undefined (to reject). Multiple callbacks can be registered and they are called sequentially, each receiving the output of the previous. If any callback returns undefined, all remaining callbacks are skipped and the changes are rejected.

This fires when applyChanges is called directly, or indirectly via applyMergeableChanges, setMergeableContent, or merge on a MergeableStore.

Example

This example registers a callback that rejects changes containing a 'secret' table.

import {createMiddleware, createStore} from 'tinybase';

const store = createStore();
const middleware = createMiddleware(store);

middleware.addWillApplyChangesCallback(([changedTables, changedValues]) =>
  changedTables['secret'] != null
    ? undefined
    : [changedTables, changedValues, 1],
);

store.applyChanges([{pets: {fido: {species: 'dog'}}}, {}, 1]);
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog'}

store.applyChanges([{secret: {r1: {c1: 'v1'}}}, {}, 1]);
console.log(store.getTable('secret'));
// -> {}

middleware.destroy();

Since

v8.0.0