UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

98 lines 2.46 kB
import { Subject } from 'rxjs'; import emission from '../shared/emission'; import { Connectible } from './connectible'; /** * * Represents [source](https://connective.dev/docs/source) pins. * */ export class Source extends Connectible { constructor(_subject = new Subject()) { super(); this._subject = _subject; } /** * * This source will send given value, perhaps with given context. * Will create a new [emission](https://connective.dev/docs/emission) object. * * @param value the value to send * @param context the emission context * */ send(value, context) { this.emit(emission(value, context)); } /** * * Will emit the given emission object. * * @param emission * */ emit(emission) { this._subject.next(emission); } /** * * @note this sends a complete notification through-out the flow. * Pins that are merely reliant on this source will also be unusable * afterwards. * */ clear() { this._subject.complete(); this._subject = new Subject(); return super.clear(); } /** * * Determines if any pin is connected to this pin. * */ isConnected() { return this.tracking || super.isConnected(); } /** * * Resolves the underlying observable of this pin by subscribing the * subject of this pin to all inbound pins. * * @param inbound * */ resolve(inbound) { inbound.forEach(pin => { this.track(pin.observable.subscribe(this._subject)); }); inbound.length = 0; return this._subject; } /** * * Determines whether this pin is locked. A source is never locked. * */ isLocked() { return false; } /** * * Determines whether should resolve the underlying observable. * * @param inbound * @param observable * */ shouldResolve(inbound, observable) { return inbound.length > 0 || !observable; } } /** * * Creates a [source](https://connective.dev/docs/source) pin. * A source pin can be used as the starting point of a reactive flow. * [Checkout the docs](https://connective.dev/docs/source) for examples and further information. * */ export function source(sub) { return new Source(sub); } export default source; //# sourceMappingURL=source.js.map