Skip to main

subscribe

subscribe

Signature - source.ts#L605

function subscribe<T>(
    sink?: Sink<T> | ((event: Event<T>) => void),
): (source: Source<T>) => Sink<T>

Higher order function which takes a sink, and returns another function which receives a source that will be subscribed to using the given sink. This is useful, for example, at the end of pipe calls in order to subscribe to the transformed source.

Parameters

ParameterTypeDescription
sink
Sink<T> | ((event: Event<T>) => void)

The sink to be given to the received source.

Returns

TypeDescription
(source: Source<T>) => Sink<T>

The higher order function which takes a source to subscribe to.

Example Usage

import { DogPictures, myGetDogPictures } from './myApi.ts';
import { MyRequestTimeoutError } from './myRequestTimeoutError.ts';
import { myReportError } from './myReportError.ts'
import { myUpdateViewWithDogs } from './myUpdateViewWithDogs.ts'

const sink = Sink<DogPictures>(event => {
    if (event.type === ThrowType) {
        myReportError(event.error)
    } else if (event.type === EndType) {
        return;
    }
    myUpdateViewWithDogs(event.value)
});

pipe(
    myGetDogPictures(...),
    timeoutMs(5000, throwError(() => new MyRequestTimeoutError())),
    retry(3),
    subscribe(sink)
);

See Also