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