generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 37
refactor: Use channel to optimize render #32
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69779b4
chore: init effect state
zombieJ 0999a11
refactor: useLayoutEffect for refresh
zombieJ 278fa3b
chore: tmp of key cache
zombieJ 9eff81a
refactor: merge effect at once
zombieJ e3ef549
chore: clean up
zombieJ 559dbbb
chore: support if channel not support
zombieJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import raf from 'rc-util/lib/raf'; | ||
|
||
export default function channelUpdate(callback: VoidFunction) { | ||
if (typeof MessageChannel === 'undefined') { | ||
raf(callback); | ||
} else { | ||
const channel = new MessageChannel(); | ||
channel.port1.onmessage = () => callback(); | ||
channel.port2.postMessage(undefined); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import useEvent from 'rc-util/lib/hooks/useEvent'; | ||
import * as React from 'react'; | ||
import { unstable_batchedUpdates } from 'react-dom'; | ||
import channelUpdate from './channelUpdate'; | ||
|
||
type Updater<T> = T | ((origin: T) => T); | ||
|
||
type UpdateCallbackFunc = VoidFunction; | ||
|
||
type NotifyEffectUpdate = (callback: UpdateCallbackFunc) => void; | ||
|
||
/** | ||
* Batcher for record any `useEffectState` need update. | ||
*/ | ||
export function useBatcher() { | ||
// Updater Trigger | ||
const updateFuncRef = React.useRef<UpdateCallbackFunc[]>(null); | ||
|
||
// Notify update | ||
const notifyEffectUpdate: NotifyEffectUpdate = callback => { | ||
if (!updateFuncRef.current) { | ||
updateFuncRef.current = []; | ||
|
||
channelUpdate(() => { | ||
unstable_batchedUpdates(() => { | ||
updateFuncRef.current.forEach(fn => { | ||
fn(); | ||
}); | ||
updateFuncRef.current = null; | ||
}); | ||
}); | ||
} | ||
|
||
updateFuncRef.current.push(callback); | ||
}; | ||
|
||
return notifyEffectUpdate; | ||
} | ||
|
||
/** | ||
* Trigger state update by `useLayoutEffect` to save perf. | ||
*/ | ||
export default function useEffectState<T extends string | number | object>( | ||
notifyEffectUpdate: NotifyEffectUpdate, | ||
defaultValue?: T, | ||
): [T, (value: Updater<T>) => void] { | ||
// Value | ||
const [stateValue, setStateValue] = React.useState(defaultValue); | ||
|
||
// Set State | ||
const setEffectVal = useEvent((nextValue: Updater<T>) => { | ||
notifyEffectUpdate(() => { | ||
setStateValue(nextValue); | ||
}); | ||
}); | ||
|
||
return [stateValue, setEffectVal]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Overflow, { OverflowProps } from './Overflow'; | ||
import Overflow from './Overflow'; | ||
import type { OverflowProps } from './Overflow'; | ||
|
||
export { OverflowProps }; | ||
export type { OverflowProps }; | ||
|
||
export default Overflow; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
TS 会报错,所以包了一下。
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.
soga