TinyBase logoTinyBase

schedule

The schedule method allows you to queue up a series of asynchronous actions that must run in sequence during persistence.

schedule(actions: (() => Promise<any>)[]): Promise<DurableObjectStoragePersister>
TypeDescription
actions(() => Promise<any>)[]

One or many functions which will be scheduled, and which can be asynchronous.

returnsPromise<DurableObjectStoragePersister>

A reference to the Persister object.

For example, a database Persister may need to ensure that multiple asynchronous tasks to check and update the database schema are completed before data is written to it. Therefore it's most likely you will be using this method inside your setPersisted implementation.

Call this method to add a single asynchronous action, or a sequence of them in one call. This will also start to run the first task in the queue (which once complete will then run the next, and so on), and so this method itself is also asynchronous and returns a promise of the Persister.

Example

This example creates a custom Persister object against a newly-created Store and then sequences two tasks in order to update its data on a hypothetical remote system.

import {
  checkRemoteSystemIsReady,
  getDataFromRemoteSystem,
  sendDataToRemoteSystem,
} from 'custom-remote-handlers';
import {createCustomPersister} from 'tinybase/persisters';
import {createStore} from 'tinybase';

const store = createStore();
const persister = createCustomPersister(
  store,
  async () => {
    // getPersisted
    return await getDataFromRemoteSystem();
  },
  async (getContent) => {
    // setPersisted
    await persister.schedule(
      async () => await checkRemoteSystemIsReady(),
      async () => await sendDataToRemoteSystem(getContent()),
    );
  },
  (listener) => setInterval(listener, 1000),
  (interval) => clearInterval(interval),
);

Since

v4.0.0