UNPKG

4.03 kBTypeScriptView Raw
1import type { Actor, IAction, IActorOutput, IActorTest } from './Actor';
2import type { Bus, IActorReply } from './Bus';
3/**
4 * A mediator can mediate an action over a bus of actors.
5 *
6 * It does the following:
7 * 1. Accepts an action in {@link Mediator#mediate}.
8 * 2. Sends the action to the bus to test its applicability on all actors.
9 * 3. It _mediates_ over these test results.
10 * 4. It selects the _best_ actor.
11 * 5. The action is run by the _best_ actor, and the result if returned.
12 *
13 * The _mediates_ and _best_ parts are filled in by subclasses of this abstract Mediator class.
14 *
15 * @template A The type of actor to mediator over.
16 * @template I The input type of an actor.
17 * @template T The test type of an actor.
18 * @template O The output type of an actor.
19 */
20export declare abstract class Mediator<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput> implements IMediatorArgs<A, I, T, O> {
21 readonly name: string;
22 readonly bus: Bus<A, I, T, O>;
23 /**
24 * All enumerable properties from the `args` object are inherited to this mediator.
25 *
26 * @param {IMediatorArgs<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest,
27 * O extends IActorOutput>} args Arguments object
28 * @param {string} args.name The name for this mediator.
29 * @param {Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus
30 * The bus this mediator will mediate over.
31 * @throws When required arguments are missing.
32 */
33 protected constructor(args: IMediatorArgs<A, I, T, O>);
34 /**
35 * Publish the given action in the bus.
36 *
37 * This will send the test action on all actors in the bus.
38 * All actor replies will be returned.
39 *
40 * @param {I} action The action to mediate for.
41 * @return {IActorReply<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>[]}
42 * The list of actor replies.
43 */
44 publish(action: I): IActorReply<A, I, T, O>[];
45 /**
46 * Mediate for the given action to get an actor.
47 *
48 * This will send the test action on all actors in the bus.
49 * The actor that tests _best_ will be returned.
50 *
51 * @param {I} action The action to mediate for.
52 * @return {Promise<O extends IActorOutput>} A promise that resolves to the _best_ actor.
53 */
54 mediateActor(action: I): Promise<A>;
55 /**
56 * Mediate for the given action.
57 *
58 * This will send the test action on all actors in the bus.
59 * The action will be run on the actor that tests _best_,
60 * of which the result will be returned.
61 *
62 * @param {I} action The action to mediate for.
63 * @return {Promise<O extends IActorOutput>} A promise that resolves to the mediation result.
64 */
65 mediate(action: I): Promise<O>;
66 /**
67 * Mediate for the given action with the given actor test results for the action.
68 *
69 * One actor must be returned that provided the _best_ test result.
70 * How '_best_' is interpreted, depends on the implementation of the Mediator.
71 *
72 * @param {I} action The action to mediate for.
73 * @param {IActorReply<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest,
74 * O extends IActorOutput>[]} testResults The actor test results for the action.
75 * @return {Promise<A extends Actor<I, T, O>>} A promise that resolves to the _best_ actor.
76 */
77 protected abstract mediateWith(action: I, testResults: IActorReply<A, I, T, O>[]): Promise<A>;
78}
79export interface IMediatorArgs<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput> {
80 /**
81 * The name for this mediator.
82 * @default {<rdf:subject>}
83 */
84 name: string;
85 /**
86 * The bus this mediator will mediate over.
87 */
88 bus: Bus<A, I, T, O>;
89}
90export declare type Mediate<I extends IAction, O extends IActorOutput, T extends IActorTest = IActorTest> = Mediator<Actor<I, T, O>, I, T, O>;