feat: request 添加merge类型

This commit is contained in:
winixt 2021-05-24 15:53:59 +08:00
parent 3f40ebe08b
commit bb2f25f644
6 changed files with 86 additions and 55 deletions

View File

@ -64,6 +64,9 @@ function setCacheData({
data,
cacheTime = 1000 * 60 * 3
}) {
// merge 类型没有缓存
if (cacheType === CACHE_TYPE.merge) return;
const _key = genInnerKey(key, cacheType);
const currentCacheData = {
@ -97,6 +100,9 @@ function isExpire({ expire, cacheTime }) {
}
function getCacheData({ key, cacheType = 'ram' }) {
// merge 类型没有缓存
if (cacheType === CACHE_TYPE.merge) return;
const _key = genInnerKey(key, cacheType);
if (cacheType !== CACHE_TYPE.ram) {
const cacheInstance = window[CACHE_TYPE[cacheType]];
@ -144,15 +150,14 @@ function handleCachingStart(ctx, config) {
}
// 有请求成功的
function handleCachingQueueSuccess(ctx, config, data) {
function handleCachingQueueSuccess(ctx, config) {
// 移除首次缓存 flag
const _key = genInnerKey(ctx.key, config.cache.cacheType);
const queue = cachingQueue.get(_key);
if (queue && queue.length > 0) {
queue.forEach((resolve) => {
resolve({
isSuccess: true,
data
response: ctx.response
});
});
}
@ -165,23 +170,26 @@ function handleCachingQueueError(ctx, config) {
const _key = genInnerKey(ctx.key, config.cache.cacheType);
const queue = cachingQueue.get(_key);
if (queue && queue.length > 0) {
const firstResolve = queue.shift();
firstResolve({
isSuccess: false
});
cachingQueue.set(_key, queue);
// 非 merge 类型,进行队列重试,直到有一个请求成功,后面的全部成功
if (config.cache.cacheType !== CACHE_TYPE.merge) {
const firstResolve = queue.shift();
firstResolve();
cachingQueue.set(_key, queue);
} else {
queue.forEach((resolve) => {
resolve({
error: ctx.error
});
});
cachingQueue.delete(_key);
cacheStartFlag.delete(_key);
}
} else {
cachingQueue.delete(_key);
cacheStartFlag.delete(_key);
}
}
/**
* TODO 添加一种 merge 类型
* merge 当期所有请求按顺序发起请求只要有一个成功所有请求都成功并且跳过后续的请求否则所有请求都失败
* merge 错误提示
*/
export default async (ctx, next) => {
const { config } = ctx;
if (config.cache) {
@ -193,10 +201,10 @@ export default async (ctx, next) => {
return;
}
const result = await handleCachingStart(ctx, config);
if (result && result.isSuccess) {
ctx.response = {
data: result.data
};
if (result) {
Object.keys(result).forEach((key) => {
ctx[key] = result[key];
});
return;
}
}
@ -205,15 +213,13 @@ export default async (ctx, next) => {
if (config.cache) {
const requestdata = checkHttpRequestHasBody(config.method) ? config.data : config.params;
if (!ctx.error && ctx.response && canCache(requestdata) && canCache(ctx.response.data)) {
handleCachingQueueSuccess(ctx, config, ctx.response.data);
handleCachingQueueSuccess(ctx, config);
if (config.cache.cacheType !== CACHE_TYPE.merge) {
setCacheData({
key: ctx.key,
data: ctx.response.data,
...config.cache
});
}
setCacheData({
key: ctx.key,
data: ctx.response.data,
...config.cache
});
} else {
handleCachingQueueError(ctx, config);
}

View File

@ -58,7 +58,7 @@
"less-loader": "^8.0.0",
"mini-css-extract-plugin": "^1.3.5",
"mockjs": "^1.1.0",
"postcss": "^8.2.4",
"postcss": "8.3.0",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-loader": "^4.2.0",
"postcss-safe-parser": "^5.0.2",
@ -72,4 +72,4 @@
"webpack-dev-server": "^3.11.2",
"webpackbar": "^5.0.0-3"
}
}
}

View File

@ -1,5 +1,5 @@
// fes.config.js 只负责管理 cli 相关的配置
import pxtoviewport from 'postcss-px-to-viewport';
import pxtoviewport from '@ttou/postcss-px-to-viewport';
export default {

View File

@ -42,7 +42,7 @@
},
"devDependencies": {
"@webank/eslint-config-webank": "0.3.0",
"postcss-px-to-viewport": "1.1.1"
"@ttou/postcss-px-to-viewport": "1.1.4"
},
"dependencies": {
"debounce": "1.2.1",

View File

@ -22,6 +22,9 @@ export default {
const clickIcon = () => {
console.log('click Icon');
};
request('/api', null).then((res) => {
console.log(res);
});
// request('/api', null, {
// throttle: 3000,
// cache: true
@ -54,28 +57,34 @@ export default {
// }, 3200);
// request('/api', null, {
// cache: true
// cache: {
// cacheType: 'merge'
// }
// }).then((res) => {
// console.log(res);
// });
// request('/api', null, {
// cache: true
// cache: {
// cacheType: 'merge'
// }
// }).then((res) => {
// console.log(res);
// });
// request('/api', null, {
// cache: true
// cache: {
// cacheType: 'merge'
// }
// }).then((res) => {
// console.log(res);
// });
request('/api', null, {
// skipErrorHandler: [500]
}).then((res) => {
console.log(res);
}).catch((err) => {
console.log('inner error', err);
});
// request('/api', null, {
// // skipErrorHandler: [500]
// }).then((res) => {
// console.log(res);
// }).catch((err) => {
// console.log('inner error', err);
// });
return {
fes,
rotate,

View File

@ -3514,6 +3514,11 @@
dependencies:
defer-to-connect "^1.0.1"
"@ttou/postcss-px-to-viewport@1.1.4":
version "1.1.4"
resolved "https://registry.yarnpkg.com/@ttou/postcss-px-to-viewport/-/postcss-px-to-viewport-1.1.4.tgz#cecebd6d8a8ab19aafde271fc5115fcf3e8d1909"
integrity sha512-Y6sQx0IR43V17qGMAeZmVcOwi3/piKYzTo5QgNshLd8LO7QH9ZR2VQhf3LobdCg4H/hWFBpMVTAjcw+tBEKPiA==
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
version "7.1.12"
resolved "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.12.tgz#4d8e9e51eb265552a7e4f1ff2219ab6133bdfb2d"
@ -12254,6 +12259,11 @@ nanoid@^3.1.20:
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.1.20.tgz#badc263c6b1dcf14b71efaa85f6ab4c1d6cfc788"
integrity sha512-a1cQNyczgKbLX9jwbS/+d7W8fX/RfgYR7lVWwWOGIPNgK2m0MWvrGF6/m4kk6U3QcFMnZf3RIhL0v2Jgh/0Uxw==
nanoid@^3.1.23:
version "3.1.23"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.23.tgz#f744086ce7c2bc47ee0a8472574d5c78e4183a81"
integrity sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw==
nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
@ -12566,7 +12576,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
object-assign@>=4.0.1, object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0:
version "4.1.1"
resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
@ -13645,14 +13655,6 @@ postcss-ordered-values@^4.1.2:
postcss "^7.0.0"
postcss-value-parser "^3.0.0"
postcss-px-to-viewport@1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/postcss-px-to-viewport/-/postcss-px-to-viewport-1.1.1.tgz#a25ca410b553c9892cc8b525cc710da47bf1aa55"
integrity sha512-2x9oGnBms+e0cYtBJOZdlwrFg/mLR4P1g2IFu7jYKvnqnH/HLhoKyareW2Q/x4sg0BgklHlP1qeWo2oCyPm8FQ==
dependencies:
object-assign ">=4.0.1"
postcss ">=5.0.2"
postcss-reduce-initial@^4.0.3:
version "4.0.3"
resolved "https://registry.npmjs.org/postcss-reduce-initial/-/postcss-reduce-initial-4.0.3.tgz#7fd42ebea5e9c814609639e2c2e84ae270ba48df"
@ -13737,14 +13739,14 @@ postcss@6.0.1:
source-map "^0.5.6"
supports-color "^3.2.3"
postcss@>=5.0.2, postcss@^8.0.0, postcss@^8.1.0, postcss@^8.1.10, postcss@^8.2.4, postcss@^8.2.5, postcss@^8.2.6:
version "8.2.7"
resolved "https://registry.npmjs.org/postcss/-/postcss-8.2.7.tgz#48ed8d88b4de10afa0dfd1c3f840aa57b55c4d47"
integrity sha512-DsVLH3xJzut+VT+rYr0mtvOtpTjSyqDwPf5EZWXcb0uAKfitGpTY9Ec+afi2+TgdN8rWS9Cs88UDYehKo/RvOw==
postcss@8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.3.0.tgz#b1a713f6172ca427e3f05ef1303de8b65683325f"
integrity sha512-+ogXpdAjWGa+fdYY5BQ96V/6tAo+TdSSIMP5huJBIygdWwKtVoB5JWZ7yUd4xZ8r+8Kvvx4nyg/PQ071H4UtcQ==
dependencies:
colorette "^1.2.2"
nanoid "^3.1.20"
source-map "^0.6.1"
nanoid "^3.1.23"
source-map-js "^0.6.2"
postcss@^6.0.1:
version "6.0.23"
@ -13764,6 +13766,15 @@ postcss@^7.0.0, postcss@^7.0.1, postcss@^7.0.16, postcss@^7.0.27:
source-map "^0.6.1"
supports-color "^6.1.0"
postcss@^8.0.0, postcss@^8.1.0, postcss@^8.1.10, postcss@^8.2.5, postcss@^8.2.6:
version "8.2.7"
resolved "https://registry.npmjs.org/postcss/-/postcss-8.2.7.tgz#48ed8d88b4de10afa0dfd1c3f840aa57b55c4d47"
integrity sha512-DsVLH3xJzut+VT+rYr0mtvOtpTjSyqDwPf5EZWXcb0uAKfitGpTY9Ec+afi2+TgdN8rWS9Cs88UDYehKo/RvOw==
dependencies:
colorette "^1.2.2"
nanoid "^3.1.20"
source-map "^0.6.1"
preact@^10.0.0:
version "10.5.12"
resolved "https://registry.npmjs.org/preact/-/preact-10.5.12.tgz#6a8ee8bf40a695c505df9abebacd924e4dd37704"
@ -15214,6 +15225,11 @@ source-list-map@^2.0.0, source-list-map@^2.0.1:
resolved "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz#3993bd873bfc48479cca9ea3a547835c7c154b34"
integrity sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==
source-map-js@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-0.6.2.tgz#0bb5de631b41cfbda6cfba8bd05a80efdfd2385e"
integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==
source-map-resolve@^0.5.0, source-map-resolve@^0.5.2:
version "0.5.3"
resolved "https://registry.npmjs.org/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a"