UNPKG

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