Skip to content

Conversation

Copy link

Copilot AI commented Jan 9, 2026

[English Template / 英文模板]

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

💡 需求背景和解决方案

问题:useDebounceFn/useThrottleFn、useDebounce/useThrottle、useDebounceEffect/useThrottleEffect 存在 ~95% 代码重复(每对各约 46、20、26 行)。

方案:提取通用模式到三个工厂函数:

  1. createRateLimitFn - 生成 useDebounceFn/useThrottleFn

    const useDebounceFn = createRateLimitFn<noop, DebounceOptions>(debounce, 'useDebounceFn');
    const useThrottleFn = createRateLimitFn<noop, ThrottleOptions>(throttle, 'useThrottleFn');
  2. createRateLimitValue - 生成 useDebounce/useThrottle

    const useDebounce = createRateLimitValue(useDebounceFn);
    const useThrottle = createRateLimitValue(useThrottleFn);
  3. createRateLimitEffect - 生成 useDebounceEffect/useThrottleEffect

    const useDebounceEffect = createRateLimitEffect(useDebounceFn);
    const useThrottleEffect = createRateLimitEffect(useThrottleFn);

新增目录:packages/hooks/src/createRateLimitHooks/

结果

  • 消除 ~74 行重复代码
  • 类型安全完整保留(DebounceOptions 包含 maxWait,ThrottleOptions 不包含)
  • 零破坏性变更,所有公开 API 不变

📝 更新日志

语言 更新描述
🇺🇸 英文 Refactor: Extract duplicated rate-limiting code into factory functions
🇨🇳 中文 重构:将重复的防抖/节流代码提取为工厂函数

☑️ 请求合并前的自查清单

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供
Original prompt

Find and refactor duplicated code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability refactor: extract duplicated rate-limiting code into factory functions Jan 9, 2026
Copilot AI requested a review from liuyib January 9, 2026 06:38
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 18, 2026

npm i https://pkg.pr.new/ahooks@2893
npm i https://pkg.pr.new/@ahooksjs/use-url-state@2893

commit: 48e0c8a

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.

3 participants