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YACV,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;SAC9D;IACH,CAAC;IAES,QAAQ,CAAC,OAAuB,EAAE,OAAe,EAAE,IAAkB;QAC7E,MAAM,MAAM,GAAuB,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACnE,IAAI,MAAM,EAAE;YACV,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;SAC9D;IACH,CAAC;CACF;AA1ID,sBA0IC","sourcesContent":["import type { IActionContext, Logger } from '@comunica/types';\nimport type { Bus } from './Bus';\nimport { CONTEXT_KEY_LOGGER } from './ContextEntries';\n\n/**\n * An actor can act on messages of certain types and provide output of a certain type.\n *\n * The flow of an actor is as follows:\n * 1. Send a message to {@link Actor#test} to test if an actor can run that action.\n * 2. If the actor can reply to the message, let the actor run the action using {@link Actor#run}.\n *\n * An actor is typically subscribed to a bus,\n * using which the applicability to an action can be tested.\n *\n * @see Bus\n *\n * @template I The input type of an actor.\n * @template T The test type of an actor.\n * @template O The output type of an actor.\n */\nexport abstract class Actor<I extends IAction, T extends IActorTest, O extends IActorOutput> implements\n IActorArgs<I, T, O> {\n public readonly name: string;\n public readonly bus: Bus<Actor<I, T, O>, I, T, O>;\n public readonly beforeActors: Actor<I, T, O>[] = [];\n\n /**\n * All enumerable properties from the `args` object are inherited to this actor.\n *\n * The actor will subscribe to the given bus when this constructor is called.\n *\n * @param {IActorArgs<I extends IAction, T extends IActorTest, O extends IActorOutput>} args Arguments object\n * @param {string} args.name The name for this actor.\n * @param {Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus\n * The bus this actor subscribes to.\n * @throws When required arguments are missing.\n */\n protected constructor(args: IActorArgs<I, T, O>) {\n Object.assign(this, args);\n this.bus.subscribe(this);\n if (this.beforeActors.length > 0) {\n this.bus.addDependencies(this, this.beforeActors);\n }\n }\n\n /**\n * Get the logger from the given context.\n * @param {ActionContext} context An optional context.\n * @return {Logger} The logger or undefined.\n */\n public static getContextLogger(context: IActionContext): Logger | undefined {\n return context.get(CONTEXT_KEY_LOGGER);\n }\n\n /**\n * Check if this actor can run the given action,\n * without actually running it.\n *\n * @param {I} action The action to test.\n * @return {Promise<T>} A promise that resolves to the test result.\n */\n public abstract test(action: I): Promise<T>;\n\n /**\n * Run the given action on this actor.\n *\n * In most cases, this method should not be called directly.\n * Instead, {@link #runObservable} should be called.\n *\n * @param {I} action The action to run.\n * @return {Promise<T>} A promise that resolves to the run result.\n */\n public abstract run(action: I): Promise<O>;\n\n /**\n * Run the given action on this actor\n * AND invokes the {@link Bus#onRun} method.\n *\n * @param {I} action The action to run.\n * @return {Promise<T>} A promise that resolves to the run result.\n */\n public runObservable(action: I): Promise<O> {\n const output: Promise<O> = this.run(action);\n this.bus.onRun(this, action, output);\n return output;\n }\n\n /**\n * Initialize this actor.\n * This should be used for doing things that take a while,\n * such as opening files.\n *\n * @return {Promise<void>} A promise that resolves when the actor has been initialized.\n */\n public async initialize(): Promise<any> {\n return true;\n }\n\n /**\n * Deinitialize this actor.\n * This should be used for cleaning up things when the application is shut down,\n * such as closing files and removing temporary files.\n *\n * @return {Promise<void>} A promise that resolves when the actor has been deinitialized.\n */\n public async deinitialize(): Promise<any> {\n return true;\n }\n\n /* Proxy methods for the (optional) logger that is defined in the context */\n\n protected getDefaultLogData(context: IActionContext, data?: (() => any)): any {\n const dataActual = data ? data() : {};\n dataActual.actor = this.name;\n return dataActual;\n }\n\n protected logTrace(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.trace(message, this.getDefaultLogData(context, data));\n }\n }\n\n protected logDebug(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.debug(message, this.getDefaultLogData(context, data));\n }\n }\n\n protected logInfo(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.info(message, this.getDefaultLogData(context, data));\n }\n }\n\n protected logWarn(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.warn(message, this.getDefaultLogData(context, data));\n }\n }\n\n protected logError(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.error(message, this.getDefaultLogData(context, data));\n }\n }\n\n protected logFatal(context: IActionContext, message: string, data?: (() => any)): void {\n const logger: Logger | undefined = Actor.getContextLogger(context);\n if (logger) {\n logger.fatal(message, this.getDefaultLogData(context, data));\n }\n }\n}\n\nexport interface IActorArgs<I extends IAction, T extends IActorTest, O extends IActorOutput> {\n /**\n * The name for this actor.\n * @default {<rdf:subject>}\n */\n name: string;\n /**\n * The bus this actor subscribes to.\n */\n bus: Bus<Actor<I, T, O>, I, T, O>;\n /**\n * Actor that must be registered in the bus before this actor.\n */\n beforeActors?: Actor<I, T, O>[];\n}\n\n/**\n * Data interface for the type of action.\n */\nexport interface IAction {\n /**\n * The input context that is passed through by actors.\n */\n context: IActionContext;\n}\n\n/**\n * Data interface for the type of an actor test result.\n */\nexport interface IActorTest {\n\n}\n\n/**\n * Data interface for the type of an actor run result.\n */\nexport interface IActorOutput {\n\n}\n"]}
\No newline at end of file