Skip to content

Commit 8697fca

Browse files
authored
feat: add client side watch (#80)
Add client side `refresh` and `execute`, watch options.
1 parent 8b31990 commit 8697fca

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

playground/pages/test-reactive.vue

+17-19
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
点击加载
88
</button>
99
<button
10-
@click="async () => {
11-
_refresh()
12-
console.log('重置')
13-
}"
10+
@click="refresh"
1411
>
1512
重置
1613
</button>
@@ -24,20 +21,22 @@ import * as API from '../api'
2421
const page = ref(1)
2522
const list = ref<number[]>([])
2623
const num = ref<number>()
27-
let _refresh = () => {}
28-
const getList = async () => {
29-
const { data, refresh, pending, error, status } = await API.getListReactive(page)
30-
watch(() => data.value, () => {
31-
if (page.value === 1) {
32-
list.value = data.value?.data || []
33-
}
34-
else {
35-
list.value = list.value.concat(data.value?.data || [])
36-
}
37-
})
38-
_refresh = refresh
39-
console.log(data.value, pending.value, error.value, status.value, 'data =====>')
40-
}
24+
const { data, refresh, pending, error, status } = await API.getListReactive(page)
25+
26+
watch(() => data.value, async () => {
27+
await nextTick()
28+
console.log(data.value)
29+
30+
if (page.value === 1) {
31+
list.value = data.value?.data || []
32+
}
33+
else {
34+
list.value = list.value.concat(data.value?.data || [])
35+
}
36+
}, {
37+
immediate: true
38+
})
39+
console.log(data.value, pending.value, error.value, status.value, 'data =====>')
4140
4241
const getNum = async () => {
4342
const { data, refresh, pending, error, status } = await API.getNum(page.value)
@@ -48,7 +47,6 @@ const getNum = async () => {
4847
}
4948
}
5049
51-
await getList()
5250
await getNum()
5351
5452
onMounted(async () => {

src/runtime/ajax.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { MaybeRef, Ref } from '#imports'
22
import type { NitroFetchRequest } from 'nitro'
33
import type { AsyncData, AsyncDataOptions, AsyncDataRequestStatus, NuxtError } from 'nuxt/app'
44
import type { FetchContext, FetchMethod, FetchResponse, HTTPConfig, Interceptors, KeysOf, PickFrom } from './type'
5-
import { createError, reactive, ref, toValue, unref, useAsyncData, useNuxtApp } from '#imports'
5+
import { createError, getCurrentScope, onScopeDispose, reactive, ref, toValue, unref, useAsyncData, useNuxtApp, useRequestFetch, watch } from '#imports'
66
import { hash, serialize } from 'ohash'
77

88
type CustomFetchReturnValue<DataT, ErrorT> = AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>
@@ -171,7 +171,7 @@ export class CustomFetch {
171171
_cachedController.get(key)?.abort?.()
172172
}
173173

174-
return $fetch(url as string, {
174+
return useRequestFetch()(url as string, {
175175
signal: _cachedController.get(key)?.signal,
176176
...defaultOptions,
177177
..._config
@@ -192,6 +192,8 @@ export class CustomFetch {
192192
pending: Ref<boolean>
193193
error: Ref<(ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>
194194
status: Ref<AsyncDataRequestStatus>
195+
refresh?: () => Promise<DataT>
196+
execute?: () => Promise<DataT>
195197
} = {
196198
data: ref(null),
197199
pending: ref(true),
@@ -235,6 +237,18 @@ export class CustomFetch {
235237
asyncData.pending.value = false
236238
_cachedController.delete(key)
237239
})
240+
241+
asyncData.refresh = asyncData.execute = () => _handler().then(result => asyncData.data.value = result)
242+
const hasScope = getCurrentScope()
243+
if (options.watch) {
244+
const unsub = watch(options.watch, async () => {
245+
asyncData.refresh!()
246+
})
247+
if (hasScope) {
248+
onScopeDispose(unsub)
249+
}
250+
}
251+
238252
return promise as any
239253
}
240254

0 commit comments

Comments
 (0)