Function dataBag

  • Requests the data bag, in an input adapter, to read what it holds while the step is built, or in the ID generator, to read the instance's initial data while the instance is created.

    import assert from "node:assert/strict";
    import { LocalExecutor } from "itinera/executor";
    import {
    type Contributor,
    contributor,
    type DataBag,
    dataBag,
    type JourneyId,
    journeyId,
    } from "itinera/journey";
    import {
    input,
    type Outcome,
    type StepName,
    stepName,
    Step,
    step,
    } from "itinera/step";
    import {
    type FromDataBag,
    fromWorkflow,
    inputAdapter,
    type InputAdapterOptions,
    key,
    type Supplied,
    type SuppliedValue,
    withStep,
    Workflow,
    workflow,
    } from "itinera/workflow";
    import { integer, string } from "itinera/value";

    @step()
    class Charge extends Step {
    constructor(@input("amount", integer) readonly amount: number) {
    super();
    }

    override run(@contributor() contributor: Contributor): Outcome {
    contributor.contribute("charged", this.amount);
    return this.succeed();
    }
    }

    const adapting: InputAdapterOptions = { steps: ["charge"], name: "prices" };

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge" })
    class Orders extends Workflow {
    readonly asked: string[] = [];

    constructor(readonly prices: ReadonlyMap<string, number>) {
    super();
    }

    @inputAdapter(adapting)
    price(
    @stepName() step: StepName,
    @key() key: string,
    @fromWorkflow("order", string) order: string,
    @journeyId() id: JourneyId,
    @dataBag() bag: DataBag,
    ): Supplied {
    this.asked.push(`${id}: ${step} ${key}`);
    const price = this.prices.get(order);
    return price === undefined || bag.read(key, integer).kind === "present"
    ? this.fromDataBag()
    : this.supply(price);
    }
    }

    export default (async () => {
    const orders = Orders.build(new Map([["A-1", 40]]));
    const supplying = orders.instance(new Map([["order", "A-1"]]));
    const reading = orders.instance(
    new Map<string, string | number>([
    ["order", "A-1"],
    ["amount", 25],
    ]),
    );

    const supplied = await new LocalExecutor().run(supplying);
    const read = await new LocalExecutor().run(reading);

    assert.ok(supplied.isSucceeded() && read.isSucceeded());
    assert.equal(charged(supplied.status.dataBag), 40);
    assert.equal(charged(read.status.dataBag), 25);
    assert.deepEqual(orders.asked, [
    `${supplying.journeyId}: charge amount`,
    `${reading.journeyId}: charge amount`,
    ]);
    const charge = "charge" as StepName;
    assert.deepEqual(
    orders.price(
    charge,
    "amount",
    "A-1",
    supplying.journeyId,
    supplied.status.dataBag,
    ),
    { kind: "supplied", value: 40 } satisfies SuppliedValue,
    );
    assert.deepEqual(
    orders.price(
    charge,
    "amount",
    "A-1",
    reading.journeyId,
    read.status.dataBag,
    ),
    { kind: "fromDataBag" } satisfies FromDataBag,
    );
    })();

    function charged(bag: DataBag): number {
    return bag.read("charged", integer).option().unwrap();
    }

    Returns RequestDecorator<DataBag, Place<Workflow, string | symbol>>