samihult / xjog

Persistent XState charts
https://www.xjog.io
MIT License
5 stars 2 forks source link
xstate

ALPHA NOTICE

This library is in the alpha phase. The programming interface and the feature set are still changing! The database structure will evolve and future versions may not be able to migrate data from alpha versions. The documentation and test coverage are being improved.


XJog

XJog is a specialized XState statechart runner.

It is made specifically for running long-living, persisted charts. This makes it suitable for driving business processes that can take considerable time to execute. A real-life example is a customer process starting from the cart, going through checkout and managing the delivery of the goods.

The main features:

Modules:

On roadmap:

Database adapters

Usage

yarn add xstate xjog
import { createMachine } from 'xstate';
import { SQLitePersistenceAdapter, XJog } from 'xjog';

// Configure a regular xState chart
export const doorMachine = createMachine({
  id: 'door',
  initial: 'closed',
  states: {
    closed: {
      on: {
        open: 'open',
      },
    },
    open: {
      on: {
        close: 'closed',
      },
    },
  },
});

// Use default in-memory database
const persistence = await SQLitePersistenceAdapter.connect();

const xJog = new XJog(persistence);
const door = await xJog.registerMachine(doorMachine);

await xJog.start();

// Create a door
const frontDoor = await door.createChart();
frontDoor.subscribe((stateUpdate) => {
  console.log('Door is', stateUpdate.state.value);
});

await frontDoor.send('open');
await frontDoor.send('close');

// Stop everything
await xJog.kill();