Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/observables. A spec-faithful yet ergonomic TC39-inspired Observable library with detailed ty
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341// deno-lint-ignore-file no-explicit-any/** * Helper types describe how pipeline stages connect, what kinds of foreign * inputs can be adapted, and which lifecycle hooks custom operators can use. * * They are most useful when you are authoring operators or interop helpers * rather than just consuming built-in operators. * * @module */import type { ObservableError } from "../error.ts";import type { Observable } from "../observable.ts";import type { SpecObservable } from "../_spec.ts";
/** * Stream-level operator function used internally by `pipe()`. */export type Operator<In, Out> = ( stream: ReadableStream<In>,) => ReadableStream<Out>;
/** * Removes `ObservableError` from a type so operators can describe * error-filtered output channels. */export type ExcludeError<T> = Exclude<T, ObservableError>;
/** * Represents an operator slot in a pipeline where the previous operator may * or may not have filtered `ObservableError` values out of the stream. */export type OperatorItem<T, R> = Operator<T, R> | Operator<T, ExcludeError<R>>;
/** * Infers the item type contributed by either an Observable source or an * operator in a pipe chain. */export type InferSourceType<TSource extends unknown> = TSource extends Observable<unknown> ? InferObservableType<TSource> : TSource extends OperatorItem<unknown, unknown> ? InferOperatorItemOutputType<TSource> : TSource;
/** * Extracts the chunk type emitted by an operator. */export type InferOperatorItemOutputType< TSource extends OperatorItem<any, any>,> = ReturnType<TSource> extends ReadableStream<infer T> ? T : never;
/** * Extracts the value type emitted by an Observable. */export type InferObservableType<TSource extends Observable<unknown>> = TSource extends Observable<infer R> ? R : any;
/** * Returns the first item in a non-empty tuple. */export type FirstTupleItem<TTuple extends readonly [unknown, ...unknown[]]> = TTuple[0];
/** * Returns the last item in any tuple shape. */export type GenericLastTupleItem<T extends readonly any[]> = T extends [...infer _, infer L] ? L : never;
/** * Returns the last tuple item only when it is a valid operator item. */export type LastTupleItem<T extends readonly unknown[]> = GenericLastTupleItem<T> extends OperatorItem<any, any> ? GenericLastTupleItem<T> : never;
/** * Computes the Observable type returned by a `pipe()` call from the source and * its final operator. */export type ObservableWithPipe< TPipe extends readonly [Observable<unknown>, ...OperatorItem<any, unknown>[]],> = Observable<InferOperatorItemOutputType<LastTupleItem<TPipe>>>;
/** * Error-handling policy for custom operators. * * - `pass-through`: wrap failures as `ObservableError` values and let later * stages decide what to do * - `ignore`: drop wrapped failures from the output * - `throw`: terminate the stream on failure * - `manual`: handle success and failure paths yourself inside the transform */export type OperatorErrorMode = "ignore" | "pass-through" | "throw" | "manual";
/** * Fields shared by all operator-builder option shapes. */export interface BaseTransformOptions { /** * Optional name for the operator (used in error reporting) */ name?: string;}
/** * Structural readable/writable pair used by platform transforms such as * `CompressionStream`. */export interface StreamPair<TIn, TOut> { /** Readable side exposed by the transform-like object. */ readable: ReadableStream<TOut>; /** Writable side exposed by the transform-like object. */ writable: WritableStream<TIn>;}
/** * Minimal subscribable shape used by foreign Observable implementations. */export interface SubscribableLike<T> { /** Subscribe to values from the foreign Observable-like object. */ subscribe(observer: { next?(value: T): void; error?(error: unknown): void; complete?(): void; }): { unsubscribe?(): void } | void;}
/** * Primary conversion shapes mirrored by helper utilities and interop types. * * This alias is intentionally descriptive rather than authoritative. * `Observable.from()` keeps its own explicit signature so the runtime entry * points `from()` and `of()` can stay distinct, but the shapes listed here * mirror the main non-subscribable inputs that `Observable.from()` accepts. */export type ObservableInputLike<T> = | SpecObservable<T> | AsyncIterable<T> | Iterable<T> | PromiseLike<T> | ArrayLike<T>;
/** * Wider Observable-like values accepted by interop helpers that adapt foreign * operator ecosystems. * * This is intentionally wider than `ObservableInputLike<T>`. The base alias * covers the primary `Observable.from()`-style conversion set, while interop * helpers may also need to consume direct subscribables returned by third-party * libraries such as RxJS. */export type ObservableInteropInputLike<T> = | ObservableInputLike<T> | SubscribableLike<T>;
/** * Foreign operator shape used by libraries that transform one Observable-like * source into another Observable-like result. */export type ObservableOperatorInterop< TIn, TOut, TSource = Observable<TIn>,> = ( source: TSource,) => ObservableInteropInputLike<TOut>;
/** * Configuration for adapting foreign Observable-style operators. * * Some libraries, such as RxJS, require their own Observable class on the * input side even when the returned operator shape is still `(source) => * output`. `sourceAdapter` lets callers convert this library's Observable into * that foreign source type before the operator runs. */export interface ObservableOperatorInteropOptions<TIn, TSource = Observable<TIn>> { /** * Converts this library's Observable into the source type expected by the * foreign operator. */ sourceAdapter?: (source: Observable<TIn>) => TSource;
/** * Context label used when the foreign output reports an error. */ errorContext?: string;}
// ========================================// 2. CREATEOPERATOR INTERFACES// ========================================
/** * Options for adapting an existing `TransformStream` factory into an operator. */export interface TransformStreamOptions<T, R> extends BaseTransformOptions { /** * An existing TransformStream to use for transformation */ stream: (opts: TransformStreamOptions<T, R>) => TransformStream<T, R>;}
/** * Options for authoring a stateless transform callback. */export interface TransformFunctionOptions<T, R> extends BaseTransformOptions { /** * Error-handling policy for this operator. * * `pass-through` is the default because it keeps recovery explicit in later * stages instead of silently dropping failures. * * @default "pass-through" */ errorMode?: OperatorErrorMode;
/** * Function to transform each chunk * @param chunk - The input chunk * @param controller - The TransformStreamDefaultController * @returns The transformed chunk(s) or undefined to filter out the chunk */ transform: ( chunk: T, controller: TransformStreamDefaultController<R>, ) => R | undefined | void | null | Promise<R | undefined | void | null>;
/** * Function called when the stream is about to close * Can enqueue final chunks before closing * @param controller - The TransformStreamDefaultController */ flush?: ( controller: TransformStreamDefaultController<R>, ) => void | Promise<void>;
/** * Called when the stream starts, before processing any chunks * @param controller - The TransformStreamDefaultController */ start?: ( controller: TransformStreamDefaultController<R>, ) => void | Promise<void>;
/** * Called when the stream is cancelled before natural completion */ cancel?: (reason?: unknown) => void | Promise<void>;}
/** * All accepted option shapes for `createOperator()`. */export type CreateOperatorOptions<T, R> = | TransformStreamOptions<T, R> | TransformFunctionOptions<T, R>;
// ========================================// 3. CREATESTATEFULOPERATOR INTERFACES// ========================================
/** * Options for authoring a stateful operator. */export interface StatefulTransformFunctionOptions<T, R, S> extends BaseTransformOptions { /** * Error-handling policy for this operator. * * @default "pass-through" */ errorMode?: OperatorErrorMode;
/** * Function to create the initial state * @returns The initial state */ createState: () => S;
/** * Function to transform each chunk, with access to the current state * @param chunk - The input chunk * @param state - The current state (can be modified) * @param controller - The TransformStreamDefaultController */ transform: ( chunk: T, state: S, controller: TransformStreamDefaultController<R>, ) => void | Promise<void>;
/** * Function called when the stream is about to close * Can enqueue final chunks based on the state * @param state - The final state * @param controller - The TransformStreamDefaultController */ flush?: ( state: S, controller: TransformStreamDefaultController<R>, ) => void | Promise<void>;
/** * Called when the stream starts, before processing any chunks * @param state - The initial state * @param controller - The TransformStreamDefaultController */ start?: ( state: S, controller: TransformStreamDefaultController<R>, ) => void | Promise<void>;
/** * Called when the stream is cancelled before natural completion * @param state - The current state */ cancel?: ( state: S, reason?: unknown, ) => void | Promise<void>;}
/** * Shared metadata passed into lifecycle helpers that wrap custom transforms. */export interface TransformHandlerContext<S extends unknown = undefined> { /** * Human-readable operator name used in wrapped error messages. */ operatorName?: string; /** * Indicates that the lifecycle handler should pass shared state through. */ isStateful?: boolean; /** * Shared operator state for stateful transforms. */ state?: S;}