clear
The clear method resets this Checkpoints object to its initial state, removing all the checkpoints it has been managing.
clear(): Checkpoints| returns | Checkpoints | A reference to the |
|---|
Obviously this method should be used with caution as it destroys the ability to undo or redo recent changes to the Store (though of course the Store itself is not reset by this method).
This method can be useful when a Store is being loaded via a Persister asynchronously after the Checkpoints object has been attached, and you don't want users to be able to undo the initial load of the data. In this case you could call the clear method immediately after the initial load so that that is the baseline from which all subsequent changes are tracked.
Example
This example creates a Store, a Checkpoints object, adds a listener, makes a change and then clears the checkpoints.
import {createCheckpoints, createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {sold: false}}});
const checkpoints = createCheckpoints(store);
console.log(checkpoints.getCheckpointIds());
// -> [[], '0', []]
const listenerId = checkpoints.addCheckpointIdsListener(() => {
console.log('checkpoints changed');
});
store.setCell('pets', 'fido', 'color', 'brown');
// -> 'checkpoints changed'
checkpoints.addCheckpoint();
// -> 'checkpoints changed'
store.setCell('pets', 'fido', 'sold', true);
// -> 'checkpoints changed'
checkpoints.addCheckpoint();
// -> 'checkpoints changed'
console.log(store.getTables());
// -> {pets: {fido: {sold: true, color: 'brown'}}}
console.log(checkpoints.getCheckpointIds());
// -> [['0', '1'], '2', []]
checkpoints.clear();
// -> 'checkpoints changed'
console.log(store.getTables());
// -> {pets: {fido: {sold: true, color: 'brown'}}}
console.log(checkpoints.getCheckpointIds());
// -> [[], '0', []]
checkpoints.delListener(listenerId);
Since
v1.0.0