TinyBase logoTinyBase β

onCheckpoint

The onCheckpoint function registers a listener that is called whenever the label of a specified Checkpoint changes.

onCheckpoint(
  checkpointId: MaybeGetter<IdOrNull>,
  listener: CheckpointListener,
  checkpointsOrCheckpointsId?: MaybeGetter<undefined | CheckpointsOrCheckpointsId>,
): void
TypeDescription
checkpointIdMaybeGetter<IdOrNull>

The Id of the Checkpoint to listen to, or null to listen to any Checkpoint.

listenerCheckpointListener

The function to call when the Checkpoint label changes.

checkpointsOrCheckpointsId?MaybeGetter<undefined | CheckpointsOrCheckpointsId>

The Checkpoints object to use, or its Id.

returnsvoid

This has no return value.

Example

This example registers a Svelte listener and responds to a TinyBase change.

App.svelte
<svelte:options runes={true} />

<script>
  import {onCheckpoint} from 'tinybase/ui-svelte';

  let {checkpoints} = $props();
  let seen = $state('');

  onCheckpoint(null, () => (seen = 'changed'), checkpoints);
</script>

{seen}
import {flushSync, mount} from 'svelte';
import {createCheckpoints, createStore} from 'tinybase';
import App from './App.svelte';

const store = createStore()
  .setTables({
    pets: {
      fido: {species: 'dog', color: 'brown', sold: false, next: 'felix'},
      felix: {species: 'cat', color: 'black', sold: true},
    },
    species: {dog: {price: 5}, cat: {price: 4}},
  })
  .setValues({open: true, employees: 3});
const checkpoints = createCheckpoints(store);
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {checkpoints}}));
flushSync(() => {
  store.setCell('pets', 'fido', 'species', 'guide dog');
  checkpoints.addCheckpoint('saved');
});
flushSync();
console.log(app.textContent);
// -> 'changed'

Since

v8.1.0