TinyBase logoTinyBase

addCheckpointIdsListener

The addCheckpointIdsListener method registers a listener function with the Checkpoints object that will be called whenever its set of checkpoints changes.

addCheckpointIdsListener(listener: CheckpointIdsListener): Id
TypeDescription
listenerCheckpointIdsListener

The function that will be called whenever the checkpoints change.

returnsId

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

The provided listener is a CheckpointIdsListener function, and will be called with a reference to the Checkpoints object.

Example

This example creates a Store, a Checkpoints object, and then registers a listener that responds to any changes to the checkpoints.

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

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

const listenerId = checkpoints.addCheckpointIdsListener(() => {
  console.log('Checkpoint Ids changed');
  console.log(checkpoints.getCheckpointIds());
});

store.setCell('pets', 'fido', 'species', 'dog');
// -> 'Checkpoint Ids changed'
// -> [['0'], undefined, []]

checkpoints.addCheckpoint();
// -> 'Checkpoint Ids changed'
// -> [['0'], '1', []]

checkpoints.goBackward();
// -> 'Checkpoint Ids changed'
// -> [[], '0', ['1']]

checkpoints.goForward();
// -> 'Checkpoint Ids changed'
// -> [['0'], '1', []]

checkpoints.delListener(listenerId);
checkpoints.destroy();