UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

74 lines 2.51 kB
import { retry, tap, share } from 'rxjs/operators'; import emission from '../shared/emission'; import { isEmissionError } from '../shared/errors/emission-error'; import pin from '../pin/pin'; import group from '../pin/group'; import source from '../pin/source'; import pipe from '../pin/pipe'; import { block } from '../pin/filter'; import { Agent } from './agent'; /** * * Represents [handle error](https://connective.dev/docs/handle-error) agents. * */ export class HandleError extends Agent { constructor() { super({ inputs: ['input'], outputs: ['output', 'error'], }); this._err = source(); this._gate = this.input.to(pipe(tap(null, error => { if (isEmissionError(error)) this._err.emit(emission(error, error.emission.context)); else this._err.send(error); }), retry(), share())); } createOutput(label) { this.checkOutput(label); if (label == 'error') return group(this._err, this._gate.to(block())).to(pin()); else return this._gate; } createEntries() { return [this.input]; } createExits() { return [this.output, this.error]; } clear() { this._err.clear(); return super.clear(); } /** * * Shortcut for `.in('input')`, the input pin receiving values. * [Read this](https://connective.dev/docs/handle-error#signature) for more details. * */ get input() { return this.in('input'); } /** * * Shortcut for `.out('output')`, which will emit error-free values. * [Read this](https://connective.dev/docs/handle-error#signature) for more details. * */ get output() { return this.out('output'); } /** * * Shortcut for `.out('error')`, which will emit errors. * [Read this](https://connective.dev/docs/handle-error#signature) for more details. * */ get error() { return this.out('error'); } } /** * * Creates a [handle error](https://connective.dev/docs/handle-error) agent. * Handle error agents will pass on incoming values, but also will catch errors * occuring upstream and pass them along, stopping the flow from closing in resposne to such errors. * [Checkout the docs](https://connective.dev/docs/handle-error) for examples and further information. * */ export function handleError() { return new HandleError(); } export default handleError; //# sourceMappingURL=handle-error.js.map