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

some suggestions to unify code #44

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

wingleung
Copy link
Contributor

kicking off #37

wondering if we could change the api response to return a supported property instead of the existing unsupported property. If we change it to supported there is no double negatives confusion.

hardware-concurrency/index.js Outdated Show resolved Hide resolved
hardware-concurrency/index.js Outdated Show resolved Hide resolved
memory/index.js Outdated Show resolved Hide resolved
memory/index.js Show resolved Hide resolved
memory/index.js Show resolved Hide resolved
network/index.js Outdated
unsupported = true;
}
const useNetworkStatus = (initialEffectiveConnectionType = null) => {
const supported = (typeof navigator !== 'undefined' && 'connection' in navigator && 'effectiveType' in navigator.connection)
Copy link
Contributor

Choose a reason for hiding this comment

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

@wingleung

Mentioned above.

Copy link
Contributor

Choose a reason for hiding this comment

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

semicolon please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this initialization needs to be in the hook because we use useState

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm I get the feeling moving the supported initialization should work but it's failing some of the tests for useNetworkStatus() and I'm not sure if it's a jest/jsdom thing or a react hook/useState thing.

Copy link
Contributor

@anton-karlovskiy anton-karlovskiy Mar 13, 2020

Choose a reason for hiding this comment

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

@wingleung

Thank you for your updates.
Have you made some progress on it? I'll also try and fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

As long as we've considered this conditional typeof window !== 'undefined' I think it won't throw error.

Copy link
Contributor

Choose a reason for hiding this comment

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

useMemoryStatus, useHardwareConcurrency, useHardwareConcurrency

For above hooks or utilities, I think we have already moved unsupported out of the main function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved them out of the hooks for the others indeed, would be nice to move it for the network one too. The useState somehow needs te be treated differently in the tests than the ones without useState, I'll have another look at it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@anton-karlovskiy finally got it working, the tests need to load the modules in a jest.isolateModules() function. This uses a sandboxed registry to ensure no states outside of the hook is being reused in the other tests.

Copy link
Contributor

@anton-karlovskiy anton-karlovskiy Mar 15, 2020

Choose a reason for hiding this comment

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

@wingleung

Thank you for all your efforts through getting it work.
I'll look through the changes made to learn how jest.isolateModules() has been used.

save-data/index.js Outdated Show resolved Hide resolved
@anton-karlovskiy
Copy link
Contributor

anton-karlovskiy commented Mar 10, 2020

@wingleung cc @addyosmani

Overall the optimization looks good to me.
I think we could reduce the number of lines of code.

@wingleung Could you please check my comments?

@wingleung
Copy link
Contributor Author

@anton-karlovskiy since the new commits use the supported property I've changed the unsupported properties from the other hooks to supported

@anton-karlovskiy
Copy link
Contributor

since the new commits use the supported property I've changed the unsupported properties from the other hooks to supported

That sounds good to me. But first let's ask @addyosmani.

@addyosmani addyosmani added the chillin' ❄️ Chillin' (awaiting further feedback) label Mar 13, 2020
@wingleung wingleung force-pushed the feature/code-cleanup-suggestions branch from 4b4731d to cf8b7f8 Compare March 13, 2020 21:57
@anton-karlovskiy
Copy link
Contributor

anton-karlovskiy commented Mar 20, 2020

@wingleung

There are conflicting files.

@wingleung wingleung force-pushed the feature/code-cleanup-suggestions branch from 58aba42 to ef759f5 Compare March 21, 2020 06:55
…nup-suggestions

# Conflicts:
#	media-capabilities/index.js
#	media-capabilities/media-capabilities.test.js
#	save-data/index.js
#	save-data/save-data.test.js
@wingleung
Copy link
Contributor Author

@anton-karlovskiy thanks for the heads up, merge conflicts resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chillin' ❄️ Chillin' (awaiting further feedback)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants