Skip to content

Commit e266e7e

Browse files
committed
feat: use-request image v1.3.0
1 parent 4748169 commit e266e7e

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

packages/use-request/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vue-hooks-plus/use-request",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Vue use-request hooks library",
55
"files": [
66
"dist",

packages/use-request/src/Fetch.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ref, unref, watchEffect } from 'vue'
1+
import { Ref } from 'vue'
22
import {
33
UseRequestFetchState,
44
UseRequestOptions,
@@ -107,7 +107,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
107107
)
108108
// 是否停止请求
109109
if (stopNow) {
110-
return new Promise(() => {})
110+
return new Promise(() => { })
111111
}
112112

113113
this.setState({
@@ -131,7 +131,7 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
131131
const requestReturnResponse = (res: any) => {
132132
// 取消了请求,count将与currentCount不一致,将发送空请求
133133
if (currentCount !== this.count) {
134-
return new Promise(() => {})
134+
return new Promise(() => { })
135135
}
136136
// 格式化数据
137137
const formattedResult = this.options.formatResult ? this.options.formatResult(res) : res
@@ -158,24 +158,26 @@ export default class Fetch<TData, TParams extends unknown[] = any> {
158158

159159
if (!servicePromise) {
160160
/** 自动依赖收集 */
161-
if (!this.options.manual && this.options.refreshDeps === true) {
162-
watchEffect(async () => {
163-
if (unref(this.options.ready)) {
164-
this.setFetchState(true, 'loading')
165-
servicePromise = this.serviceRef.value(...params)
166-
const servicePromiseResult = await servicePromise
167-
return requestReturnResponse(servicePromiseResult)
168-
}
169-
})
170-
} else {
171-
servicePromise = this.serviceRef.value(...params)
172-
}
161+
// if (!this.options.manual && this.options.refreshDeps === true) {
162+
// watchEffect(async () => {
163+
// if (unref(this.options.ready)) {
164+
// this.setFetchState(true, 'loading')
165+
// servicePromise = this.serviceRef.value(...params)
166+
// const servicePromiseResult = await servicePromise
167+
// return requestReturnResponse(servicePromiseResult)
168+
// }
169+
// })
170+
// } else {
171+
// servicePromise = this.serviceRef.value(...params)
172+
// }
173+
servicePromise = this.serviceRef.value(...params)
173174
}
175+
// servicePromise = this.serviceRef.value(...params)
174176
const servicePromiseResult = await servicePromise
175177
return requestReturnResponse(servicePromiseResult)
176178
} catch (error) {
177179
if (currentCount !== this.count) {
178-
return new Promise(() => {})
180+
return new Promise(() => { })
179181
}
180182

181183
this.setState({

packages/use-request/src/plugins/usePollingPlugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
1010
const timerRef = ref<Interval>();
1111
const unsubscribeRef = ref<() => void>();
1212
const countRef = ref<number>(0);
13-
13+
14+
1415

1516
const stopPolling = () => {
1617
if (timerRef.value) {
@@ -33,7 +34,7 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
3334
onBefore: () => {
3435
stopPolling();
3536
},
36-
onError: () => {
37+
onError: () => {
3738
countRef.value += 1;
3839
},
3940
onSuccess: () => {

packages/use-request/src/useRequestImplement.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, reactive, toRefs, onMounted, onUnmounted, unref, inject, UnwrapRef } from 'vue'
1+
import { ref, reactive, toRefs, onUnmounted, inject, UnwrapRef, watchEffect, computed, isRef, onMounted, unref } from 'vue'
22

33
import Fetch from './Fetch'
44
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
@@ -89,6 +89,27 @@ function useRequestImplement<TData, TParams extends any[]>(
8989
return p(fetchInstance, fetchOptions)
9090
})
9191

92+
const readyComputed = computed(() => isRef(ready) ? ready.value : ready)
93+
94+
// const isMount = ref(false)
95+
96+
watchEffect(() => {
97+
if (!manual) {
98+
const params = fetchInstance.state.params || options.defaultParams || []
99+
// if (readyComputed.value && !isMount.value) {
100+
// fetchInstance.run(...(params as TParams))
101+
// // 模拟首次mount
102+
// isMount.value = true
103+
104+
// }
105+
// 自动收集依赖
106+
if (readyComputed.value && fetchInstance.options.refreshDeps === true && !!serviceRef.value) {
107+
console.log("运行1");
108+
fetchInstance.run(...(params as TParams))
109+
}
110+
}
111+
})
112+
92113
// manual
93114
onMounted(() => {
94115
if (!manual) {

0 commit comments

Comments
 (0)