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

轮询时,如何只loading一次?后面的请求不再loading 了 #213

Closed
4 tasks done
zaxlct opened this issue Jun 24, 2024 · 6 comments
Closed
4 tasks done
Labels
enhancement: pending triage issue pending triage

Comments

@zaxlct
Copy link

zaxlct commented Jun 24, 2024

Description

  const { data, error, loading, run, runAsync } = useRequest('xxx', {
    manual,
    pollingInterval: 1000 * 5,
  })

如果页面上绑定了v-loading="loading"那么每次请求都会loading一次,我的需求是只希望第一次时loading,后续不要再loading了,这样会打断用户操作

Suggested solution

建议:返回一个类似 pollingTimes的参数?如果是1就代表执行了一次

Alternative

No response

Additional context

No response

Validations

@zaxlct zaxlct added the enhancement: pending triage issue pending triage label Jun 24, 2024
@Sharang-heng
Copy link

+1,现在要实现这种需求还得自己手动维护一个参数,如果官方能提供参数控制就最好了

@XiaoDaiGua-Ray
Copy link
Contributor

XiaoDaiGua-Ray commented Jun 26, 2024

针对这个建议,可以考虑通过自定义一个插件去实现。不过轮询次数的统计返回值,可以考虑实现。

@NelsonYong
Copy link
Contributor

我的建议是 自己维护一个 loading 的 ref,在点击的时候设置为 true, 在 onsuccess 中判断,如果 loading 为 true 则设置为 false.
目前我这边不打算实现类似的功能,需要自己维护 @Sharang-heng

@zm8
Copy link

zm8 commented Jul 3, 2024

可以这样 v-loading="loading && !data",其实更希望内部抛出一个 refreshing。

@inhiblab
Copy link
Contributor

@zm8 可以通过插件式去实现这种定制需求 https://inhiblabcore.github.io/docs/hooks/useRequest/plugin/ 。也可以参考官方的插件进行设计实现

@inhiblab
Copy link
Contributor

inhiblab commented Jul 13, 2024

@zaxlct @zm8 @XiaoDaiGua-Ray 这也是一个好的方式。我这里只写了个 demo,具体细节可以在业务中再完善哈。插件系统搞起来 🤩

 const useFirstLoadingForPollingPlugin: UseRequestPlugin<
    any,
    [],
    {
      usePollingFirstLoading?: boolean
      pollingCallback?: (isLoading: boolean) => void
    }
  > = (fetchInstance, { pluginOptions }) => {
    const usePollingFirstLoading = !!pluginOptions?.usePollingFirstLoading
    const markRef = ref(false)
    return {
      name: 'firstLoadingForPollingPlugin',
      onBefore: () => {
        if (usePollingFirstLoading && !markRef.value) {
          markRef.value = true
          pluginOptions?.pollingCallback(true)
        } else {
          pluginOptions?.pollingCallback(false)
        }
      },
      onSuccess: () => {},
    }
  }
  const isLoadingRef = ref(false)
  const { data, loading } = useRequest(
    () => getUsername(),
    {
      debugKey: 'plugindemo',
      pollingInterval: 2000,
      pluginOptions: {
        usePollingFirstLoading: true,
        pollingCallback: isLoading => {
          console.log('isLoading', isLoading)
          isLoadingRef.value = isLoading
        }
      },
    },
    [useFirstLoadingForPollingPlugin],
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: pending triage issue pending triage
Projects
None yet
Development

No branches or pull requests

6 participants