1 | import * as tslib_1 from "tslib";
|
---|
2 | import { Observable } from '../../Observable';
|
---|
3 | import { Subscription } from '../../Subscription';
|
---|
4 | import { from } from '../../observable/from';
|
---|
5 | export function fromFetch(input, initWithSelector = {}) {
|
---|
6 | const { selector } = initWithSelector, init = tslib_1.__rest(initWithSelector, ["selector"]);
|
---|
7 | return new Observable(subscriber => {
|
---|
8 | const controller = new AbortController();
|
---|
9 | const signal = controller.signal;
|
---|
10 | let abortable = true;
|
---|
11 | let unsubscribed = false;
|
---|
12 | const subscription = new Subscription();
|
---|
13 | subscription.add(() => {
|
---|
14 | unsubscribed = true;
|
---|
15 | if (abortable) {
|
---|
16 | controller.abort();
|
---|
17 | }
|
---|
18 | });
|
---|
19 | let perSubscriberInit;
|
---|
20 | if (init) {
|
---|
21 | if (init.signal) {
|
---|
22 | if (init.signal.aborted) {
|
---|
23 | controller.abort();
|
---|
24 | }
|
---|
25 | else {
|
---|
26 | const outerSignal = init.signal;
|
---|
27 | const outerSignalHandler = () => {
|
---|
28 | if (!signal.aborted) {
|
---|
29 | controller.abort();
|
---|
30 | }
|
---|
31 | };
|
---|
32 | outerSignal.addEventListener('abort', outerSignalHandler);
|
---|
33 | subscription.add(() => outerSignal.removeEventListener('abort', outerSignalHandler));
|
---|
34 | }
|
---|
35 | }
|
---|
36 | perSubscriberInit = Object.assign({}, init, { signal });
|
---|
37 | }
|
---|
38 | else {
|
---|
39 | perSubscriberInit = { signal };
|
---|
40 | }
|
---|
41 | fetch(input, perSubscriberInit).then(response => {
|
---|
42 | if (selector) {
|
---|
43 | subscription.add(from(selector(response)).subscribe(value => subscriber.next(value), err => {
|
---|
44 | abortable = false;
|
---|
45 | if (!unsubscribed) {
|
---|
46 | subscriber.error(err);
|
---|
47 | }
|
---|
48 | }, () => {
|
---|
49 | abortable = false;
|
---|
50 | subscriber.complete();
|
---|
51 | }));
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | abortable = false;
|
---|
55 | subscriber.next(response);
|
---|
56 | subscriber.complete();
|
---|
57 | }
|
---|
58 | }).catch(err => {
|
---|
59 | abortable = false;
|
---|
60 | if (!unsubscribed) {
|
---|
61 | subscriber.error(err);
|
---|
62 | }
|
---|
63 | });
|
---|
64 | return subscription;
|
---|
65 | });
|
---|
66 | }
|
---|
67 | //# sourceMappingURL=fetch.js.map |
---|