UNPKG

@tempest/core

Version:

The core of the Tempest Stream Library

72 lines 1.83 kB
import { append, remove, findIndex } from '../util/array'; function tryEvent(time, value, sink) { try { sink.event(time, value); } catch (e) { sink.error(time, e); } } function tryEnd(time, value, sink) { try { sink.end(time, value); } catch (e) { sink.error(time, e); } } export class None { event(t, x) { return void 0; } end(t, x) { return void 0; } error(t, x) { return void 0; } } const NONE = new None(); export function none() { return NONE; } function removeManyAt(i, sinks) { const updated = remove(i, sinks); // It's impossible to create a Many with 1 sink // so we can't end up with updated.length === 0 here return updated.length === 1 ? updated[0] : new Many(updated); } function removeMany(sink, many) { const { sinks } = many; const i = findIndex(sink, sinks); return i < 0 ? many : removeManyAt(i, sinks); } export function addSink(sink, sinks) { return sinks === NONE ? sink : sinks instanceof Many ? new Many(append(sink, sinks.sinks)) : new Many([sinks, sink]); } export function removeSink(sink, sinks) { return sinks === NONE || sink === sinks ? NONE : sinks instanceof Many ? removeMany(sink, sinks) : sinks; } export class Many { constructor(sinks) { this.sinks = sinks; } event(t, x) { const s = this.sinks; for (let i = 0; i < s.length; ++i) { tryEvent(t, x, s[i]); } } end(t, x) { const s = this.sinks; for (let i = 0; i < s.length; ++i) { tryEnd(t, x, s[i]); } } error(t, x) { const s = this.sinks; for (let i = 0; i < s.length; ++i) { s[i].error(t, x); } } } //# sourceMappingURL=sink.js.map