-
Notifications
You must be signed in to change notification settings - Fork 2
/
throwIfEmpty.ts
38 lines (34 loc) · 881 Bytes
/
throwIfEmpty.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
import { Operator, Observer } from '../index';
import { createSource } from '../sources';
import subscribe from '../utils/subscribe';
/**
* Emit an error if the source completes without emitting a value
*
* @param errorFactory - factory function to produce the error to be thrown
* @return callbag operator
*
* @public
*/
function throwIfEmpty<I>(errorFactory: () => any): Operator<I, I> {
return source => {
return createSource((next, complete, error) => {
let empty = true;
const observer: Observer<I> = {
next: value => {
empty = false;
next(value);
},
error,
complete: () => {
if (empty) {
error(errorFactory());
} else {
complete();
}
},
};
return subscribe(source)(observer);
});
};
}
export default throwIfEmpty;