Skip to content

Commit b7c8334

Browse files
committed
fix: Clamp duration to setTimeout max value
1 parent 477e95f commit b7c8334

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/Notice.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { clsx } from 'clsx';
22
import KeyCode from '@rc-component/util/lib/KeyCode';
3+
import warning from '@rc-component/util/lib/warning';
34
import * as React from 'react';
45
import type { NoticeConfig } from './interface';
56
import pickAttrs from '@rc-component/util/lib/pickAttrs';
67

8+
/**
9+
* Maximum delay value for setTimeout in seconds (2^31 - 1 ms).
10+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value
11+
*/
12+
const MAX_DURATION = 2147483647 / 1000;
13+
714
export interface NoticeProps extends Omit<NoticeConfig, 'onClose'> {
815
prefixCls: string;
916
className?: string;
@@ -38,7 +45,9 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
3845
const [percent, setPercent] = React.useState(0);
3946
const [spentTime, setSpentTime] = React.useState(0);
4047
const mergedHovering = forcedHovering || hovering;
41-
const mergedDuration: number = typeof duration === 'number' ? duration : 0;
48+
49+
const rawDuration: number = typeof duration === 'number' ? duration : 0;
50+
const mergedDuration: number = Math.min(rawDuration, MAX_DURATION);
4251
const mergedShowProgress = mergedDuration > 0 && showProgress;
4352

4453
// ======================== Close =========================
@@ -52,6 +61,14 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
5261
}
5362
};
5463

64+
// ========================= Warn =========================
65+
React.useEffect(() => {
66+
warning(
67+
rawDuration <= MAX_DURATION,
68+
`\`duration\` exceeds the maximum supported value (${MAX_DURATION}s) and has been clamped.`,
69+
);
70+
}, [rawDuration]);
71+
5572
// ======================== Effect ========================
5673
React.useEffect(() => {
5774
if (!mergedHovering && mergedDuration > 0) {

0 commit comments

Comments
 (0)