Skip to content

deeplay-io/abort-controller-x-rxjs

Repository files navigation

abort-controller-x-rxjs npm version

Abortable helpers for RxJS.

This is a companion package of abort-controller-x.

Installation

yarn add abort-controller-x-rxjs

API

defer

function defer<R extends ObservableInput<any> | void>(
  observableFactory: (signal: AbortSignal) => R,
): Observable<ObservedValueOf<R>>;

Like original defer from RxJS, but aborts passed function when unsubscribed.

firstValueFrom

function firstValueFrom<T, D>(
  signal: AbortSignal,
  source: Observable<T>,
  config: FirstValueFromConfig<D>,
): Promise<T | D>;
function firstValueFrom<T>(
  signal: AbortSignal,
  source: Observable<T>,
): Promise<T>;

interface FirstValueFromConfig<T> {
  defaultValue: T;
}

Like original firstValueFrom from RxJS, but accepts AbortSignal. When that signal is aborted, unsubscribes from the observable and throws AbortError.

lastValueFrom

function lastValueFrom<T, D>(
  signal: AbortSignal,
  source: Observable<T>,
  config: LastValueFromConfig<D>,
): Promise<T | D>;
function lastValueFrom<T>(
  signal: AbortSignal,
  source: Observable<T>,
): Promise<T>;

interface LastValueFromConfig<T> {
  defaultValue: T;
}

Like original lastValueFrom from RxJS, but accepts AbortSignal. When that signal is aborted, unsubscribes from the observable and throws AbortError.

fromAsyncGenerator

function fromAsyncGenerator<T>(
  fn: (signal: AbortSignal) => AsyncIterable<T>,
): Observable<T>;

Turns abortable async generator to observable.