Mirror: 馃帺 A tiny but capable push & pull stream library for TypeScript and Flow
1/** A talkback signal is used to tell a [Source] that either the [Sink] is ready for new values or that the stream should be cancelled */ 2export const enum TalkbackKind { 3 Pull = 0, 4 Close = 1, 5} 6 7/** A talkback callback is sent to the sink with the [Start] signal to communicate signals back to the source. */ 8export type TalkbackFn = (signal: TalkbackKind) => void; 9export type TeardownFn = () => void; 10 11export const enum SignalKind { 12 Start = 0, 13 Push = 1, 14 End = 0, 15} 16 17export interface Tag<T> { 18 tag: T; 19} 20 21/** The start [Signal] is the first signal and carries a callback (talkback) so the sink can send signals to the source */ 22export type Start<_T> = Tag<SignalKind.Start> & [TalkbackFn]; 23/** The Push [Signal] carries new values to the sink, like in an event emitter */ 24export type Push<T> = Tag<SignalKind.Push> & [T]; 25 26/** A signal that communicates new events to a sink. */ 27export type Signal<T> = Start<T> | Push<T> | SignalKind.End; 28 29/** A sink accepts new values from a [Source], like [Push], [Start], and an end signal. The [Start] is used to receive a callback to send talkback signals back to the source. */ 30export type Sink<T> = (signal: Signal<T>) => void; 31/** A source is a function that accepts a [Sink] and then starts sending [Signal]s to it. */ 32export type Source<T> = (sink: Sink<T>) => void; 33/** An operator transforms a [Source] and returns a new [Source], potentially with different timings or output types. */ 34export type Operator<In, Out> = (a: Source<In>) => Source<Out>; 35 36/** Extracts the type of a given Source */ 37export type TypeOfSource<T> = T extends Source<infer U> ? U : never; 38 39export interface Subscription { 40 unsubscribe(): void; 41} 42 43export interface Observer<T> { 44 next(value: T): void; 45 complete(): void; 46} 47 48export interface Subject<T> extends Observer<T> { 49 source: Source<T>; 50}