UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

110 lines (109 loc) 3.28 kB
import { Bindable } from '../shared/bindable'; import { PinLike } from '../pin/pin-like'; import { Signature } from './signature'; import { Agent } from './agent'; declare type _ChildType = PinLike | Agent; /** * * Represents [compositions](https://connective.dev/docs/composition). This class * is to be used directly if you want to create class-based compositions, otherwise * utilize [`composition()`](https://connective.dev/docs/composition) method. * */ export declare abstract class Composition extends Agent implements Bindable { private _bindables; private _children; /** * * @param signature the signature of the composition * */ constructor(signature: Signature); /** * * Override this to modify the initialization process of a composition. * This function is called by parent's constructor, so if you want to * invoke `.build()` and `.wire()` after some child-class properties have been * initialized as well, you would need to override this function. This is a typical * scenario in case of parametric class-based compositions. * */ protected init(): void; /** * * Override this to define the child pins and agents of your composition, * using `.add()` method: * * ```typescript * build() { * this.add('myState', state()); * this.add('mySink', sink()); * this.myOtherState = this.add('myOtherState', state()) as State; * } * ``` * */ protected abstract build(): void; /** * * Override this to wire the pins and agents you defined in `.build()` to each other: * * ```typescript * build() { * this.agent('myState').out('value').to(this.pin('mySink')); * this.pin('mySink').to(this.myOtherState).to(this.out('myOutput')); * } * ``` * */ protected abstract wire(): void; protected add(child: _ChildType): _ChildType; protected add(name: string, child: _ChildType): _ChildType; /** * * @param name * @returns the child with given name. * @throws an error if no child with given name is defined. * */ protected child(name: string | number): _ChildType; /** * * @param name * @returns the pin child with given name. * @throws an error if no child with given name is defined or if it is not a pin. * */ protected pin(name: string | number): PinLike; /** * * @param name * @returns the child agent with given name. * @throws an error if no child with given name is defined or if it is not an agent. * */ protected agent(name: string | number): Agent; /** * * Registers a `Bindable` that will be bound when `.bind()` is called on this composition. * * @param bindable * */ protected toBind(bindable: Bindable): this; /** * * Binds all registered `Bindable`s, including bindable children like * [states](https://connective.dev/docs/state) and * [sinks](https://connective.dev/docs/sink). * */ bind(): this; /** * * @note `.clear()` on `Composition` also clears all registered children. * */ clear(): this; } export {};