Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
};
});
}