TinyBase logoTinyBase

addCheckpoint

The addCheckpoint method records a checkpoint of the Store into the Checkpoints object that can be reverted to in the future.

addCheckpoint(label?: string): Id
TypeDescription
label?string

An optional label to describe the actions leading up to this checkpoint.

returnsId

The Id of the newly-created checkpoint.

If no changes have been made to the Store since the last time a checkpoint was made, this method will have no effect.

The optional label parameter can be used to describe the actions that changed the Store before this checkpoint. This can be useful for interfaces that let users 'Undo [last action]'.

Example

This example creates a Store, adds a Checkpoints object, and adds two checkpoints, one with a label.

const store = createStore().setTables({pets: {fido: {sold: false}}});

const checkpoints = createCheckpoints(store);
console.log(checkpoints.getCheckpointIds());
// -> [[], '0', []]

store.setCell('pets', 'fido', 'species', 'dog');
const checkpointId1 = checkpoints.addCheckpoint();
console.log(checkpointId1);
// -> '1'

console.log(checkpoints.getCheckpointIds());
// -> [['0'], '1', []]

store.setCell('pets', 'fido', 'sold', true);
checkpoints.addCheckpoint('sale');
console.log(checkpoints.getCheckpointIds());
// -> [['0', '1'], '2', []]

console.log(checkpoints.getCheckpoint('2'));
// -> 'sale'