Skip to content
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

fix: useRequest pollingInterval invalid after staleTime is enabled #2568

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

electroluxcode
Copy link

@electroluxcode electroluxcode commented Jun 13, 2024

[English Template / 英文模板]

🤔 这个变动的性质是?

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

🔗 相关 Issue

fix #2505

💡 需求背景和解决方案

发现是 plugin 之间相互影响了,staleTime 导致了缓存,使得returnNow要求立刻返回值, 而pollingInterval 要求轮询值, 导致了 useRequest 的 staleTime 和 pollingInterval 不能够按照预期工作。因为返回值后就永远不会轮询。

正确的做法应该是 判断应该是是处于 returnNow 和 pollingInterval 的交叉状态,如果是这个状态,那么将 缓存值传递到各个plugin的 onSuccess和onFinish等方法中去(原来的代码直接return Promise.resolve,导致轮询永远无法触发),然后再执行 return Promise.resolve 。 假如不是这个交叉状态,按原来的逻辑走就行

然后原来的plugin中并不能共享 returnNow stopNow,pollingNow 之类的状态,插件中需要对这些状态进行判断,这个pr对上面的问题进行了简单的重构,

补一个用于测试的实例

import { useRequest } from 'ahooks';
import { useEffect } from 'react';
export default () => {
  let { run, loading } = useRequest(
    async (param) => {
      console.log('param');
      return new Promise((resolve) => {
        resolve(param);
      });
    },
    {
      staleTime: 3 * 1000,
      pollingInterval: 1 * 1000,
      cacheKey: 'DD',
      manual: true,
      onSuccess: (e) => {
        console.log('success', e, loading);
      },
    },
  );
  useEffect(() => {
    run('测试');
  }, []);
  return <div>测试</div>;
};

📝 更新日志

语言 更新描述
🇺🇸 英文
🇨🇳 中文

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

⚠️ 请自检并全部勾选全部选项⚠️

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

@CLAassistant
Copy link

CLAassistant commented Jun 13, 2024

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ electroluxcode
❌ crazylxr
You have signed the CLA already but the status is still pending? Let us recheck it.

@electroluxcode electroluxcode changed the title fix: useRequest polling invalid after staleTime cache is enabled fix: useRequest pollingInterval invalid after staleTime is enabled Jun 13, 2024
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.

useRequest 开启 staleTime 缓存后轮询失效
3 participants