Skip to content

Commit

Permalink
feat: optimized useRequest refreshDeps type && docs opt
Browse files Browse the repository at this point in the history
feat: optimized useRequest refreshDeps type && docs opt
  • Loading branch information
inhiblab authored Jul 4, 2023
2 parents e710d69 + b9b92d4 commit 5dfad95
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
2 changes: 0 additions & 2 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import useFormatResult from './useFormatResult'
import useFetchs from './useFetchs'
import useFullscreen from './useFullscreen'
import useHover from './useHover'
// import usePreview from './usePreview'
import usePrevious from './usePrevious'
import useInterval from './useInterval'
import useInfiniteScroll from './useInfiniteScroll'
Expand Down Expand Up @@ -82,7 +81,6 @@ export {
useMedia,
useMouse,
useNetwork,
// usePreview,
usePrevious,
useSessionStorageState,
useSet,
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/useDarkMode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function useDarkMode(): [ComputedRef<boolean>, (value?: unknown)
})

watchEffect(() => {
const className = 'dark-mode'
const className = '[vue-hooks-plus]-dark-mode'
const element = window.document.body
if (enabled.value) {
element.classList.add(className)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<vhp-button type="button" @click="count++">count is {{ count }}</vhp-button>
<div style="opacity: 0.6;"> count !==0 and count !==5 ready is true </div>
</div>
<br>
<br />
<vhp-button @click="() => (id = 1)">Change ID = 1</vhp-button>
<vhp-button @click="() => (id = 2)" style="margin-left: 16px;">Change ID = 2</vhp-button>
<vhp-button @click="() => (store.id = 1)" style="margin-left: 16px;">
Expand Down
1 change: 0 additions & 1 deletion packages/hooks/src/useRequest/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import useRequest from './useRequest'
import useRequestProvider from './useRequestProvider'
// import createUseRequest from './createUseRequest'
import { clearCache as clearUseRequestCache } from './utils/cache'

import useRequestDevToolsPlugin from './devtools'
Expand Down
9 changes: 6 additions & 3 deletions packages/hooks/src/useRequest/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, WatchSource } from 'vue'
import { Ref, watch } from 'vue'
import { CachedData } from './utils/cache'
import UseRequestFetch from './Fetch'

Expand Down Expand Up @@ -97,7 +97,7 @@ export interface UseRequestBasicOptions<TData, TParams extends unknown[]> {
/**
* Dependent on responsive objects, and the `watch` incoming listener object usage for `vue`.
*/
refreshDeps?: WatchSource[] | boolean
refreshDeps?: Parameters<typeof watch>[0][] | boolean
refreshDepsAction?: () => void

/**
Expand All @@ -106,10 +106,13 @@ export interface UseRequestBasicOptions<TData, TParams extends unknown[]> {
*/
loadingDelay?: number | Ref<number>

/**
* open vue devtools,devKey must existence and uniqueness.
*/
devKey?: string

/**
* Format the request results, which recommend to use `useFormatResult`
* Format the request results, which recommend to use `useFormatResult`.
* @param data TData
* @returns unknown need cover TData
*/
Expand Down
12 changes: 5 additions & 7 deletions packages/hooks/src/useRequest/useRequestImplement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref, reactive, toRefs, onUnmounted, inject, UnwrapRef, watchEffect, computed, isRef, onMounted, unref } from 'vue'
import { ref, reactive, toRefs, onUnmounted, inject, UnwrapRef, watchEffect, computed, isRef, unref } from 'vue'

import Fetch from './Fetch'
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
Expand Down Expand Up @@ -93,12 +93,10 @@ function useRequestImplement<TData, TParams extends any[]>(
})

// manual
onMounted(() => {
if (!manual && fetchInstance.options.refreshDeps !== true) {
const params = fetchInstance.state.params || options.defaultParams || []
if (unref(ready)) fetchInstance.run(...(params as TParams))
}
})
if (!manual && fetchInstance.options.refreshDeps !== true) {
const params = fetchInstance.state.params || options.defaultParams || []
if (unref(ready)) fetchInstance.run(...(params as TParams))
}

// onUnmounted cancel request
onUnmounted(() => {
Expand Down

0 comments on commit 5dfad95

Please sign in to comment.