UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

62 lines 2.02 kB
import { Control } from '../pin/control'; import map from '../pin/map'; import filter from '../pin/filter'; import group from '../pin/group'; import { Agent } from './agent'; /** * * Represents [gate](https://connective.dev/docs/gate) agents. * */ export class Gate extends Agent { constructor() { super({ inputs: ['value'], outputs: ['value'] }); this._control = new Control(); } /** * * Shortcut for `.in('value')`, the input pin receiving values. * [Read this](https://connective.dev/docs/gate#signature) for more details. * */ get input() { return this.in('value'); } /** * * Shortcut for `.out('value')`, the output emitting allowed values. * [Read this](https://connective.dev/docs/gate#signature) for more details. * */ get output() { return this.out('value'); } /** * * Each pin connected to this pin should emit a boolean value for each * value sent to `.input`, and if all are true, the value is emitted via `.output`. * [Read this](https://connective.dev/docs/gate) for more details. * */ get control() { return this._control; } createOutput(label) { this.checkOutput(label); return group(this.control, this.input) .to(new Control()) .to(filter(([ctrl, _]) => ctrl.every(signal => !!signal))) .to(map(([_, input]) => input)); } createEntries() { return [this.input]; } createExits() { return [this.output]; } clear() { this.control.clear(); return super.clear(); } } /** * * Creates a [gate](https://connective.dev/docs/gate) agent. * Gate agents await a control signal for each incoming value and either pass it along * or drop it based on the boolean value of the control signal. * [Checkout the docs](https://connective.dev/docs/gate) for examples and further information. * */ export function gate() { return new Gate(); } export default gate; //# sourceMappingURL=gate.js.map