@rxjs-ninja/rxjs-utility
Version:
Useful utilities for RxJS
48 lines (47 loc) • 1.82 kB
TypeScript
/**
* @packageDocumentation
* @module Utility
*/
import { Observable, Observer } from 'rxjs';
/**
* Returns an Observable that emits values from an EventSource subscribing to the the passed
* `eventName` stream. Takes an optional Observer (e.g. Subject) to emit when the stream opens
*
* @category Streams
*
* @see {@link https://codesandbox.io/s/rxjs-ninja-eventsource-example-w7v4j|RxJS Event Source Example}
*
* @typeParam T The type of the value in the message `data` property
*
* @param source The event source to subscribe to
* @param eventName The name of the event to listen to, by default this is `message`
* @param openObserver Optional observer that is emitted when the event source is opened
* @param signal Optional signal to end the event source
*
* @example
* Subscribe to an EventSource, listen for it opening and provide a stop signal
* ```ts
* // The event source emits a time every 1 minute
* const eventSource = new EventSource('/event-stream');
* const stopSource = new AbortController();
* const isOpen$ = new Subject<Event>();
*
* function endSource() {
* stopSource.abort();
* }
*
* fromEventSource<string>(eventSource, 'message', isOpen$, stopSource.signal).pipe(
* tap(value => {
* const parsed = JSON.parse(value);
* outputSpan.innerHTML = `The time is ${parsed.message}`
* }),
* finalize(() => {
* outputSpan.innerHTML = `EventSource closed`
* })
* ).subscribe();
* ```
* Output: `'The time is 12:01', 'The time is 12:02', ....`
*
* @returns Observable that emits the `data` value from an EventSource message
*/
export declare function fromEventSource<T extends unknown>(source: EventSource, eventName?: string, openObserver?: Observer<Event>, signal?: AbortSignal): Observable<T>;