Skip to content

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
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 22 additions & 9 deletions src/Overflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import classNames from 'classnames';
import ResizeObserver from 'rc-resize-observer';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import Item from './Item';
import { useBatchFrameState } from './hooks/useBatchFrameState';
import useEffectState, { useBatcher } from './hooks/useEffectState';
import RawItem from './RawItem';

export const OverflowContext = React.createContext<{
Expand Down Expand Up @@ -89,24 +89,37 @@ function Overflow<ItemType = any>(
...restProps
} = props;

const createUseState = useBatchFrameState();

const fullySSR = ssr === 'full';

const [containerWidth, setContainerWidth] = createUseState<number>(null);
const notifyEffectUpdate = useBatcher();

const [containerWidth, setContainerWidth] = useEffectState<number>(
notifyEffectUpdate,
null,
);
const mergedContainerWidth = containerWidth || 0;

const [itemWidths, setItemWidths] = createUseState(
const [itemWidths, setItemWidths] = useEffectState(
notifyEffectUpdate,
new Map<React.Key, number>(),
);

const [prevRestWidth, setPrevRestWidth] = createUseState(0);
const [restWidth, setRestWidth] = createUseState(0);
const [prevRestWidth, setPrevRestWidth] = useEffectState<number>(
notifyEffectUpdate,
0,
);
const [restWidth, setRestWidth] = useEffectState<number>(
notifyEffectUpdate,
0,
);

const [suffixWidth, setSuffixWidth] = createUseState(0);
const [suffixWidth, setSuffixWidth] = useEffectState<number>(
notifyEffectUpdate,
0,
);
const [suffixFixedStart, setSuffixFixedStart] = useState<number>(null);

const [displayCount, setDisplayCount] = useState(null);
const [displayCount, setDisplayCount] = useState<number>(null);
const mergedDisplayCount = React.useMemo(() => {
if (displayCount === null && fullySSR) {
return Number.MAX_SAFE_INTEGER;
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/channelUpdate.ts
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();
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
channel.port1.onmessage = () => callback();
channel.port1.onmessage = callback;

Copy link
Member Author

Choose a reason for hiding this comment

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

TS 会报错,所以包了一下。

Copy link
Member

Choose a reason for hiding this comment

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

soga

channel.port2.postMessage(undefined);
}
}
45 changes: 0 additions & 45 deletions src/hooks/useBatchFrameState.tsx

This file was deleted.

58 changes: 58 additions & 0 deletions src/hooks/useEffectState.tsx
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];
}
5 changes: 3 additions & 2 deletions src/index.tsx
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;