-
Notifications
You must be signed in to change notification settings - Fork 2
/
catchError.ts
36 lines (31 loc) · 882 Bytes
/
catchError.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
import { Source, Operator, Observer } from '../index';
import { createSource } from '../sources';
import subscribe from '../utils/subscribe';
/**
* Catches errors on the source to be handled by returning a new source
*
* @param handler - error handler
* @return callbag operator
*
* @public
*/
function catchError<I, O>(handler: (error: any) => Source<O>): Operator<I, I | O> {
return source => {
return createSource((next, complete, error) => {
let unsubscribeHandler = () => {};
const observer: Observer<I> = {
next,
complete,
error: err => {
unsubscribeHandler = subscribe(handler(err))({ next, error, complete });
},
};
const unsubscribe = subscribe(source)(observer);
return () => {
unsubscribeHandler();
unsubscribe();
};
});
};
}
export default catchError;