@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
97 lines • 2.99 kB
JavaScript
import { Tracker } from '../shared/tracker';
import group, { Group } from './group';
//
// TODO: write tests for this.
// TODO: make Agent inherit this, but default throwing error when used like this.
// TODO: make common agent types act as proper partial flows.
//
/**
*
* Represents a partial reactive flow, with some entry pins going into it
* and some exit pins coming out of it.
*
*/
export class PartialFlow extends Tracker {
/**
*
* Connects all given pins to all of its entry pins
*
* @param pins
* @returns a [group](https://connective.dev/docs/group) of the given pins. If any `PartialFlow`
* was among the given pins, its entry pins will be added to the group.
*
*/
from(...pins) {
return this.entries.from(...pins);
}
/**
*
* Connects all of its exit pins to given pins
*
* @param pins
* @returns a [group](https://connective.dev/docs/group) of the given pins. If any `PartialFlow`
* was among the given pins, its exit pins added to the group.
*
*/
to(...pins) {
return this.exits.to(...pins);
}
/**
*
* Connects all given pins serially to its entry pins
*
* @param pins
* @returns a [group](https://connective.dev/docs/group) of the given pins. If any `PartialFlow`
* was among the given pins, its entry pins will be added to the group.
*
*/
serialFrom(...pins) {
return this.entries.serialFrom(...pins);
}
/**
*
* Connects all of its exit pins to given pins
*
* @param pins
* @returns a [group](https://connective.dev/docs/group) of the given pins. If any `PartialFlow`
* was among the given pins, its exit pins added to the group.
*
*/
serialTo(...pins) {
return this.exits.serialTo(...pins);
}
get observable() {
return this.exits.observable;
}
/**
*
* Subscribes to all of its exit pins. Returns a composite subscription of
* all created subscriptions.
*
*/
subscribe(_, __, ___) {
return this.exits.subscribe(_, __, ___);
}
}
class InlineFlow extends PartialFlow {
constructor(factory) {
super();
this.factory = factory;
let [entries, exits] = factory();
this.entries = (entries instanceof Group) ? entries : group(...entries);
this.exits = (exits instanceof Group) ? exits : group(...exits);
}
}
/**
*
* Creates a partial flow, using the given factory function. The factory function
* should return either a [`group`](https://connective.dev/docs/group) or an array
* of [pins](https://connective.dev/docs/pin) for inputs, and a group or an array of pins
* for outputs, in array format itself (first object being the inputs, second the outputs).
*
* @param factory
*
*/
export function partialFlow(factory) { return new InlineFlow(factory); }
export default partialFlow;
//# sourceMappingURL=partial-flow.js.map