/
from-event-source.ts
115 lines (105 loc) · 3.13 KB
/
from-event-source.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @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 function fromEventSource<T extends unknown>(
source: EventSource,
eventName = 'message',
openObserver?: Observer<Event>,
signal?: AbortSignal,
): Observable<T> {
return new Observable<T>((subscriber) => {
if (signal) {
signal.onabort = () => {
source.close();
!subscriber.closed && subscriber.complete();
};
}
/**
* @private
* @internal
* @param event
*/
function handleMessage(event: Event) {
if (!eventName || (eventName && eventName === event.type)) {
subscriber.next((event as MessageEvent).data);
}
}
/**
* @private
* @internal
* @param event
*/
/* istanbul ignore next */
function handleError(event: Event) {
subscriber.error(event as ErrorEvent);
}
/**
* @private
* @internal
* @param event
*/
/* istanbul ignore next */
function handleOpen(event: Event) {
openObserver?.next(event);
openObserver?.complete();
source.removeEventListener('open', handleOpen);
}
/* istanbul ignore next */
if (openObserver) {
source.addEventListener('open', handleOpen);
}
source.addEventListener(eventName, handleMessage);
source.addEventListener('error', handleError);
return () => {
source.removeEventListener(eventName, handleMessage);
source.removeEventListener('error', handleError);
source.removeEventListener('open', handleOpen);
source.close();
/* istanbul ignore next */
!subscriber.closed && subscriber.complete();
};
});
}