UNPKG

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