@trpc/server
Version:
133 lines (132 loc) • 7.63 kB
text/typescript
//#region src/observable/types.d.ts
/** What `subscribe()` returns: call `unsubscribe()` to stop. */
interface Unsubscribable {
/** Stops the subscription and runs its teardown. */
unsubscribe(): void;
}
/** Stops a subscription. */
type UnsubscribeFn = () => void;
interface Subscribable<TValue, TError> {
/** Starts the observable, sending what it emits to `observer`. */
subscribe(observer: Partial<Observer<TValue, TError>>): Unsubscribable;
}
/**
* A stream of values, which starts when you subscribe to it. Links pass
* results along as observables.
* @see https://trpc.io/docs/client/links#creating-a-custom-link
*/
interface Observable<TValue, TError> extends Subscribable<TValue, TError> {
/** Returns an observable with the operators applied, in order. */
pipe(): Observable<TValue, TError>;
pipe<TValue1, TError1>(op1: OperatorFunction<TValue, TError, TValue1, TError1>): Observable<TValue1, TError1>;
pipe<TValue1, TError1, TValue2, TError2>(op1: OperatorFunction<TValue, TError, TValue1, TError1>, op2: OperatorFunction<TValue1, TError1, TValue2, TError2>): Observable<TValue2, TError2>;
pipe<TValue1, TError1, TValue2, TError2, TValue3, TError3>(op1: OperatorFunction<TValue, TError, TValue1, TError1>, op2: OperatorFunction<TValue1, TError1, TValue2, TError2>, op3: OperatorFunction<TValue2, TError2, TValue3, TError3>): Observable<TValue2, TError2>;
pipe<TValue1, TError1, TValue2, TError2, TValue3, TError3, TValue4, TError4>(op1: OperatorFunction<TValue, TError, TValue1, TError1>, op2: OperatorFunction<TValue1, TError1, TValue2, TError2>, op3: OperatorFunction<TValue2, TError2, TValue3, TError3>, op4: OperatorFunction<TValue3, TError3, TValue4, TError4>): Observable<TValue2, TError2>;
pipe<TValue1, TError1, TValue2, TError2, TValue3, TError3, TValue4, TError4, TValue5, TError5>(op1: OperatorFunction<TValue, TError, TValue1, TError1>, op2: OperatorFunction<TValue1, TError1, TValue2, TError2>, op3: OperatorFunction<TValue2, TError2, TValue3, TError3>, op4: OperatorFunction<TValue3, TError3, TValue4, TError4>, op5: OperatorFunction<TValue4, TError4, TValue5, TError5>): Observable<TValue2, TError2>;
}
/** Gets an observable's values, its error, and its end. */
interface Observer<TValue, TError> {
/** Gets each value. */
next: (value: TValue) => void;
/** Gets the error the observable ends with. */
error: (err: TError) => void;
/** Called when the observable ends without an error. */
complete: () => void;
}
/** What the function passed to `observable()` returns to clean up on unsubscribe. */
type TeardownLogic = Unsubscribable | UnsubscribeFn | void;
/** A function of one argument. */
type UnaryFunction<TSource, TReturn> = (source: TSource) => TReturn;
/** Turns one observable into another, for `pipe()`. */
type OperatorFunction<TValueBefore, TErrorBefore, TValueAfter, TErrorAfter> = UnaryFunction<Subscribable<TValueBefore, TErrorBefore>, Subscribable<TValueAfter, TErrorAfter>>;
/** An operator that keeps the value and error types. */
type MonoTypeOperatorFunction<TValue, TError> = OperatorFunction<TValue, TError, TValue, TError>;
//#endregion
//#region src/observable/observable.d.ts
/**
* The type of the values an observable emits.
* @public
*/
type inferObservableValue<TObservable> = TObservable extends Observable<infer TValue, unknown> ? TValue : never;
/**
* Whether a value is an observable: an object with `subscribe`.
* @public
*/
declare function isObservable(x: unknown): x is Observable<unknown, unknown>;
/**
* Creates an observable. `subscribe` runs for each subscriber, and can
* return a function that cleans up when it unsubscribes.
* @example
* ```ts
* const ticks = observable<number>((observer) => {
* const timer = setInterval(() => observer.next(Date.now()), 1000);
* return () => clearInterval(timer);
* });
* ```
* @see https://trpc.io/docs/client/links#creating-a-custom-link
* @public
*/
declare function observable<TValue, TError = unknown>(subscribe: (observer: Observer<TValue, TError>) => TeardownLogic): Observable<TValue, TError>;
/**
* Resolves with an observable's first value. Rejects if it errors or
* completes first. It doesn't unsubscribe after the first value.
* @internal
*/
declare function observableToPromise<TValue>(observable: Observable<TValue, unknown>): Promise<TValue>;
/**
* Turns an observable into an async iterable, like Node's `events.on()`.
* Values wait in a queue until `next()` reads them. An error rejects the
* `next()` after the queued values, once. Completion, `return()` and aborting
* `signal` unsubscribe and end every pending `next()` with `{ done: true }`;
* anything the observable sends after that is ignored.
*
* If unsubscribing throws, `return()` rejects with the error, and completion
* or an error from the observable rethrows it. On abort, it rejects the
* `next()` after the queued values instead, as an error would.
* @internal
*/
declare function observableToAsyncIterable<TValue>(observable: Observable<TValue, unknown>, signal: AbortSignal): AsyncIterableIterator<TValue>;
//#endregion
//#region src/observable/operators.d.ts
/** Maps each value with `project`. */
declare function map<TValueBefore, TError, TValueAfter>(project: (value: TValueBefore, index: number) => TValueAfter): OperatorFunction<TValueBefore, TError, TValueAfter, TError>;
interface ShareConfig {}
/**
* Shares one subscription to the source between all subscribers. It
* starts with the first subscriber and ends when the last unsubscribes.
*/
declare function share<TValue, TError>(_opts?: ShareConfig): MonoTypeOperatorFunction<TValue, TError>;
/** Calls `observer` with each value, error and completion, and passes them on unchanged. */
declare function tap<TValue, TError>(observer: Partial<Observer<TValue, TError>>): MonoTypeOperatorFunction<TValue, TError>;
/** Skips values equal to the one before, by `compare` (`===` by default). */
declare function distinctUntilChanged<TValue, TError>(compare?: (a: TValue, b: TValue) => boolean): MonoTypeOperatorFunction<TValue, TError>;
/**
* Skips values equal to the one before, comparing objects and arrays by
* their own enumerable keys. Objects without such keys, like two `Date`s,
* always count as equal.
*/
declare function distinctUntilDeepChanged<TValue, TError>(): MonoTypeOperatorFunction<TValue, TError>;
//#endregion
//#region src/observable/behaviorSubject.d.ts
/** An observable that holds a current value. */
interface BehaviorSubject<TValue> extends Observable<TValue, never> {
observable: Observable<TValue, never>;
/**
* Sets the value and sends it to subscribers, if it changed. A subscriber
* that throws doesn't stop the others; its error is rethrown asynchronously.
*/
next: (value: TValue) => void;
/** The current value. */
get: () => TValue;
}
/** A `BehaviorSubject` that can't be set. */
interface ReadonlyBehaviorSubject<TValue> extends Omit<BehaviorSubject<TValue>, 'next'> {}
/**
* Creates an observable that holds a current value. New subscribers get
* it right away, unless it's `undefined`.
* @internal
*/
declare function behaviorSubject<TValue>(initialValue: TValue): BehaviorSubject<TValue>;
//#endregion
export { Unsubscribable as _, distinctUntilDeepChanged as a, tap as c, observable as d, observableToAsyncIterable as f, TeardownLogic as g, Observer as h, distinctUntilChanged as i, inferObservableValue as l, Observable as m, ReadonlyBehaviorSubject as n, map as o, observableToPromise as p, behaviorSubject as r, share as s, BehaviorSubject as t, isObservable as u, UnsubscribeFn as v };
//# sourceMappingURL=index-CFQnkrGZ.d.mts.map