Skip to content

Commit

Permalink
fix: new method key in cloned method instance
Browse files Browse the repository at this point in the history
  • Loading branch information
JOU-amjs committed Mar 28, 2024
1 parent f8cc3ac commit 4c32b81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/functions/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,13 @@ export default function sendRequest<S, E, R, T, RC, RE, RH>(
requestAdapterCtrlsPromiseResolveFn = resolve;
}) as Promise<RequestAdapterReturnType | undefined>,
response = async () => {
const {
beforeRequest = noop,
responsed,
responded,
requestAdapter,
cacheLogger = defaultCacheLogger
} = getOptions(methodInstance),
// 使用克隆之前的method key,以免在beforeRequest中method被改动而导致method key改变
// method key在beforeRequest中被改变将会致使使用method 实例操作缓存时匹配失败
methodKey = getMethodInternalKey(methodInstance),
const { beforeRequest = noop, responsed, responded, requestAdapter, cacheLogger } = getOptions(methodInstance),
// 使用克隆的methodKey,防止用户使用克隆的method实例再次发起请求,导致key重复
clonedMethod = cloneMethod(methodInstance),
methodKey = getMethodInternalKey(clonedMethod),
{ e: expireMilliseconds, s: toStorage, t: tag, m: cacheMode } = getLocalCacheConfigParam(methodInstance),
{ id, storage } = getContext(methodInstance),
// 获取受控缓存或非受控缓存
clonedMethod = cloneMethod(methodInstance),
{ localCache } = getConfig(methodInstance);

// 如果当前method设置了受控缓存,则看是否有自定义的数据
Expand Down Expand Up @@ -219,8 +212,14 @@ export default function sendRequest<S, E, R, T, RC, RE, RH>(
return promiseFinally(
promiseThen(
PromiseCls.all([requestAdapterCtrls.response(), requestAdapterCtrls.headers()]),
([rawResponse, headers]) => handleResponseTask(responseSuccessHandler(rawResponse, clonedMethod), headers),
([rawResponse, headers]) => {
// 无论请求成功、失败,都需要首先移除共享的请求
deleteAttr(namespacedAdapterReturnMap, methodKey);
return handleResponseTask(responseSuccessHandler(rawResponse, clonedMethod), headers);
},
(error: any) => {
// 无论请求成功、失败,都需要首先移除共享的请求
deleteAttr(namespacedAdapterReturnMap, methodKey);
if (!isFn(responseErrorHandler)) {
throw error;
}
Expand All @@ -229,8 +228,6 @@ export default function sendRequest<S, E, R, T, RC, RE, RH>(
}
),
() => {
// 请求成功、失败,以及在成功后处理报错,都需要移除共享的请求
deleteAttr(namespacedAdapterReturnMap, methodKey);
responseCompleteHandler(clonedMethod);
}
);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/cloneMethod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Method from '@/Method';
import { getContext, newInstance } from './helper';
import { getContext, getMethodInternalKey, newInstance } from './helper';
import { ObjectCls } from './variables';

export default <S, E, R, T, RC, RE, RH>(methodInstance: Method<S, E, R, T, RC, RE, RH>) => {
Expand All @@ -20,6 +20,7 @@ export default <S, E, R, T, RC, RE, RH>(methodInstance: Method<S, E, R, T, RC, R

ObjectCls.assign(newMethod, {
...methodInstance,
__key__: getMethodInternalKey(newMethod),
config: newConfig
});
return newMethod;
Expand Down

0 comments on commit 4c32b81

Please sign in to comment.