UNPKG

4.7 kBTypeScriptView Raw
1import type { IActionContext, Logger } from '@comunica/types';
2import type { Bus } from './Bus';
3import type { TestResult } from './TestResult';
4/**
5 * An actor can act on messages of certain types and provide output of a certain type.
6 *
7 * The flow of an actor is as follows:
8 * 1. Send a message to {@link Actor#test} to test if an actor can run that action.
9 * 2. If the actor can reply to the message, let the actor run the action using {@link Actor#run}.
10 *
11 * An actor is typically subscribed to a bus,
12 * using which the applicability to an action can be tested.
13 *
14 * @see Bus
15 *
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 * @template TS The test side data type.
20 */
21export declare abstract class Actor<I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> implements IActorArgs<I, T, O, TS> {
22 readonly name: string;
23 readonly bus: Bus<Actor<I, T, O, TS>, I, T, O, TS>;
24 readonly beforeActors: Actor<I, T, O, TS>[];
25 /**
26 * All enumerable properties from the `args` object are inherited to this actor.
27 *
28 * The actor will subscribe to the given bus when this constructor is called.
29 *
30 * @param {IActorArgs<I extends IAction, T extends IActorTest, O extends IActorOutput>} args Arguments object
31 * @param {string} args.name The name for this actor.
32 * @param {Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus
33 * The bus this actor subscribes to.
34 * @throws When required arguments are missing.
35 */
36 protected constructor(args: IActorArgs<I, T, O, TS>);
37 /**
38 * Get the logger from the given context.
39 * @param {ActionContext} context An optional context.
40 * @return {Logger} The logger or undefined.
41 */
42 static getContextLogger(context: IActionContext): Logger | undefined;
43 /**
44 * Check if this actor can run the given action,
45 * without actually running it.
46 *
47 * @param {I} action The action to test.
48 * @return {Promise<T>} A promise that resolves to the test result.
49 */
50 abstract test(action: I): Promise<TestResult<T, TS>>;
51 /**
52 * Run the given action on this actor.
53 *
54 * In most cases, this method should not be called directly.
55 * Instead, {@link #runObservable} should be called.
56 *
57 * @param {I} action The action to run.
58 * @return {Promise<T>} A promise that resolves to the run result.
59 */
60 abstract run(action: I, sideData: TS): Promise<O>;
61 /**
62 * Run the given action on this actor
63 * AND invokes the {@link Bus#onRun} method.
64 *
65 * @param {I} action The action to run.
66 * @return {Promise<T>} A promise that resolves to the run result.
67 */
68 runObservable(action: I, sideData: TS): Promise<O>;
69 protected getDefaultLogData(context: IActionContext, data?: (() => any)): any;
70 protected logTrace(context: IActionContext, message: string, data?: (() => any)): void;
71 protected logDebug(context: IActionContext, message: string, data?: (() => any)): void;
72 protected logInfo(context: IActionContext, message: string, data?: (() => any)): void;
73 protected logWarn(context: IActionContext, message: string, data?: (() => any)): void;
74 protected logError(context: IActionContext, message: string, data?: (() => any)): void;
75 protected logFatal(context: IActionContext, message: string, data?: (() => any)): void;
76}
77export interface IActorArgs<I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> {
78 /**
79 * The name for this actor.
80 * @default {<rdf:subject>}
81 */
82 name: string;
83 /**
84 * The bus this actor subscribes to.
85 */
86 bus: Bus<Actor<I, T, O, TS>, I, T, O, TS>;
87 /**
88 * The message that will be configured in the bus for reporting failures.
89 *
90 * This message may be a template string that contains references to the executed `action`.
91 * For example, the following templated string is allowed:
92 * "RDF dereferencing failed: no actors could handle ${action.handle.mediaType}"
93 */
94 busFailMessage?: string;
95 /**
96 * Actor that must be registered in the bus before this actor.
97 */
98 beforeActors?: Actor<I, T, O, TS>[];
99}
100/**
101 * Data interface for the type of action.
102 */
103export interface IAction {
104 /**
105 * The input context that is passed through by actors.
106 */
107 context: IActionContext;
108}
109/**
110 * Data interface for the type of an actor test result.
111 */
112export interface IActorTest {
113}
114/**
115 * Data interface for the type of an actor run result.
116 */
117export interface IActorOutput {
118}
119
\No newline at end of file