Skip to content

Commit

Permalink
refactor: use init and selector intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Apr 19, 2020
1 parent 26ced5d commit c5b8c17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 49 deletions.
35 changes: 5 additions & 30 deletions spec/observables/dom/fetch-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,36 +270,9 @@ describe('fromFetch', () => {
...OK_RESPONSE,
text: () => Promise.resolve('bar')
};
const fetch$ = fromFetch('/foo', undefined, response => response.text());
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);

fetch$.subscribe({
next: text => {
expect(text).to.equal('bar');
},
error: 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);
}
const fetch$ = fromFetch('/foo', {
selector: response => response.text()
});
});

it('should support a selector without an init argument', done => {
mockFetch.respondWith = {
...OK_RESPONSE,
text: () => Promise.resolve('bar')
};
const fetch$ = fromFetch('/foo', response => response.text());
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);

Expand Down Expand Up @@ -328,7 +301,9 @@ describe('fromFetch', () => {
...OK_RESPONSE,
text: () => Promise.resolve('bar')
};
const fetch$ = fromFetch('/foo', undefined, response => response.text());
const fetch$ = fromFetch('/foo', {
selector: response => response.text()
});
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);
const subscription = fetch$.subscribe();
Expand Down
29 changes: 10 additions & 19 deletions src/internal/observable/dom/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import { Subscription } from '../../Subscription';
import { from } from '../../observable/from';
import { ObservableInput } from '../../types';

export function fromFetch(
input: string | Request,
init?: RequestInit
): Observable<Response>;

export function fromFetch<T>(
input: string | Request,
selector: (response: Response) => ObservableInput<T>
init: RequestInit & {
selector: (response: Response) => ObservableInput<T>
}
): Observable<T>;

export function fromFetch<T>(
export function fromFetch(
input: string | Request,
init: RequestInit | undefined,
selector: (response: Response) => ObservableInput<T>
): Observable<T>;
init?: RequestInit
): Observable<Response>;

/**
* Uses [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to
Expand Down Expand Up @@ -71,16 +67,11 @@ export function fromFetch<T>(
*/
export function fromFetch<T>(
input: string | Request,
initOrSelector?: RequestInit | ((response: Response) => ObservableInput<T>),
selector?: (response: Response) => ObservableInput<T>
initWithSelector: RequestInit & {
selector?: (response: Response) => ObservableInput<T>
} = {}
): Observable<Response | T> {
let init: RequestInit | undefined;
if (typeof initOrSelector === 'function') {
init = undefined;
selector = initOrSelector;
} else {
init = initOrSelector;
}
const { selector, ...init } = initWithSelector;
return new Observable<Response | T>(subscriber => {
const controller = new AbortController();
const signal = controller.signal;
Expand Down

0 comments on commit c5b8c17

Please sign in to comment.