Skip to content

Commit e64e31e

Browse files
committed
refactor: optimize the onBefore hook in custom plugins #226
Add `isReturn` to handle situations where the request needs to be aborted and custom data returned.
1 parent 243743c commit e64e31e

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

src/core/createQuery.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const setStateBind = <T extends StateBindParams>(
1919
) => {
2020
return (newState: Partial<UnWrapRefObject<StateBindParams>>) => {
2121
Object.keys(newState).forEach(key => {
22-
oldState[key].value = newState[key];
22+
if (oldState[key]) {
23+
oldState[key].value = newState[key];
24+
}
2325
});
2426
publicCb.forEach(fun => fun(oldState));
2527
};
@@ -80,18 +82,31 @@ const createQuery = <R, P extends unknown[]>(
8082

8183
context.runAsync = async (...args: P): Promise<R> => {
8284
setState({
83-
loading: true,
84-
params: args,
8585
status: 'pending',
8686
});
8787

8888
count.value += 1;
8989
const currentCount = count.value;
9090

91-
const { isBreak, breakResult = resolvedPromise() } = emit('onBefore', args);
91+
const {
92+
isBreak = false,
93+
isReturn = false,
94+
...rest
95+
} = emit('onBefore', args);
9296
if (isBreak) {
9397
setState({ status: 'settled' });
94-
return breakResult;
98+
return resolvedPromise();
99+
}
100+
101+
setState({
102+
loading: true,
103+
params: args,
104+
...rest,
105+
});
106+
107+
if (isReturn) {
108+
setState({ status: 'settled', loading: false });
109+
return rest.data!;
95110
}
96111

97112
onBefore?.(args);

src/core/plugins/useCachePlugin.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ export default definePlugin(
8282
}
8383
// If it's fresh, stop the request
8484
if (isFresh(cache.time)) {
85-
queryInstance.data.value = cache.data;
86-
queryInstance.loading.value = false;
8785
return {
88-
isBreak: true,
89-
breakResult: cache.data,
86+
isReturn: true,
87+
loading: false,
88+
data: cache.data,
9089
};
9190
} else {
9291
// If it is not fresh, set data and request
93-
queryInstance.data.value = cache.data;
92+
return {
93+
data: cache.data,
94+
};
9495
}
9596
},
9697
onQuery(service) {

src/core/plugins/useLoadingDelayPlugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export default definePlugin(
4949
};
5050
return {
5151
onBefore() {
52-
queryInstance.loading.value = !loadingDelayRef.value;
5352
delayLoadingTimer.value();
5453
delayLoadingTimer.value = delayLoading();
5554
startTime = getCurrentTime();
55+
return {
56+
loading: !loadingDelayRef.value,
57+
};
5658
},
5759
onQuery(service) {
5860
if (!loadingKeepRef.value) return () => service();

src/core/plugins/useReadyPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export default definePlugin(
2121
onBefore() {
2222
const readyFlag = isFunction(ready) ? ready() : ready.value;
2323
if (!readyFlag) {
24-
queryInstance.loading.value = false;
2524
return {
2625
isBreak: true,
2726
};

src/core/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Ref, WatchSource } from 'vue-demi';
44

55
import type { PaginationExtendsOption } from '../usePagination';
66
import type { CacheData } from './utils/cache';
7-
import type { EmitVoid } from './utils/types';
7+
import type { EmitVoid, UnWrapRefObject } from './utils/types';
88

99
type MutateData<R> = (newData: R) => void;
1010
type MutateFunction<R> = (arg: (oldData: R) => R) => void;
@@ -91,10 +91,12 @@ export type PluginImplementType<R, P extends any[]> = {
9191
};
9292

9393
export type PluginType<R, P extends unknown[]> = {
94-
onBefore: (params: P) => {
95-
isBreak?: Boolean;
96-
breakResult?: any;
97-
} | void;
94+
onBefore: (params: P) =>
95+
| ({
96+
isBreak?: Boolean;
97+
isReturn?: Boolean;
98+
} & Partial<UnWrapRefObject<State<R, P>>>)
99+
| void;
98100

99101
onQuery: (service: () => Promise<R>) => () => Promise<R>;
100102

0 commit comments

Comments
 (0)