UNPKG

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