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

🐛 Prevent closure over fetch #1267

Merged
merged 4 commits into from
Jul 9, 2021

Conversation

Shrugsy
Copy link
Collaborator

@Shrugsy Shrugsy commented Jul 8, 2021

Defaulting fetchFn = fetch keeps it around in a closure.
This change avoids that, allowing window.fetch
to be mocked/monkey-patched more easily

Also updates the lockfile

Defaulting `fetchFn = fetch` keeps it around in a closure.
This change avoids that, allowing `window.fetch`
to be mocked/monkey-patched more easily
@netlify
Copy link

netlify bot commented Jul 8, 2021

✔️ Deploy Preview for redux-starter-kit-docs ready!

🔨 Explore the source changes: 19a0738

🔍 Inspect the deploy log: https://app.netlify.com/sites/redux-starter-kit-docs/deploys/60e80719356b5700077789d9

😎 Browse the preview: https://deploy-preview-1267--redux-starter-kit-docs.netlify.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 8, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 19a0738:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration

* A mini-wrapper that passes arguments straight through to `window.fetch`.
* Avoids storing `window.fetch` in a closure, in order to permit mocking/monkey-patching.
*/
const defaultFetchFn = (...args: Parameters<typeof window.fetch>) => window.fetch(...args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for window here and warn on it? The docs already mention handling this in an SSR env, but it's the only feedback I have.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a good idea. I've put it immediately within the fetchBaseQuery creation call though, rather than when a fetch is attempted. Caught quicker, and spams less

Comment on lines 34 to 35
const defaultFetchFn = (...args: Parameters<typeof window.fetch>) =>
window.fetch(...args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const defaultFetchFn = (...args: Parameters<typeof window.fetch>) =>
window.fetch(...args)
const defaultFetchFn: typeof fetch = (...args) =>
fetch(...args)

Given that we had it without the window reference before, let's just keep it like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, changed as per the above, and tweaked the docstring slightly.

The reason I explicitly changed it previously was that I've experienced a setup where Sinon's useFakeTimers will mock window.setTimeout but not setTimeout. But given that I haven't found the exact reason for that, or how version specific or test setup specific the behaviour is, I can't assume that will occur elsewhere.

Co-authored-by: Lenz Weber <mail@lenzw.de>
@phryneas
Copy link
Member

phryneas commented Jul 9, 2021

I'll wait for the tests to pass and then get this in.

@phryneas phryneas changed the title 🐛 Prevent closure over window.fetch 🐛 Prevent closure over fetch Jul 9, 2021
@phryneas phryneas merged commit fb79627 into reduxjs:master Jul 9, 2021
@phryneas phryneas added this to the 1.6.1 milestone Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants