UNPKG

2.11 kBTypeScriptView Raw
1import type { Actor, IAction, IActorOutput, IActorTest } from './Actor';
2import type { IActorReply, IBusArgs } from './Bus';
3import { Bus } from './Bus';
4/**
5 * A bus that indexes identified actors,
6 * so that actions with a corresponding identifier can be published more efficiently.
7 *
8 * Multiple actors with the same identifier can be subscribed.
9 *
10 * If actors or actions do not have a valid identifier,
11 * then this will fallback to the normal bus behaviour.
12 *
13 * @see Bus
14 *
15 * @template A The actor type that can subscribe to the sub.
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 class BusIndexed<A extends Actor<I, T, O, any>, I extends IAction, T extends IActorTest, O extends IActorOutput> extends Bus<A, I, T, O> {
21 protected readonly actorsIndex: Record<string, A[]>;
22 protected readonly actorIdentifierFields: string[];
23 protected readonly actionIdentifierFields: string[];
24 /**
25 * All enumerable properties from the `args` object are inherited to this bus.
26 *
27 * @param {IBusIndexedArgs} args Arguments object
28 * @param {string} args.name The name for the bus
29 * @throws When required arguments are missing.
30 */
31 constructor(args: IBusIndexedArgs);
32 subscribe(actor: A): void;
33 unsubscribe(actor: A): boolean;
34 publish(action: I): IActorReply<A, I, T, O>[];
35 protected getActorIdentifiers(actor: A): string[] | undefined;
36 protected getActionIdentifier(action: I): string;
37}
38export interface IBusIndexedArgs extends IBusArgs {
39 /**
40 * Keys to follow down from the actor object.
41 * The value at the location following these keys should be a string, a string array, or undefined.
42 * If the value is a string array, all strings will be registered as keys that map to the actor.
43 */
44 actorIdentifierFields: string[];
45 /**
46 * Keys to follow down from the action object.
47 * The value at the location following these keys should be a string or be undefined.
48 */
49 actionIdentifierFields: string[];
50}