Skip to content

Commit

Permalink
refactor: optimize the onBefore hook in custom plugins #226
Browse files Browse the repository at this point in the history
Add `isReturn` to handle situations where the request needs to be aborted and custom data returned.
  • Loading branch information
John60676 committed Jan 10, 2024
1 parent 243743c commit e64e31e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
25 changes: 20 additions & 5 deletions src/core/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const setStateBind = <T extends StateBindParams>(
) => {
return (newState: Partial<UnWrapRefObject<StateBindParams>>) => {
Object.keys(newState).forEach(key => {
oldState[key].value = newState[key];
if (oldState[key]) {
oldState[key].value = newState[key];
}
});
publicCb.forEach(fun => fun(oldState));
};
Expand Down Expand Up @@ -80,18 +82,31 @@ const createQuery = <R, P extends unknown[]>(

context.runAsync = async (...args: P): Promise<R> => {
setState({
loading: true,
params: args,
status: 'pending',
});

count.value += 1;
const currentCount = count.value;

const { isBreak, breakResult = resolvedPromise() } = emit('onBefore', args);
const {
isBreak = false,
isReturn = false,
...rest
} = emit('onBefore', args);
if (isBreak) {
setState({ status: 'settled' });
return breakResult;
return resolvedPromise();
}

setState({
loading: true,
params: args,
...rest,
});

if (isReturn) {
setState({ status: 'settled', loading: false });
return rest.data!;
}

onBefore?.(args);
Expand Down
11 changes: 6 additions & 5 deletions src/core/plugins/useCachePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ export default definePlugin(
}
// If it's fresh, stop the request
if (isFresh(cache.time)) {
queryInstance.data.value = cache.data;
queryInstance.loading.value = false;
return {
isBreak: true,
breakResult: cache.data,
isReturn: true,
loading: false,
data: cache.data,
};
} else {
// If it is not fresh, set data and request
queryInstance.data.value = cache.data;
return {
data: cache.data,
};
}
},
onQuery(service) {
Expand Down
4 changes: 3 additions & 1 deletion src/core/plugins/useLoadingDelayPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ export default definePlugin(
};
return {
onBefore() {
queryInstance.loading.value = !loadingDelayRef.value;
delayLoadingTimer.value();
delayLoadingTimer.value = delayLoading();
startTime = getCurrentTime();
return {
loading: !loadingDelayRef.value,
};
},
onQuery(service) {
if (!loadingKeepRef.value) return () => service();
Expand Down
1 change: 0 additions & 1 deletion src/core/plugins/useReadyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default definePlugin(
onBefore() {
const readyFlag = isFunction(ready) ? ready() : ready.value;
if (!readyFlag) {
queryInstance.loading.value = false;
return {
isBreak: true,
};
Expand Down
12 changes: 7 additions & 5 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Ref, WatchSource } from 'vue-demi';

import type { PaginationExtendsOption } from '../usePagination';
import type { CacheData } from './utils/cache';
import type { EmitVoid } from './utils/types';
import type { EmitVoid, UnWrapRefObject } from './utils/types';

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

export type PluginType<R, P extends unknown[]> = {
onBefore: (params: P) => {
isBreak?: Boolean;
breakResult?: any;
} | void;
onBefore: (params: P) =>
| ({
isBreak?: Boolean;
isReturn?: Boolean;
} & Partial<UnWrapRefObject<State<R, P>>>)
| void;

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

Expand Down

0 comments on commit e64e31e

Please sign in to comment.