UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

194 lines 7.06 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var partial_flow_1 = require("../pin/partial-flow"); var group_1 = require("../pin/group"); var pin_map_1 = require("../pin/pin-map"); var pin_1 = require("../pin/pin"); var signature_mismatch_error_1 = require("./errors/signature-mismatch.error"); var improper_partial_flow_error_1 = require("./errors/improper-partial-flow.error"); /** * * The parent class for [agents](https://connective.dev/docs/agent). * */ var Agent = /** @class */ (function (_super) { __extends(Agent, _super); /** * * @param signature the [signature](https://connective.dev/docs/agent#signature) * of the agent. Must be determined either by instantiators or sub-classes. * */ function Agent(signature) { var _this = _super.call(this) || this; _this.signature = signature; _this._inputs = new pin_map_1.PinMap(function (label) { return _this.createInput(label); }); _this._outputs = new pin_map_1.PinMap(function (label) { return _this.createOutput(label); }); return _this; } /** * * @param label * @returns the input pin corresponding to given label * @throws an error if given label is not allowed by the agent's * [signature](https://connective.dev/docs/agent#signature). * */ Agent.prototype.in = function (label) { return this._inputs.get(label.toString()); }; /** * * @param label * @returns the output pin corresponding to given label * @throws an error if given label is not allowed by the agent's * [signature](https://connective.dev/docs/agent#signature). * */ Agent.prototype.out = function (label) { return this._outputs.get(label.toString()); }; Object.defineProperty(Agent.prototype, "entries", { /** * * @returns the entry pins for this agent for it to behave as a partial flow. * You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection). * */ get: function () { if (!this._entries) { var entries = this.createEntries(); this._entries = (entries instanceof group_1.Group) ? entries : group_1.group.apply(void 0, entries); } return this._entries; }, enumerable: true, configurable: true }); Object.defineProperty(Agent.prototype, "exits", { /** * * @returns the exit pins for this agent for it to behave as a partial flow. * You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection). * */ get: function () { if (!this._exits) { var exits = this.createExits(); this._exits = (exits instanceof group_1.Group) ? exits : group_1.group.apply(void 0, exits); } return this._exits; }, enumerable: true, configurable: true }); Object.defineProperty(Agent.prototype, "inputs", { get: function () { return this._inputs; }, enumerable: true, configurable: true }); Object.defineProperty(Agent.prototype, "outputs", { get: function () { return this._outputs; }, enumerable: true, configurable: true }); /** * * @note an Agent's `.clear()` also clears up * input and output pins. * */ Agent.prototype.clear = function () { this._inputs.clear(); this._outputs.clear(); return _super.prototype.clear.call(this); }; /** * * Checks if given input label matches the agent's * [signature](https://connective.dev/docs/agent#signature). * * Override this to change how validation of input labels occurs. * * @param label the input label to be validated * */ Agent.prototype.checkInput = function (label) { if (!this.signature.inputs || !this.signature.inputs.includes(label)) throw new signature_mismatch_error_1.InputNotInSignature(label, this.signature); }; /** * * Checks if given output label matches the agent's * [signature](https://connective.dev/docs/agent#signature). * * Override this to change how validation of output labels occurs. * */ Agent.prototype.checkOutput = function (label) { if (!this.signature.outputs.includes(label)) throw new signature_mismatch_error_1.OutputNotInSignature(label, this.signature); }; /** * * Validates given label and creates the corresponding input pin. * * Override this to change how an input pin is created. * * @param label * @returns the corresponding input pin. * */ Agent.prototype.createInput = function (label) { this.checkInput(label); return new pin_1.Pin(); }; /** * * Validates given label and creates the corresponding output pin. * * Override this to change how an output pin is created. * * @param label * @returns the corresponding output pin. * */ Agent.prototype.createOutput = function (label) { this.checkOutput(label); return new pin_1.Pin(); }; /** * * Override this to specify which pins should be considered as entries of this agent as a `PartialFlow`. * You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection). * If not overriden, the agent will be considered an improper patial flow and an error will be thrown * when used as one. * */ Agent.prototype.createEntries = function () { throw new improper_partial_flow_error_1.ImproperPartialFlow(this); }; /** * * Override this to specify which pins should be considered as exits of this agent as a `PartialFlow`. * You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection). * If not overriden, the agent will be considered an improper patial flow and an error will be thrown * when used as one. * */ Agent.prototype.createExits = function () { throw new improper_partial_flow_error_1.ImproperPartialFlow(this); }; return Agent; }(partial_flow_1.PartialFlow)); exports.Agent = Agent; //# sourceMappingURL=agent.js.map