Skip to content

Commit b9eb7ff

Browse files
author
Kai.b
committed
perf: skip hidden notification progress updates
1 parent cea66a1 commit b9eb7ff

2 files changed

Lines changed: 134 additions & 2 deletions

File tree

‎src/Notification.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { clsx } from 'clsx';
33
import useNoticeTimer from './hooks/useNoticeTimer';
4-
import { isNonNullable, isReactRenderable, useEvent } from '@rc-component/util';
4+
import { isNonNullable, isReactRenderable, useEvent, useLayoutEffect } from '@rc-component/util';
55
import useClosable, { type ClosableType } from './hooks/useClosable';
66
import DefaultProgress from './Progress';
77
import type { NotificationProgressProps } from './Progress';
@@ -106,6 +106,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
106106
} = props;
107107

108108
const [percent, setPercent] = React.useState(0);
109+
const percentRef = React.useRef(0);
109110
const noticePrefixCls = `${prefixCls}-notice`;
110111

111112
// ========================= Close ==========================
@@ -118,7 +119,19 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
118119
// ======================== Duration ========================
119120
const [hovering, setHovering] = React.useState(false);
120121

121-
const [onResume, onPause] = useNoticeTimer(duration, onInternalClose, setPercent);
122+
const [onResume, onPause] = useNoticeTimer(duration, onInternalClose, (nextPercent) => {
123+
percentRef.current = nextPercent;
124+
if (showProgress) {
125+
setPercent(nextPercent);
126+
}
127+
});
128+
129+
// Progress can be enabled while paused, when no timer tick will update state.
130+
useLayoutEffect(() => {
131+
if (showProgress) {
132+
setPercent(percentRef.current);
133+
}
134+
}, [showProgress]);
122135

123136
const validPercent = 100 - Math.min(Math.max(percent * 100, 0), 100);
124137
const Progress = components?.progress || DefaultProgress;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { act, render } from '@testing-library/react';
2+
import React from 'react';
3+
import Notification from '../src/Notification';
4+
5+
function advanceFrames(count: number) {
6+
for (let frame = 0; frame < count; frame += 1) {
7+
act(() => vi.advanceTimersByTime(16));
8+
}
9+
}
10+
11+
describe('Notification timer rendering', () => {
12+
beforeEach(() => vi.useFakeTimers());
13+
afterEach(() => vi.useRealTimers());
14+
15+
it.each([undefined, false])(
16+
'does not render hidden progress on timer ticks (%s)',
17+
(showProgress) => {
18+
const onRender = vi.fn();
19+
const onClose = vi.fn();
20+
const { queryByRole, unmount } = render(
21+
<React.Profiler id="notification" onRender={onRender}>
22+
<Notification
23+
prefixCls="rc-notification"
24+
description="Notice"
25+
duration={1}
26+
showProgress={showProgress}
27+
onClose={onClose}
28+
/>
29+
</React.Profiler>,
30+
);
31+
const initialRenderCount = onRender.mock.calls.length;
32+
33+
advanceFrames(32);
34+
35+
expect(onRender).toHaveBeenCalledTimes(initialRenderCount);
36+
expect(queryByRole('progressbar')).toBeNull();
37+
expect(onClose).not.toHaveBeenCalled();
38+
39+
advanceFrames(32);
40+
expect(onClose).toHaveBeenCalledTimes(1);
41+
unmount();
42+
},
43+
);
44+
45+
it('continues rendering visible progress and closes at the original duration', () => {
46+
const onClose = vi.fn();
47+
const { getByRole, unmount } = render(
48+
<Notification prefixCls="rc-notification" duration={1} showProgress onClose={onClose} />,
49+
);
50+
51+
advanceFrames(32);
52+
expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(48.8, 1);
53+
expect(onClose).not.toHaveBeenCalled();
54+
55+
advanceFrames(32);
56+
expect(onClose).toHaveBeenCalledTimes(1);
57+
unmount();
58+
});
59+
60+
it('updates progress when enabled during an existing countdown without restarting it', () => {
61+
const onClose = vi.fn();
62+
const onRender = vi.fn();
63+
const notice = (showProgress: boolean) => (
64+
<React.Profiler id="notification" onRender={onRender}>
65+
<Notification
66+
prefixCls="rc-notification"
67+
duration={1}
68+
showProgress={showProgress}
69+
onClose={onClose}
70+
/>
71+
</React.Profiler>
72+
);
73+
const { rerender, getByRole, unmount } = render(notice(false));
74+
75+
advanceFrames(16);
76+
rerender(notice(true));
77+
advanceFrames(16);
78+
expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(48.8, 1);
79+
80+
rerender(notice(false));
81+
const renderCount = onRender.mock.calls.length;
82+
advanceFrames(16);
83+
expect(onRender).toHaveBeenCalledTimes(renderCount);
84+
expect(onClose).not.toHaveBeenCalled();
85+
86+
advanceFrames(16);
87+
expect(onClose).toHaveBeenCalledTimes(1);
88+
unmount();
89+
});
90+
91+
it('shows elapsed progress when enabled while the countdown is paused', () => {
92+
const onClose = vi.fn();
93+
const notice = (showProgress: boolean, hovering: boolean) => (
94+
<Notification
95+
prefixCls="rc-notification"
96+
duration={1}
97+
showProgress={showProgress}
98+
hovering={hovering}
99+
onClose={onClose}
100+
/>
101+
);
102+
const { rerender, getByRole, unmount } = render(notice(false, false));
103+
104+
advanceFrames(16);
105+
rerender(notice(false, true));
106+
advanceFrames(32);
107+
rerender(notice(true, true));
108+
109+
expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(74.4, 1);
110+
advanceFrames(16);
111+
expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(74.4, 1);
112+
expect(onClose).not.toHaveBeenCalled();
113+
114+
rerender(notice(true, false));
115+
advanceFrames(48);
116+
expect(onClose).toHaveBeenCalledTimes(1);
117+
unmount();
118+
});
119+
});

0 commit comments

Comments
 (0)