Skip to content

Conversation

@jynxio
Copy link

@jynxio jynxio commented Jan 3, 2026

Problem Description

The setTimeout used in this component has a maximum delay limit. If the duration exceeds this limit, it overflows and triggers immediately (treated as 0ms).

This causes issues for Ant Design's Message and Notification components (CodeSandbox):

import { App } from 'antd';

const { message } = App.useApp();
const maxDuration = Math.pow(2, 31) - 1; // ms
const exceededDuration = maxDuration + 1; // ms

message.info('', maxDuration / 1000); // ✅ Works: within limit, renders normally
message.info('', exceededDuration / 1000); // ❌ Fails: exceeds limit, closes immediately due to overflow


Proposed Solutions

I considered three approaches. This PR implements Option 2. Please let me know if you prefer a different approach.

  1. Treat overflow as 0: The component will never close (duration = 0)
  2. Clamp to max value: Cap the duration at the maximum valid setTimeout limit (closes after ~25 days)
  3. Support exact duration: Strictly implement the specified duration using recursive setTimeout

Why not Option 3?
It has a low ROI (Return on Investment):

  1. Complexity: Requires extra code to handle recursion and race conditions
  2. Rarity: This is an extreme edge case
  3. Sufficiency: Option 2 is practically sufficient for all real-world use cases


Changes

  • Implemented Option 2
  • Added test cases


Impact

If the duration for a Message or Notification exceeds the limit:

  • Before: The component flashed and disappeared immediately
  • After: The duration is clamped to approximately 25 days

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • 通知组件现在会将过长的持续时间限制在最大值,超出时会发出警告提示。
  • Tests

    • 新增测试覆盖持续时间限制和警告机制的验证。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Jan 3, 2026

@jynxio is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Jan 3, 2026

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

概述

该 PR 在 Notice 组件中引入了最大持续时间限制,使用 MAX_DURATION 常数对提供的 duration 值进行约束,当超出限制时会触发警告。同时添加了测试用例来验证这一约束行为及相关警告机制。

变更内容

功能模块 文件 变更摘要
Duration 约束逻辑
src/Notice.tsx
引入 MAX_DURATION 常数对 duration 进行上限约束;添加 useEffect 钩子在超出限制时通过 warning 工具发出警告;保留原有的进度计算和超时逻辑,但改用约束后的 duration 值
MAX_DURATION 验证测试
tests/index.test.tsx
新增测试套件验证 duration 在限制内(如 0.1s)时正常渲染且无警告;当 duration 超出上限时验证警告输出、值约束以及约束后的定时器行为

代码审查工作量评估

🎯 2 (Simple) | ⏱️ ~10 分钟

可能相关的 PR

建议审查者

  • afc163
  • zombieJ

庆祝诗

🐰 噢,时光有了上限呀,
超出就会发出警告!
Duration 被温柔地约束,
Math.min 保驾护航,
测试覆盖周全,代码稳如磐石! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确总结了主要变更:将超过 setTimeout 最大值的 duration 限制在最大值以修复溢出问题。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @jynxio, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a subtle but critical bug in how Ant Design's Message and Notification components handle extremely long duration values. Previously, durations exceeding the setTimeout maximum (a 32-bit signed integer limit) would cause an overflow, leading to the component immediately disappearing. The fix introduces a mechanism to clamp such durations to the maximum allowable value, ensuring the components behave as expected by remaining visible for the capped duration, and provides a clear warning to developers when this clamping occurs.

Highlights

  • Problem Addressed: This PR fixes an issue where Ant Design's Message and Notification components would immediately disappear if their duration exceeded the maximum delay value for setTimeout (2^31 - 1 milliseconds), due to an integer overflow.
  • Solution Implemented: The chosen solution clamps the provided duration to the setTimeout maximum limit (approximately 25 days) if it exceeds this value, preventing the overflow and ensuring the component remains visible for the capped duration.
  • Developer Warning: A warning is now issued to the console if a duration value is provided that exceeds the setTimeout maximum, informing developers that the value has been clamped.
  • Test Coverage: New test cases have been added to verify the clamping behavior and ensure the warning is correctly triggered when an excessive duration is used.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue with setTimeout overflowing for large duration values. The chosen solution is to clamp the duration to the maximum supported value, which is a sensible approach for this edge case. The implementation is correct and includes a helpful warning for developers when the duration is clamped. The added tests are thorough and cover the new logic well. I've provided a couple of suggestions to improve code readability and maintainability by replacing magic numbers with their calculated forms, making the code's intent clearer.

* Maximum delay value for setTimeout in seconds (2^31 - 1 ms).
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value
*/
const MAX_DURATION = 2147483647 / 1000;

Choose a reason for hiding this comment

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

medium

To improve readability and avoid a magic number, it would be clearer to express 2147483647 as the calculation it represents. This makes the origin of the number apparent directly in the code, aligning with the explanation in the comment and the PR description.

Suggested change
const MAX_DURATION = 2147483647 / 1000;
const MAX_DURATION = (2 ** 31 - 1) / 1000;

Comment on lines +1042 to +1043
const MAX_DURATION = 2147483647 / 1000;
const EXCEEDED_DURATION = (2147483647 + 1) / 1000;

Choose a reason for hiding this comment

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

medium

To improve maintainability and avoid repeating the magic number 2147483647, you could define the maximum timeout value in milliseconds as a constant and then derive MAX_DURATION and EXCEEDED_DURATION from it. This makes the code easier to read and ensures consistency if the value ever needs to be changed.

Suggested change
const MAX_DURATION = 2147483647 / 1000;
const EXCEEDED_DURATION = (2147483647 + 1) / 1000;
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
const MAX_DURATION = MAX_TIMEOUT_MS / 1000;
const EXCEEDED_DURATION = (MAX_TIMEOUT_MS + 1) / 1000;

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/index.test.tsx (1)

1038-1067: 测试全面验证了限制行为,建议考虑复用常量。

该测试正确验证了超限时长的警告和限制行为,包括边界条件的检查。测试逻辑准确且覆盖充分。

一个可选的改进:测试中硬编码了 MAX_DURATION 常量(第 1042 行),可以考虑从 src/Notice.tsx 导入该常量以提高可维护性。不过由于该值基于 JavaScript 规范不太可能变化,当前实现也是可接受的。

🔎 可选的改进方案

可以导出 MAX_DURATION 常量并在测试中导入:

src/Notice.tsx 中:

 /**
  * Maximum delay value for setTimeout in seconds (2^31 - 1 ms).
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value
  */
-const MAX_DURATION = 2147483647 / 1000;
+export const MAX_DURATION = 2147483647 / 1000;

tests/index.test.tsx 中:

+import { MAX_DURATION } from '../src/Notice';
+
 describe('MAX_DURATION', () => {
   // ...
   it('should warn and clamp when duration exceeds limit', () => {
     const { instance } = renderDemo();
     const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
 
-    const MAX_DURATION = 2147483647 / 1000;
     const EXCEEDED_DURATION = (2147483647 + 1) / 1000;
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 477e95f and ea760b7.

📒 Files selected for processing (2)
  • src/Notice.tsx
  • tests/index.test.tsx
🔇 Additional comments (5)
src/Notice.tsx (4)

8-12: 常量定义正确且文档清晰。

MAX_DURATION 的计算和注释都很准确,将 setTimeout 的最大毫秒值转换为秒以匹配 duration 属性的单位。MDN 链接为开发者提供了有用的参考。


49-50: 限制逻辑实现正确。

rawDuration 正确处理了非数字类型的 duration(如 false),mergedDuration 通过 Math.min 将时长限制在最大值内。这个实现简洁且正确。


64-70: 警告机制设计合理。

使用 useEffect 配合 rawDuration 依赖项来触发警告,确保当时长超限时开发者能够收到提示。警告消息清晰地说明了限制值和处理方式。


51-51: 现有逻辑与限制机制集成正确。

所有使用时长的地方(进度条、超时定时器)都统一使用了 mergedDuration,确保了限制行为的一致性。通知将在限制后的时长结束时正确关闭。

Also applies to: 74-91, 93-119

tests/index.test.tsx (1)

1023-1036: 测试用例正确验证了正常情况。

该测试确认了当 duration 在限制范围内时不会触发警告,测试值(0.1s)选择合理,代表了典型的使用场景。

@codecov
Copy link

codecov bot commented Jan 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.74%. Comparing base (477e95f) to head (ea760b7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #381   +/-   ##
=======================================
  Coverage   99.74%   99.74%           
=======================================
  Files           7        7           
  Lines         776      793   +17     
  Branches      127      128    +1     
=======================================
+ Hits          774      791   +17     
  Misses          2        2           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant