UNPKG

1.77 kBTypeScriptView Raw
1import type { Actor, IAction, IActorOutput, IActorTest } from './Actor';
2import type { Bus } from './Bus';
3/**
4 * An ActionObserver can passively listen to {@link Actor#run} inputs and outputs for all actors on a certain bus.
5 *
6 * ActionObserver should not edit inputs and outputs,
7 * they should be considered immutable.
8 *
9 * @see Actor
10 * @see Bus
11 *
12 * @template I The input type of an actor.
13 * @template O The output type of an actor.
14 */
15export declare abstract class ActionObserver<I extends IAction, O extends IActorOutput> {
16 readonly name: string;
17 readonly bus: Bus<Actor<I, IActorTest, O>, I, IActorTest, O>;
18 /**
19 * All enumerable properties from the `args` object are inherited to this observer.
20 *
21 * The observer will NOT automatically subscribe to the given bus when this constructor is called.
22 *
23 * @param {IActionObserverArgs<I extends IAction, O extends IActorOutput>} args Arguments object
24 * @throws When required arguments are missing.
25 */
26 protected constructor(args: IActionObserverArgs<I, O>);
27 /**
28 * Invoked when an action was run by an actor.
29 *
30 * @param actor The action on which the {@link Actor#run} method was invoked.
31 * @param {I} action The original action input.
32 * @param {Promise<O>} output A promise resolving to the final action output.
33 */
34 abstract onRun(actor: Actor<I, IActorTest, O>, action: I, output: Promise<O>): void;
35}
36export interface IActionObserverArgs<I extends IAction, O extends IActorOutput> {
37 /**
38 * The name for this observer.
39 * @default {<rdf:subject>}
40 */
41 name: string;
42 /**
43 * The bus this observer can subscribe to.
44 */
45 bus: Bus<Actor<I, IActorTest, O>, I, IActorTest, O>;
46}