Interface Dispatcher

Delivers the events and the global metadata of one journey to the reporters added to it.

An executor gets a new dispatcher for every journey from its DispatcherFactory, adds the journey's reporters to it, sets every entry of the journey's global metadata and dispatches every event of the journey through it, and drops it when the journey ends. What adding a reporter means, and how events and global metadata are delivered, is the dispatcher's own decision; DefaultDispatcher is the usual one. Whatever the dispatcher does, the executor keeps a reporter that failed from receiving anything more, and every other reporter from receiving anything but journey_aborted.

Each operation may be synchronous or asynchronous. Failing in add, or in setGlobalMetadata before the journey starts, refuses the journey; failing in dispatch, or in setGlobalMetadata while the journey runs, aborts it with reporter failed, except while journey_aborted is dispatched, when the failure is ignored.

import assert from "node:assert/strict";
import type { Event } from "itinera/event";
import type { Dispatcher, Reporter } from "itinera/report";
import type { PlainValue } from "itinera/value";

class OnlyMine implements Dispatcher {
constructor(private readonly mine: Reporter) {}

add(): void {
// The workflow's reporters are left out: only this dispatcher's own hears the journey.
}

async dispatch(event: Event): Promise<void> {
await this.mine.report(event);
}

async setGlobalMetadata(key: string, value: PlainValue): Promise<void> {
await this.mine.receiveGlobalMetadata?.(key, value);
}
}

class Tenant implements Reporter {
tenant: PlainValue = null;

receiveGlobalMetadata(key: string, value: PlainValue): void {
if (key === "tenant") this.tenant = value;
}

report(): void {
// Events are not this reporter's concern.
}
}

export default (async () => {
const mine = new Tenant();
const dispatcher = new OnlyMine(mine);
await dispatcher.setGlobalMetadata("tenant", "acme");
assert.equal(mine.tenant, "acme");
})();
interface Dispatcher {
    add(reporter: Reporter): void | Promise<void>;
    dispatch(event: Event): void | Promise<void>;
    setGlobalMetadata(key: string, value: PlainValue): void | Promise<void>;
}

Implemented by

Index

  • Adds a reporter for the journey.

    Parameters

    Returns void | Promise<void>

  • Delivers one event.

    Parameters

    Returns void | Promise<void>

  • Passes one entry of the journey's global metadata on.

    Parameters

    Returns void | Promise<void>