UNPKG

@comunica/core

Version:

Lightweight, semantic and modular actor framework

1 lines 9.02 kB
{"version":3,"file":"Actor.js","sourceRoot":"","sources":["Actor.ts"],"names":[],"mappings":";;;AAEA,qDAAsD;AAGtD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAsB,KAAK;IAMzB;;;;;;;;;;OAUG;IACH,YAAsB,IAA6B;QAbnC,iBAAY,GAAyB,EAAE,CAAC;QActD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1B,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QACzB,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,IAAI,CAAC,GAAG,CAAC,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC;QAC7C,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,gBAAgB,CAAC,OAAuB;QACpD,OAAO,OAAO,CAAC,GAAG,CAAC,mCAAkB,CAAC,CAAC;IACzC,CAAC;IAsBD;;;;;;OAMG;IACI,aAAa,CAAC,MAAS,EAAE,QAAY;QAC1C,MAAM,MAAM,GAAe,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACtD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QACrC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,4EAA4E;IAElE,iBAAiB,CAAC,OAAuB,EAAE,IAAkB;QACrE,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACtC,UAAU,CAAC,KAAK,GAAG,IAAI,CAA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AM,GAAuB,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACnE,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;QAC/D,CAAC;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,CAAC;YACX,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;CACF;AAvHD,sBAuHC","sourcesContent":["import type { IActionContext, Logger } from '@comunica/types';\nimport type { Bus } from './Bus';\nimport { CONTEXT_KEY_LOGGER } from './ContextEntries';\nimport type { TestResult } from './TestResult';\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 * @template TS The test side data type.\n */\nexport abstract class Actor<I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined>\nimplements IActorArgs<I, T, O, TS> {\n public readonly name: string;\n public readonly bus: Bus<Actor<I, T, O, TS>, I, T, O, TS>;\n public readonly beforeActors: Actor<I, T, O, TS>[] = [];\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, TS>) {\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 if (args.busFailMessage) {\n this.bus.failMessage = args.busFailMessage;\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<TestResult<T, TS>>;\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, sideData: TS): 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, sideData: TS): Promise<O> {\n const output: Promise<O> = this.run(action, sideData);\n this.bus.onRun(this, action, output);\n return output;\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, TS = undefined> {\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, TS>, I, T, O, TS>;\n /**\n * The message that will be configured in the bus for reporting failures.\n *\n * This message may be a template string that contains references to the executed `action`.\n * For example, the following templated string is allowed:\n * \"RDF dereferencing failed: no actors could handle ${action.handle.mediaType}\"\n */\n busFailMessage?: string;\n /**\n * Actor that must be registered in the bus before this actor.\n */\n beforeActors?: Actor<I, T, O, TS>[];\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"]}