1 | import type { ActionObserver } from './ActionObserver';
|
2 | import type { Actor, IAction, IActorOutput, IActorTest } from './Actor';
|
3 | import type { TestResult } from './TestResult';
|
4 | /**
|
5 | * A publish-subscribe bus for sending actions to actors
|
6 | * to test whether or not they can run an action.
|
7 | *
|
8 | * This bus does not run the action itself,
|
9 | * for that a {@link Mediator} can be used.
|
10 | *
|
11 | * @see Actor
|
12 | * @see Mediator
|
13 | *
|
14 | * @template A The actor type that can subscribe to the sub.
|
15 | * @template I The input type of an actor.
|
16 | * @template T The test type of an actor.
|
17 | * @template O The output type of an actor.
|
18 | * @template TS The test side data type.
|
19 | */
|
20 | export declare class Bus<A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> implements IBusArgs {
|
21 | readonly name: string;
|
22 | protected readonly actors: A[];
|
23 | protected readonly observers: ActionObserver<I, O, TS>[];
|
24 | protected readonly dependencyLinks: Map<A, A[]>;
|
25 | failMessage: string;
|
26 | /**
|
27 | * All enumerable properties from the `args` object are inherited to this bus.
|
28 | *
|
29 | * @param {IBusArgs} args Arguments object
|
30 | * @param {string} args.name The name for the bus
|
31 | * @throws When required arguments are missing.
|
32 | */
|
33 | constructor(args: IBusArgs);
|
34 | /**
|
35 | * Subscribe the given actor to the bus.
|
36 | * After this, the given actor can be unsubscribed from the bus by calling { Bus#unsubscribe}.
|
37 | *
|
38 | * An actor that is subscribed multiple times will exist that amount of times in the bus.
|
39 | *
|
40 | * {A} actor The actor to subscribe.
|
41 | */
|
42 | subscribe(actor: A): void;
|
43 | /**
|
44 | * Subscribe the given observer to the bus.
|
45 | * After this, the given observer can be unsubscribed from the bus by calling {@link Bus#unsubscribeObserver}.
|
46 | *
|
47 | * An observer that is subscribed multiple times will exist that amount of times in the bus.
|
48 | *
|
49 | * @param {ActionObserver<I, O>} observer The observer to subscribe.
|
50 | */
|
51 | subscribeObserver(observer: ActionObserver<I, O, TS>): void;
|
52 | /**
|
53 | * Unsubscribe the given actor from the bus.
|
54 | *
|
55 | * An actor that is subscribed multiple times will be unsubscribed only once.
|
56 | *
|
57 | * @param {A} actor The actor to unsubscribe
|
58 | * @return {boolean} If the given actor was successfully unsubscribed,
|
59 | * otherwise it was not subscribed before.
|
60 | */
|
61 | unsubscribe(actor: A): boolean;
|
62 | /**
|
63 | * Unsubscribe the given observer from the bus.
|
64 | *
|
65 | * An observer that is subscribed multiple times will be unsubscribed only once.
|
66 | *
|
67 | * @param {ActionObserver<I, O>} observer The observer to unsubscribe.
|
68 | * @return {boolean} If the given observer was successfully unsubscribed,
|
69 | * otherwise it was not subscribed before.
|
70 | */
|
71 | unsubscribeObserver(observer: ActionObserver<I, O, TS>): boolean;
|
72 | /**
|
73 | * Publish an action to all actors in the bus to test if they can run the action.
|
74 | *
|
75 | * @param {I} action An action to publish
|
76 | * @return {IActorReply<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest,
|
77 | * O extends IActorOutput>[]}
|
78 | * An array of reply objects. Each object contains a reference to the actor,
|
79 | * and a promise to its {@link Actor#test} result.
|
80 | */
|
81 | publish(action: I): IActorReply<A, I, T, O, TS>[];
|
82 | /**
|
83 | * Invoked when an action was run by an actor.
|
84 | *
|
85 | * @param actor The action on which the {@link Actor#run} method was invoked.
|
86 | * @param {I} action The original action input.
|
87 | * @param {Promise<O>} output A promise resolving to the final action output.
|
88 | */
|
89 | onRun(actor: Actor<I, T, O, TS>, action: I, output: Promise<O>): void;
|
90 | /**
|
91 | * Indicate that the given actor has the given actor dependencies.
|
92 | *
|
93 | * This will ensure that the given actor will be present in the bus *before* the given dependencies.
|
94 | *
|
95 | * @param {A} dependent A dependent actor that will be placed before the given actors.
|
96 | * @param {A[]} dependencies Actor dependencies that will be placed after the given actor.
|
97 | */
|
98 | addDependencies(dependent: A, dependencies: A[]): void;
|
99 | /**
|
100 | * Reorder the bus based on all present dependencies.
|
101 | */
|
102 | reorderForDependencies(): void;
|
103 | }
|
104 | export interface IBusArgs {
|
105 | /**
|
106 | * The name for this bus.
|
107 | * @default {<rdf:subject>}
|
108 | */
|
109 | name: string;
|
110 | }
|
111 | /**
|
112 | * Data interface for holding an actor and a promise to a reply from that actor.
|
113 | */
|
114 | export interface IActorReply<A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> {
|
115 | actor: A;
|
116 | reply: Promise<TestResult<T, TS>>;
|
117 | }
|
118 | export type IReply<I extends IAction = IAction, O extends IActorOutput = IActorOutput, T extends IActorTest = IActorTest, TS = undefined> = IActorReply<Actor<I, T, O, TS>, I, T, O, TS>;
|
119 | export type IBus<I extends IAction = IAction, O extends IActorOutput = IActorOutput, T extends IActorTest = IActorTest, TS = undefined> = Bus<Actor<I, T, O, TS>, I, T, O, TS>;
|