Skip to content

Commit

Permalink
Merge branch 'master' into ts/use-set-state
Browse files Browse the repository at this point in the history
  • Loading branch information
awmleer authored Dec 21, 2021
2 parents 8077f18 + 0a8b87c commit 0ab66b4
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 17 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ English | [简体中文](https://github.com/alibaba/hooks/blob/master/README.zh-

</div>

> :warning: This is v3 branch, find v2 from <a href="https://github.com/alibaba/hooks/tree/release/v2.x" target="_blank">https://github.com/alibaba/hooks/tree/release/v2.x</a>
## 📚 Documentation

- [English](https://ahooks.js.org/)
Expand Down
2 changes: 0 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

</div>

> :warning: 当前是 v3 版本,如果你想找 v2 版本,去这里 <a href="https://github.com/alibaba/hooks/tree/release/v2.x" target="_blank">https://github.com/alibaba/hooks/tree/release/v2.x</a>
## 📚 文档

- [English](https://ahooks.js.org/)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.6",
"version": "3.0.7",
"packages": ["packages/*"],
"npmClient": "yarn",
"command": {
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ahooks",
"version": "3.0.6",
"version": "3.0.7",
"description": "react hooks library",
"keywords": [
"ahooks",
Expand Down
11 changes: 7 additions & 4 deletions packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const useCachePlugin: Plugin<any, any[]> = (
cacheSubscribe.trigger(key, cachedData.data);
};

const _getCache = (key: string) => {
const _getCache = (key: string, params: any[] = []) => {
if (customGetCache) {
return customGetCache();
return customGetCache(params);
}
return cache.getCache(key);
};
Expand All @@ -47,6 +47,9 @@ const useCachePlugin: Plugin<any, any[]> = (
if (cacheData && Object.hasOwnProperty.call(cacheData, 'data')) {
fetchInstance.state.data = cacheData.data;
fetchInstance.state.params = cacheData.params;
if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) {
fetchInstance.state.loading = false;
}
}

// subscribe same cachekey update, trigger update
Expand All @@ -64,8 +67,8 @@ const useCachePlugin: Plugin<any, any[]> = (
}

return {
onBefore: () => {
const cacheData = _getCache(cacheKey);
onBefore: (params) => {
const cacheData = _getCache(cacheKey, params);

if (!cacheData || !Object.hasOwnProperty.call(cacheData, 'data')) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/useRequest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface Options<TData, TParams extends any[]> {
cacheTime?: number;
staleTime?: number;
setCache?: (data: CachedData<TData, TParams>) => void;
getCache?: () => CachedData<TData, TParams> | undefined;
getCache?: (params: TParams) => CachedData<TData, TParams> | undefined;

// retry
retryCount?: number;
Expand Down
13 changes: 10 additions & 3 deletions packages/hooks/src/useRequest/src/utils/cachePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ const setCachePromise = (cacheKey: CachedKey, promise: Promise<any>) => {
// Because the promise.finally will change the reference of the promise
cachePromise.set(cacheKey, promise);

promise.finally(() => {
cachePromise.delete(cacheKey);
});
// no use promise.finally for compatibility
promise
.then((res) => {
cachePromise.delete(cacheKey);
return res;
})
.catch((err) => {
cachePromise.delete(cacheKey);
throw err;
});
};

export { getCachePromise, setCachePromise };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isBrowser from '../utils/isBrowser';
import isBrowser from './isBrowser';
import useEffectWithTarget from './useEffectWithTarget';
import useLayoutEffectWithTarget from './useLayoutEffectWithTarget';

Expand Down
4 changes: 2 additions & 2 deletions packages/use-url-state/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ahooksjs/use-url-state",
"version": "3.0.6",
"version": "3.0.7",
"description": "A hook that stores the state into url query parameters.",
"main": "./lib/index.js",
"module": "./es/index.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"react-router": "^5.0.0 || ^6.0.0"
},
"dependencies": {
"ahooks": "^3.0.6",
"ahooks": "^3.0.7",
"query-string": "^6.9.0"
}
}

0 comments on commit 0ab66b4

Please sign in to comment.