Documentation
    Preparing search index...

    Documentation

    FutureMachine Logo

    FutureMachine

    A TypeScript library for Futures, persistent Promises that survive process restarts and can be resolved by ID.


    Package Version Description
    @futuremachine/core npm version The core FutureMachine engine.
    @futuremachine/db-sqlite-node npm version A node:sqlite implementation of the FutureDatabase.
    @futuremachine/db-conformance-tests npm version Utilities for testing FutureDatabase implementations.
    @futuremachine/typedoc-plugin TypeDoc plugin to support FutureMachine interfaces.

    To see FutureMachine in action, try this two-part "Hello World." On the first run, we define a logger Method and attach it to a Future. Even after the process exits, the database remembers this 'reaction.' On the second run, we resolve the Future with "world", triggering the original logic to print "Hello world!"

    npm i @futuremachine/core @futuremachine/db-sqlite-node
    
    // hello_world.ts
    import fs from 'node:fs';

    import { createMethodMachine, type FutureId } from '@futuremachine/core';
    import { SQLFutureDatabase } from '@futuremachine/db-sqlite-node';

    const idFile = './savedFutureId';
    const db = new SQLFutureDatabase('test.db');

    // Build your FutureMachine.
    const { methods } = createFutureMachine(db);
    const logger = methods.create('logger', (str: string) => {
    console.log(`Hello ${str}!`);
    });
    const futures = methods.build();

    if (!fs.existsSync(idFile)) {
    // First run.
    console.log(
    'Creating a Future, adding a reaction, and saving its id to disk.'
    );

    const { future, id } = futures.withResolvers<string>();
    future.next(logger);

    fs.writeFileSync(idFile, id);
    } else {
    // Second run.
    console.log('Reading the FutureId from disk and resolving its Future.');

    const id = fs.readFileSync(idFile, 'utf8') as FutureId<string>;
    fs.unlinkSync(idFile);

    futures.resolveFutureById(id, 'world');
    }

    await db.close();
    > npx tsx hello_world.ts
    Creating a Future, adding a reaction, and saving its id to disk.

    > npx tsx hello_world.ts
    Reading the FutureId from disk and resolving its Future.
    Hello world!