Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | import { EmptyError } from '../util/EmptyError';
|
---|
2 | import { Subscriber } from '../Subscriber';
|
---|
3 | export function throwIfEmpty(errorFactory = defaultErrorFactory) {
|
---|
4 | return (source) => {
|
---|
5 | return source.lift(new ThrowIfEmptyOperator(errorFactory));
|
---|
6 | };
|
---|
7 | }
|
---|
8 | class ThrowIfEmptyOperator {
|
---|
9 | constructor(errorFactory) {
|
---|
10 | this.errorFactory = errorFactory;
|
---|
11 | }
|
---|
12 | call(subscriber, source) {
|
---|
13 | return source.subscribe(new ThrowIfEmptySubscriber(subscriber, this.errorFactory));
|
---|
14 | }
|
---|
15 | }
|
---|
16 | class ThrowIfEmptySubscriber extends Subscriber {
|
---|
17 | constructor(destination, errorFactory) {
|
---|
18 | super(destination);
|
---|
19 | this.errorFactory = errorFactory;
|
---|
20 | this.hasValue = false;
|
---|
21 | }
|
---|
22 | _next(value) {
|
---|
23 | this.hasValue = true;
|
---|
24 | this.destination.next(value);
|
---|
25 | }
|
---|
26 | _complete() {
|
---|
27 | if (!this.hasValue) {
|
---|
28 | let err;
|
---|
29 | try {
|
---|
30 | err = this.errorFactory();
|
---|
31 | }
|
---|
32 | catch (e) {
|
---|
33 | err = e;
|
---|
34 | }
|
---|
35 | this.destination.error(err);
|
---|
36 | }
|
---|
37 | else {
|
---|
38 | return this.destination.complete();
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 | function defaultErrorFactory() {
|
---|
43 | return new EmptyError();
|
---|
44 | }
|
---|
45 | //# sourceMappingURL=throwIfEmpty.js.map |
---|
Note:
See
TracBrowser
for help on using the repository browser.