-
Notifications
You must be signed in to change notification settings - Fork 52
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
perf(react): remove unnecessary useSetTimeout's callback calling #264
Conversation
Co-authored-by: Gromit (전민재) <[email protected]>
🦋 Changeset detectedLatest commit: bfd97a9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Codecov Report
@@ Coverage Diff @@
## main #264 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 802 810 +8
Branches 135 136 +1
=========================================
+ Hits 802 810 +8
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left comments as my intention
const isClient = typeof window !== 'undefined' | ||
export const useIsomorphicLayoutEffect = isClient ? useLayoutEffect : useEffect | ||
|
||
export const useTimeout = (fn: () => void, ms: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed useSetTimeout to useTimeout, because we use just Timeout not setTimeout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we use ms package for this? https://www.npmjs.com/package/ms
It is really good for dx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understood that you meant to use ms
inside suspensive
. But wouldn't it be more useful to use it like the code below? If ms
is unnecessary, it can prevent unnecessary dependencies from being installed and unnecessary calculations to be performed. Also, the unit of time desired by the Delay component is quite small. In most cases, I expect to use ms prop
for Delay of less than 2000ms, but I am wondering whether it would be a good idea to use ms
internally in this situation. If suspensive need to include it, I think it would be a good idea to create ms yourself so that you can include only the necessary parts.
Delay using with ms is okay
import ms from 'ms'
<Delay ms={ms('2 days')}>
{/* ... */}
</Delay>
Delay have to make BREAKING CHANGE if we use ms
internally
<Delay time={'2 days'}> // this ms prop have to make BREAKING CHANGE. because '2 days' is not just ms prop
{/* ... */}
</Delay>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean to use it for test-utils, It is internal package not for user.
If not, I agree with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that makes sense. I agree ms
can be good devDependency to test case like this! Let's add ms
as root's devDependency in next Pull Request
export const useTimeout = (fn: () => void, ms: number) => { | ||
const fnRef = useRef(fn) | ||
|
||
useIsomorphicLayoutEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used useIsomorphicLayoutEffect in this time, thanks to @minsoo-web
I referred to this code of usehooks-ts recommended by @ssi02014 a lot. thanks!👍
useEffect(() => { | ||
const timeout = setTimeout(fn, delay) | ||
return () => clearTimeout(timeout) | ||
}, [fn, delay]) | ||
const id = setTimeout(() => fnRef.current(), ms) | ||
return () => clearTimeout(id) | ||
}, [ms]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unncessary fn calling
@ssi02014 If you okay, please approve this Pull Request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👏
Ah, we need to add test case to check there is no unncessary useTimeout's fn calling on rerendering. I will add it to prove this fixing will work |
Yes, I agree. The old (The test code below is just a reference example, written in const delayTime = 1000;
const TestComponent = () => {
const [number, setNumber] = useState(0);
useTimeout(() => {
setNumber(number + 1);
}, delayTime);
return <div>{number}</div>;
}; it('should not be re-called even if the component is re-rendered', () => {
const { rerender } = render(<TestComponent />);
expect(screen.getByText('0')).toBeInTheDocument();
act(() => jest.advanceTimersByTime(delayTime));
expect(screen.getByText('1')).toBeInTheDocument();
rerender(<TestComponent />);
act(() => jest.advanceTimersByTime(delayTime))
expect(screen.getByText('1')).toBeInTheDocument();
}); |
Oh, cool! If you okay, could you make Pull Request to this branch too? |
@manudeli Oh sorry I didn't catch your intent, can I just keep the current pull request and work on it? |
After merging this, new Pull Request to add test case also okay! Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
const timeout = setTimeout(fn, delay) | ||
return () => clearTimeout(timeout) | ||
}, [fn, delay]) | ||
const id = setTimeout(() => fnRef.current(), ms) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const id = setTimeout(() => fnRef.current(), ms) | |
const id = setTimeout(fnRef.current, ms) |
what about remove useless callback function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manudeli
please check this review
const isClient = typeof window !== 'undefined' | ||
export const useIsomorphicLayoutEffect = isClient ? useLayoutEffect : useEffect | ||
|
||
export const useTimeout = (fn: () => void, ms: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we use ms package for this? https://www.npmjs.com/package/ms
It is really good for dx.
import { useEffect, useRef } from 'react' | ||
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect' | ||
|
||
export const useTimeout = (fn: () => void, ms: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this hook is needable to export ? why don't we import from test-util? it is duplicated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@minsoo-web
I checked and the Delay
component is using the existing useSetTimeout
.
Since "test-utils" has strong test-related semantics, we should consider whether it's appropriate to export and use the "useTimeout" hook from "test-utils". Personally, I think it's more appropriate to put it in suspensive/react
.
Currently, "suspensive/react" has "suspensive/test-utils" as a dependency, so adding "suspensive/react" as a dependency from "suspensive/test-utils" to eliminate duplicate code may cause a circular dependency issue
.
Is there a good way to remove duplicate code while preserving the intent of the package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@minsoo-web ssi02014 commented why same with exactly my intention.
also @suspensive/test-utils is just devDependency of @suspensive/react. so we need to import useTimeout from internal module (@suspensive/react/src/hooks).
If you know alternative way, make Pull Request or add Issue please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is simply a suggestion, so I hope you take it lightly.
I thought it would be fun to create a new package related to timer and manage it as a repo.
@timer/react
- useTimeout
- useInterval
@timer/core
- delayPromise
I thought it would be a way to take the package we made and distributed as a new dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
const timeout = setTimeout(fn, delay) | ||
return () => clearTimeout(timeout) | ||
}, [fn, delay]) | ||
const id = setTimeout(() => fnRef.current(), ms) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manudeli
please check this review
fix #267 # Overview <!-- A clear and concise description of what this pr is about. --> @minsoo-web suggest us to add `ms` in #264 (comment) I added `ms` as dev dependency to test apis related with time ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests.
fix #263 # Overview <!-- A clear and concise description of what this pr is about. --> @ssi02014 Thanks for reporting important issue. Could you review this Pull Request too? I remove unnecessary useSetTimeout's callback calling and I added you as co-author in [79fecd9](79fecd9) ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Gromit (전민재) <[email protected]>
fix #267 # Overview <!-- A clear and concise description of what this pr is about. --> @minsoo-web suggest us to add `ms` in #264 (comment) I added `ms` as dev dependency to test apis related with time ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests.
fix #263 # Overview <!-- A clear and concise description of what this pr is about. --> @ssi02014 Thanks for reporting important issue. Could you review this Pull Request too? I remove unnecessary useSetTimeout's callback calling and I added you as co-author in [79fecd9](79fecd9) ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests. ---------
fix #267 # Overview <!-- A clear and concise description of what this pr is about. --> @minsoo-web suggest us to add `ms` in #264 (comment) I added `ms` as dev dependency to test apis related with time ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests.
fix #263 # Overview <!-- A clear and concise description of what this pr is about. --> @ssi02014 Thanks for reporting important issue. Could you review this Pull Request too? I remove unnecessary useSetTimeout's callback calling and I added you as co-author in [79fecd9](79fecd9) ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Gromit (전민재) <[email protected]>
fix #267 # Overview <!-- A clear and concise description of what this pr is about. --> @minsoo-web suggest us to add `ms` in #264 (comment) I added `ms` as dev dependency to test apis related with time ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests.
fix #263
Overview
@ssi02014 Thanks for reporting important issue. Could you review this Pull Request too?
I remove unnecessary useSetTimeout's callback calling and I added you as co-author in 79fecd9
PR Checklist