persistent-computations

1.0.2 • Public • Published

Persistent computations

A library for building chains of computations where each step can be persisted and recovered on the next run.

The data is serialized and recovered using serialize and deserialize functions from the v8 module. Read more about serialization in V8.

Basic usage

import { PersistentComputation, PersistentComputationContext } from 'persistent-computations';
// or you can use shorter exports
// import { PC, PCContext } from 'persistent-computations';

class OneStepOperation extends PersistentComputation {
  async run() {
    const data = await this.step(() => 'foo');

    // Any computation you don't want persisted - do it outside of `this.step` call
    return data.repeat(3);
  }
}

class TwoStepOperation extends PersistentComputation {
  async run() {
    // The value returned in the step's callback should be V8-serializable
    const firstStep = await this.step(async () => ({ foo: 'foo' }));
    const secondStep = await this.step(async () => ({ bar: 'bar' }));

    return {
      firstStep,
      secondStep,
    };
  }
}

const ctx = new PersistentComputationContext();
await ctx.run([TwoStepOperation]);

/persistent-computations/

    Package Sidebar

    Install

    npm i persistent-computations

    Weekly Downloads

    0

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    28.7 kB

    Total Files

    17

    Last publish

    Collaborators

    • ksenkso