@comunica/core
- Version 4.0.2
- Published
- 176 kB
- 2 dependencies
- MIT license
Install
npm i @comunica/core
yarn add @comunica/core
pnpm add @comunica/core
Overview
Lightweight, semantic and modular actor framework
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Variables
variable CONTEXT_KEY_LOGGER
const CONTEXT_KEY_LOGGER: ActionContextKey<Logger>;
Functions
function failTest
failTest: (message: string) => TestResultFailed;
Create a new test result that represents a test failure.
Parameter message
The error message that describes the failure.
function passTest
passTest: <T>(value: T) => TestResultPassed<T, undefined>;
Create a new test result that represents a passed value.
Parameter value
The value the test passed with.
function passTestVoid
passTestVoid: () => TestResultPassed<any, undefined>;
Create a new test result that represents a passed void value.
function passTestVoidWithSideData
passTestVoidWithSideData: <TS>(sideData: TS) => TestResultPassed<any, TS>;
Create a new test result that represents a passed void value with side data.
Parameter sideData
Additional data to pass to the run phase.
function passTestWithSideData
passTestWithSideData: <T, S>(value: T, sideData: S) => TestResultPassed<T, S>;
Create a new test result that represents a passed value with side data.
Parameter value
The value the test passed with.
Parameter sideData
Additional data to pass to the run phase.
Classes
class ActionContext
class ActionContext implements IActionContext {}
Implementation of IActionContext using Immutable.js.
constructor
constructor(data?: Record<string, any>);
method delete
delete: <V>(key: IActionContextKey<V>) => IActionContext;
method ensureActionContext
static ensureActionContext: ( maybeActionContext?: IActionContext | Record<string, any>) => IActionContext;
Convert the given object to an action context object if it is not an action context object yet. If it already is an action context object, return the object as-is.
Parameter maybeActionContext
An action context or record. {ActionContext} An action context object.
method get
get: <V>(key: IActionContextKey<V>) => V | undefined;
method getRaw
getRaw: (key: string) => any | undefined;
method getSafe
getSafe: <V>(key: IActionContextKey<V>) => V;
method has
has: <V>(key: IActionContextKey<V>) => boolean;
method hasRaw
hasRaw: (key: string) => boolean;
method keys
keys: () => IActionContextKey<any>[];
method merge
merge: (...contexts: IActionContext[]) => IActionContext;
method set
set: <V>(key: IActionContextKey<V>, value: V) => IActionContext;
method setDefault
setDefault: <V>(key: IActionContextKey<V>, value: V) => IActionContext;
Will only set the value if the key is not already set.
method setRaw
setRaw: (key: string, value: any) => IActionContext;
method toJS
toJS: () => any;
method toString
toString: () => string;
class ActionContextKey
class ActionContextKey<V> implements IActionContextKey<V> {}
Simple implementation of IActionContextKey.
constructor
constructor(name: string);
property dummy
readonly dummy: {};
property name
readonly name: string;
A unique context key name.
class ActionObserver
abstract class ActionObserver< I extends IAction, O extends IActorOutput, TS = undefined> {}
An ActionObserver can passively listen to Actor#run inputs and outputs for all actors on a certain bus.
ActionObserver should not edit inputs and outputs, they should be considered immutable.
See Also
Actor
Bus
I The input type of an actor. O The output type of an actor. TS The test side data type.
constructor
protected constructor(args: IActionObserverArgs<I, O, undefined>);
All enumerable properties from the
args
object are inherited to this observer.The observer will NOT automatically subscribe to the given bus when this constructor is called.
Parameter args
Arguments object
Throws
When required arguments are missing.
property bus
readonly bus: Bus<Actor<I, IActorTest, O, TS>, I, IActorTest, O, TS>;
property name
readonly name: string;
method onRun
abstract onRun: ( actor: Actor<I, IActorTest, O, TS>, action: I, output: Promise<O>) => void;
Invoked when an action was run by an actor.
Parameter actor
The action on which the Actor#run method was invoked.
Parameter action
The original action input.
Parameter output
A promise resolving to the final action output.
class Actor
abstract class Actor< I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> implements IActorArgs<I, T, O, TS> {}
An actor can act on messages of certain types and provide output of a certain type.
The flow of an actor is as follows: 1. Send a message to Actor#test to test if an actor can run that action. 2. If the actor can reply to the message, let the actor run the action using Actor#run.
An actor is typically subscribed to a bus, using which the applicability to an action can be tested.
See Also
Bus
I The input type of an actor. T The test type of an actor. O The output type of an actor. TS The test side data type.
constructor
protected constructor(args: IActorArgs<I, T, O, TS>);
All enumerable properties from the
args
object are inherited to this actor.The actor will subscribe to the given bus when this constructor is called.
Parameter args
Arguments object
Parameter
{string} args.name The name for this actor.
Parameter
{Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus The bus this actor subscribes to.
Throws
When required arguments are missing.
property beforeActors
readonly beforeActors: Actor<I, T, O, TS>[];
property bus
readonly bus: Bus<Actor<I, T, O, TS>, I, T, O, TS>;
property name
readonly name: string;
method getContextLogger
static getContextLogger: (context: IActionContext) => Logger | undefined;
Get the logger from the given context.
Parameter context
An optional context. {Logger} The logger or undefined.
method getDefaultLogData
protected getDefaultLogData: (context: IActionContext, data?: () => any) => any;
method logDebug
protected logDebug: ( context: IActionContext, message: string, data?: () => any) => void;
method logError
protected logError: ( context: IActionContext, message: string, data?: () => any) => void;
method logFatal
protected logFatal: ( context: IActionContext, message: string, data?: () => any) => void;
method logInfo
protected logInfo: ( context: IActionContext, message: string, data?: () => any) => void;
method logTrace
protected logTrace: ( context: IActionContext, message: string, data?: () => any) => void;
method logWarn
protected logWarn: ( context: IActionContext, message: string, data?: () => any) => void;
method run
abstract run: (action: I, sideData: TS) => Promise<O>;
Run the given action on this actor.
In most cases, this method should not be called directly. Instead, should be called.
Parameter action
The action to run. {Promise} A promise that resolves to the run result.
method runObservable
runObservable: (action: I, sideData: TS) => Promise<O>;
Run the given action on this actor AND invokes the Bus#onRun method.
Parameter action
The action to run. {Promise} A promise that resolves to the run result.
method test
abstract test: (action: I) => Promise<TestResult<T, TS>>;
Check if this actor can run the given action, without actually running it.
Parameter action
The action to test. {Promise} A promise that resolves to the test result.
class Bus
class Bus< A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> implements IBusArgs {}
A publish-subscribe bus for sending actions to actors to test whether or not they can run an action.
This bus does not run the action itself, for that a Mediator can be used.
See Also
Actor
Mediator
A The actor type that can subscribe to the sub. I The input type of an actor. T The test type of an actor. O The output type of an actor. TS The test side data type.
constructor
constructor(args: IBusArgs);
All enumerable properties from the
args
object are inherited to this bus.Parameter args
Arguments object
Parameter
{string} args.name The name for the bus
Throws
When required arguments are missing.
property actors
protected readonly actors: A[];
property dependencyLinks
protected readonly dependencyLinks: Map<A, A[]>;
property failMessage
failMessage: string;
property name
readonly name: string;
property observers
protected readonly observers: ActionObserver<I, O, TS>[];
method addDependencies
addDependencies: (dependent: A, dependencies: A[]) => void;
Indicate that the given actor has the given actor dependencies.
This will ensure that the given actor will be present in the bus *before* the given dependencies.
Parameter dependent
A dependent actor that will be placed before the given actors.
Parameter dependencies
Actor dependencies that will be placed after the given actor.
method onRun
onRun: (actor: Actor<I, T, O, TS>, action: I, output: Promise<O>) => void;
Invoked when an action was run by an actor.
Parameter actor
The action on which the Actor#run method was invoked.
Parameter action
The original action input.
Parameter output
A promise resolving to the final action output.
method publish
publish: (action: I) => IActorReply<A, I, T, O, TS>[];
Publish an action to all actors in the bus to test if they can run the action.
Parameter action
An action to publish {IActorReply<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>[]} An array of reply objects. Each object contains a reference to the actor, and a promise to its Actor#test result.
method reorderForDependencies
reorderForDependencies: () => void;
Reorder the bus based on all present dependencies.
method subscribe
subscribe: (actor: A) => void;
Subscribe the given actor to the bus. After this, the given actor can be unsubscribed from the bus by calling Bus#unsubscribe.
An actor that is subscribed multiple times will exist that amount of times in the bus.
Parameter actor
The actor to subscribe.
method subscribeObserver
subscribeObserver: (observer: ActionObserver<I, O, TS>) => void;
Subscribe the given observer to the bus. After this, the given observer can be unsubscribed from the bus by calling Bus#unsubscribeObserver.
An observer that is subscribed multiple times will exist that amount of times in the bus.
Parameter observer
The observer to subscribe.
method unsubscribe
unsubscribe: (actor: A) => boolean;
Unsubscribe the given actor from the bus.
An actor that is subscribed multiple times will be unsubscribed only once.
Parameter actor
The actor to unsubscribe {boolean} If the given actor was successfully unsubscribed, otherwise it was not subscribed before.
method unsubscribeObserver
unsubscribeObserver: (observer: ActionObserver<I, O, TS>) => boolean;
Unsubscribe the given observer from the bus.
An observer that is subscribed multiple times will be unsubscribed only once.
Parameter observer
The observer to unsubscribe. {boolean} If the given observer was successfully unsubscribed, otherwise it was not subscribed before.
class BusIndexed
class BusIndexed< A extends Actor<I, T, O, any>, I extends IAction, T extends IActorTest, O extends IActorOutput> extends Bus<A, I, T, O> {}
A bus that indexes identified actors, so that actions with a corresponding identifier can be published more efficiently.
Multiple actors with the same identifier can be subscribed.
If actors or actions do not have a valid identifier, then this will fallback to the normal bus behaviour.
See Also
Bus
A The actor type that can subscribe to the sub. I The input type of an actor. T The test type of an actor. O The output type of an actor.
constructor
constructor(args: IBusIndexedArgs);
All enumerable properties from the
args
object are inherited to this bus.Parameter args
Arguments object
Parameter
{string} args.name The name for the bus
Throws
When required arguments are missing.
property actionIdentifierFields
protected readonly actionIdentifierFields: string[];
property actorIdentifierFields
protected readonly actorIdentifierFields: string[];
property actorsIndex
protected readonly actorsIndex: Record<string, A[]>;
method getActionIdentifier
protected getActionIdentifier: (action: I) => string;
method getActorIdentifiers
protected getActorIdentifiers: (actor: A) => string[] | undefined;
method publish
publish: (action: I) => IActorReply<A, I, T, O>[];
method subscribe
subscribe: (actor: A) => void;
method unsubscribe
unsubscribe: (actor: A) => boolean;
class Mediator
abstract class Mediator< A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> implements IMediatorArgs<A, I, T, O, TS> {}
A mediator can mediate an action over a bus of actors.
It does the following: 1. Accepts an action in Mediator#mediate. 2. Sends the action to the bus to test its applicability on all actors. 3. It _mediates_ over these test results. 4. It selects the _best_ actor. 5. The action is run by the _best_ actor, and the result if returned.
The _mediates_ and _best_ parts are filled in by subclasses of this abstract Mediator class.
A The type of actor to mediator over. I The input type of an actor. T The test type of an actor. O The output type of an actor.
constructor
protected constructor(args: IMediatorArgs<A, I, T, O, TS>);
All enumerable properties from the
args
object are inherited to this mediator.Parameter args
Arguments object
Parameter
{string} args.name The name for this mediator.
Parameter
{Bus<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>} args.bus The bus this mediator will mediate over.
Throws
When required arguments are missing.
property bus
readonly bus: Bus<A, I, T, O, TS>;
property name
readonly name: string;
method constructFailureMessage
protected constructFailureMessage: ( action: I, actorFailures: string[]) => string;
Construct a human-friendly failure message that accumulates the given actors's failure messages.
Parameter action
The action that was executed.
Parameter actorFailures
The failure messages that were collected from actor tests based on the given executed action.
method getObjectValue
protected static getObjectValue: (obj: any, path: string[]) => any;
method mediate
mediate: (action: I) => Promise<O>;
Mediate for the given action.
This will send the test action on all actors in the bus. The action will be run on the actor that tests _best_, of which the result will be returned.
Parameter action
The action to mediate for. {Promise<O extends IActorOutput>} A promise that resolves to the mediation result.
method mediateActor
mediateActor: (action: I) => Promise<TestResult<A, TS>>;
Mediate for the given action to get an actor.
This will send the test action on all actors in the bus. The actor that tests _best_ will be returned.
Parameter action
The action to mediate for. {Promise<O extends IActorOutput>} A promise that resolves to the _best_ actor.
method mediateTestable
mediateTestable: (action: I) => Promise<TestResult<O, TS>>;
Mediate for the given action.
This will send the test action on all actors in the bus. The action will be run on the actor that tests _best_, of which the result will be returned.
Parameter action
The action to mediate for. {Promise<O extends IActorOutput>} A promise that resolves to the mediation result.
method mediateWith
protected abstract mediateWith: ( action: I, testResults: IActorReply<A, I, T, O, TS>[]) => Promise<TestResult<A, TS>>;
Mediate for the given action with the given actor test results for the action.
One actor must be returned that provided the _best_ test result. How '_best_' is interpreted, depends on the implementation of the Mediator.
Parameter action
The action to mediate for.
Parameter testResults
The actor test results for the action. {Promise<A extends Actor<I, T, O>>} A promise that resolves to the _best_ actor.
method publish
publish: (action: I) => IActorReply<A, I, T, O, TS>[];
Publish the given action in the bus.
This will send the test action on all actors in the bus. All actor replies will be returned.
Parameter action
The action to mediate for. {IActorReply<A extends Actor<I, T, O>, I extends IAction, T extends IActorTest, O extends IActorOutput>[]} The list of actor replies.
class TestResultFailed
class TestResultFailed {}
A failed test result. This should not be constructed manually. Instead,
testFail
should be used.
constructor
constructor(failMessage: string);
property failMessage
protected readonly failMessage: string;
method get
get: () => undefined;
Get the value of the passed test, or undefined if the test failed.
method getFailMessage
getFailMessage: () => string;
Get the failure message callback of the failed test, or undefined if the test passed.
method getOrThrow
getOrThrow: () => never;
Get the value of the passed test, or throw an error if the test failed.
method getSideData
getSideData: () => never;
The side data that will be passed to run.
method isFailed
isFailed: () => this is TestResultFailed;
Check if the test has failed. If true, it will contain a failure message.
method isPassed
isPassed: () => this is TestResultPassed<any, any>;
Check if the test has passed. If true, it will contain a value.
method map
map: () => TestResultFailed;
For passed tests, map the passed value to another value. Failed tests will remain unchanged.
This will not mutate the test result, and instead return a new test result.
method mapAsync
mapAsync: () => Promise<TestResultFailed>;
For passed tests, asynchronously map the passed value to another value. Failed tests will remain unchanged.
This will not mutate the test result, and instead return a new test result.
class TestResultPassed
class TestResultPassed<T, TS> {}
A passed test result. This should not be constructed manually. Instead,
testPass
should be used.
constructor
constructor(passValue: {}, sideData: {});
property sideData
protected readonly sideData: {};
property value
protected readonly value: {};
method get
get: () => T;
Get the value of the passed test, or undefined if the test failed.
method getFailMessage
getFailMessage: () => undefined;
Get the failure message callback of the failed test, or undefined if the test passed.
method getOrThrow
getOrThrow: () => T;
Get the value of the passed test, or throw an error if the test failed.
method getSideData
getSideData: () => TS;
The side data that will be passed to run.
method isFailed
isFailed: () => this is TestResultFailed;
Check if the test has failed. If true, it will contain a failure message.
method isPassed
isPassed: () => this is TestResultPassed<T, TS>;
Check if the test has passed. If true, it will contain a value.
method map
map: <T2>(mapper: (value: T, sideData: TS) => T2) => TestResultPassed<T2, TS>;
For passed tests, map the passed value to another value. Failed tests will remain unchanged.
This will not mutate the test result, and instead return a new test result.
Parameter mapper
A function that will transform the passed value.
method mapAsync
mapAsync: <T2>( mapper: (value: T, sideData: TS) => Promise<T2>) => Promise<TestResultPassed<T2, TS>>;
For passed tests, asynchronously map the passed value to another value. Failed tests will remain unchanged.
This will not mutate the test result, and instead return a new test result.
Parameter mapper
A function that will transform the passed value.
Interfaces
interface IAction
interface IAction {}
Data interface for the type of action.
property context
context: IActionContext;
The input context that is passed through by actors.
interface IActionObserverArgs
interface IActionObserverArgs< I extends IAction, O extends IActorOutput, TS = undefined> {}
interface IActorArgs
interface IActorArgs< I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> {}
property beforeActors
beforeActors?: Actor<I, T, O, TS>[];
Actor that must be registered in the bus before this actor.
property bus
bus: Bus<Actor<I, T, O, TS>, I, T, O, TS>;
The bus this actor subscribes to.
property busFailMessage
busFailMessage?: string;
The message that will be configured in the bus for reporting failures.
This message may be a template string that contains references to the executed
action
. For example, the following templated string is allowed: "RDF dereferencing failed: no actors could handle ${action.handle.mediaType}"
property name
name: string;
The name for this actor. {<rdf:subject>}
interface IActorOutput
interface IActorOutput {}
Data interface for the type of an actor run result.
interface IActorReply
interface IActorReply< A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> {}
Data interface for holding an actor and a promise to a reply from that actor.
interface IActorTest
interface IActorTest {}
Data interface for the type of an actor test result.
interface IBusArgs
interface IBusArgs {}
property name
name: string;
The name for this bus. {<rdf:subject>}
interface IBusIndexedArgs
interface IBusIndexedArgs extends IBusArgs {}
property actionIdentifierFields
actionIdentifierFields: string[];
Keys to follow down from the action object. The value at the location following these keys should be a string or be undefined.
property actorIdentifierFields
actorIdentifierFields: string[];
Keys to follow down from the actor object. The value at the location following these keys should be a string, a string array, or undefined. If the value is a string array, all strings will be registered as keys that map to the actor.
interface IMediatorArgs
interface IMediatorArgs< A extends Actor<I, T, O, TS>, I extends IAction, T extends IActorTest, O extends IActorOutput, TS = undefined> {}
Type Aliases
type IBus
type IBus< I extends IAction = IAction, O extends IActorOutput = IActorOutput, T extends IActorTest = IActorTest, TS = undefined> = Bus<Actor<I, T, O, TS>, I, T, O, TS>;
type IReply
type IReply< I extends IAction = IAction, O extends IActorOutput = IActorOutput, T extends IActorTest = IActorTest, TS = undefined> = IActorReply<Actor<I, T, O, TS>, I, T, O, TS>;
type Mediate
type Mediate< I extends IAction, O extends IActorOutput, T extends IActorTest = IActorTest, TS = undefined> = Mediator<Actor<I, T, O, TS>, I, T, O, TS>;
type TestResult
type TestResult<T, TS = undefined> = TestResultPassed<T, TS> | TestResultFailed;
A test result represents the result of an actor test that can either be passed or failed.
Test results are immutable.
Package Files (9)
Dependencies (2)
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@comunica/core
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@comunica/core)
- HTML<a href="https://www.jsdocs.io/package/@comunica/core"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 5083 ms. - Missing or incorrect documentation? Open an issue for this package.