Skip to content

Commit

Permalink
fix(fromFetch): don't abort if fetch resolves (#4742)
Browse files Browse the repository at this point in the history
* test(fetch): test expectations after complete

* fix(fromFetch): don't abort if fetch resolves

Closes #4739
  • Loading branch information
cartant authored and benlesh committed May 2, 2019
1 parent cd5895d commit 1dc09e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 12 additions & 7 deletions spec/observables/dom/fetch-spec.ts
Expand Up @@ -115,14 +115,19 @@ describe('fromFetch', () => {
expect(response).to.equal(OK_RESPONSE);
},
error: done,
complete: done,
complete: () => {
// Wait until the complete and the subsequent unsubscribe are finished
// before testing these expectations:
setTimeout(() => {
expect(MockAbortController.created).to.equal(1);
expect(mockFetch.calls.length).to.equal(1);
expect(mockFetch.calls[0].input).to.equal('/foo');
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
done();
}, 0);
}
});

expect(MockAbortController.created).to.equal(1);
expect(mockFetch.calls.length).to.equal(1);
expect(mockFetch.calls[0].input).to.equal('/foo');
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
});

it('should handle Response that is not `ok`', done => {
Expand Down
7 changes: 6 additions & 1 deletion src/internal/observable/dom/fetch.ts
Expand Up @@ -55,6 +55,7 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab
const controller = new AbortController();
const signal = controller.signal;
let outerSignalHandler: () => void;
let abortable = true;
let unsubscribed = false;

if (init) {
Expand All @@ -73,9 +74,11 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab
}

fetch(input, init).then(response => {
abortable = false;
subscriber.next(response);
subscriber.complete();
}).catch(err => {
abortable = false;
if (!unsubscribed) {
// Only forward the error if it wasn't an abort.
subscriber.error(err);
Expand All @@ -84,7 +87,9 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab

return () => {
unsubscribed = true;
controller.abort();
if (abortable) {
controller.abort();
}
};
});
}

0 comments on commit 1dc09e9

Please sign in to comment.