1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Actor = void 0;
|
4 | const ContextEntries_1 = require("./ContextEntries");
|
5 | /**
|
6 | * An actor can act on messages of certain types and provide output of a certain type.
|
7 | *
|
8 | * The flow of an actor is as follows:
|
9 | * 1. Send a message to {@link Actor#test} to test if an actor can run that action.
|
10 | * 2. If the actor can reply to the message, let the actor run the action using {@link Actor#run}.
|
11 | *
|
12 | * An actor is typically subscribed to a bus,
|
13 | * using which the applicability to an action can be tested.
|
14 | *
|
15 | * @see Bus
|
16 | *
|
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 | */
|
21 | class Actor {
|
22 | /**
|
23 | * All enumerable properties from the `args` object are inherited to this actor.
|
24 | *
|
25 | * The actor will subscribe to the given bus when this constructor is called.
|
26 | *
|
27 | * @param {IActorArgs<I extends IAction, T extends IActorTest, O extends IActorOutput>} args Arguments object
|
28 | * @param {string} args.name The name for this actor.
|
29 | * @param {Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus
|
30 | * The bus this actor subscribes to.
|
31 | * @throws When required arguments are missing.
|
32 | */
|
33 | constructor(args) {
|
34 | this.beforeActors = [];
|
35 | Object.assign(this, args);
|
36 | this.bus.subscribe(this);
|
37 | if (this.beforeActors.length > 0) {
|
38 | this.bus.addDependencies(this, this.beforeActors);
|
39 | }
|
40 | }
|
41 | /**
|
42 | * Get the logger from the given context.
|
43 | * @param {ActionContext} context An optional context.
|
44 | * @return {Logger} The logger or undefined.
|
45 | */
|
46 | static getContextLogger(context) {
|
47 | return context.get(ContextEntries_1.CONTEXT_KEY_LOGGER);
|
48 | }
|
49 | /**
|
50 | * Run the given action on this actor
|
51 | * AND invokes the {@link Bus#onRun} method.
|
52 | *
|
53 | * @param {I} action The action to run.
|
54 | * @return {Promise<T>} A promise that resolves to the run result.
|
55 | */
|
56 | runObservable(action) {
|
57 | const output = this.run(action);
|
58 | this.bus.onRun(this, action, output);
|
59 | return output;
|
60 | }
|
61 | /**
|
62 | * Initialize this actor.
|
63 | * This should be used for doing things that take a while,
|
64 | * such as opening files.
|
65 | *
|
66 | * @return {Promise<void>} A promise that resolves when the actor has been initialized.
|
67 | */
|
68 | async initialize() {
|
69 | return true;
|
70 | }
|
71 | /**
|
72 | * Deinitialize this actor.
|
73 | * This should be used for cleaning up things when the application is shut down,
|
74 | * such as closing files and removing temporary files.
|
75 | *
|
76 | * @return {Promise<void>} A promise that resolves when the actor has been deinitialized.
|
77 | */
|
78 | async deinitialize() {
|
79 | return true;
|
80 | }
|
81 | /* Proxy methods for the (optional) logger that is defined in the context */
|
82 | getDefaultLogData(context, data) {
|
83 | const dataActual = data ? data() : {};
|
84 | dataActual.actor = this.name;
|
85 | return dataActual;
|
86 | }
|
87 | logTrace(context, message, data) {
|
88 | const logger = Actor.getContextLogger(context);
|
89 | if (logger) {
|
90 | logger.trace(message, this.getDefaultLogData(context, data));
|
91 | }
|
92 | }
|
93 | logDebug(context, message, data) {
|
94 | const logger = Actor.getContextLogger(context);
|
95 | if (logger) {
|
96 | logger.debug(message, this.getDefaultLogData(context, data));
|
97 | }
|
98 | }
|
99 | logInfo(context, message, data) {
|
100 | const logger = Actor.getContextLogger(context);
|
101 | if (logger) {
|
102 | logger.info(message, this.getDefaultLogData(context, data));
|
103 | }
|
104 | }
|
105 | logWarn(context, message, data) {
|
106 | const logger = Actor.getContextLogger(context);
|
107 | if (logger) {
|
108 | logger.warn(message, this.getDefaultLogData(context, data));
|
109 | }
|
110 | }
|
111 | logError(context, message, data) {
|
112 | const logger = Actor.getContextLogger(context);
|
113 | if (logger) {
|
114 | logger.error(message, this.getDefaultLogData(context, data));
|
115 | }
|
116 | }
|
117 | logFatal(context, message, data) {
|
118 | const logger = Actor.getContextLogger(context);
|
119 | if (logger) {
|
120 | logger.fatal(message, this.getDefaultLogData(context, data));
|
121 | }
|
122 | }
|
123 | }
|
124 | exports.Actor = Actor;
|
125 | //# sourceMappingURL=Actor.js.map |
\ | No newline at end of file |