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

[BUGFIX BETA] make fetch function JIT (#6430) #6434

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions packages/adapter/addon/-private/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import require, { has } from 'require';

type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;

let _fetch: (() => FetchFunction) | null = null;
let _fetch: () => FetchFunction | null = null;

if (has('fetch')) {
// use `fetch` module by default, this is commonly provided by ember-fetch
let foundFetch = require('fetch').default;
_fetch = () => foundFetch;
} else if (typeof fetch === 'function') {
// fallback to using global fetch
_fetch = () => fetch;
} else {
throw new Error(
'cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?'
);
}
export default function getFetchFunction(): FetchFunction {
if (_fetch !== null) {
return _fetch();
}

export default _fetch;
if (has('fetch')) {
// use `fetch` module by default, this is commonly provided by ember-fetch
let fetchFn = require('fetch').default;
_fetch = () => fetchFn;
} else if (typeof fetch === 'function') {
// fallback to using global fetch
_fetch = () => fetch;
} else {
throw new Error(
'cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?'
);
}
return _fetch();
}