Skip to content

Commit

Permalink
Merge ed8d771 into cd5895d
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Apr 26, 2019
2 parents cd5895d + ed8d771 commit c729346
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 c729346

Please sign in to comment.