mirror of https://github.com/msgbyte/tailchat
refactor: 缓存管理与redux增加好友
parent
298121fcb8
commit
bb88176b72
@ -0,0 +1,15 @@
|
||||
import React from 'react';
|
||||
import { QueryClientProvider } from 'react-query';
|
||||
import { queryClient } from './';
|
||||
|
||||
/**
|
||||
* 缓存上下文
|
||||
*/
|
||||
export const CacheProvider: React.FC = React.memo((props) => {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{props.children}
|
||||
</QueryClientProvider>
|
||||
);
|
||||
});
|
||||
CacheProvider.displayName = 'CacheProvider';
|
@ -0,0 +1,20 @@
|
||||
import { fetchUserInfo, UserBaseInfo } from '../model/user';
|
||||
import { queryClient } from './index';
|
||||
|
||||
function buildCacheFactory<T>(
|
||||
scope: string,
|
||||
fetcher: (id: string) => Promise<T>
|
||||
) {
|
||||
return async (id: string): Promise<T> => {
|
||||
const data = await queryClient.fetchQuery([scope, id], () => fetcher(id));
|
||||
return data;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的用户信息
|
||||
*/
|
||||
export const getCachedUserInfo = buildCacheFactory<UserBaseInfo>(
|
||||
'user',
|
||||
fetchUserInfo
|
||||
);
|
@ -0,0 +1,5 @@
|
||||
import { QueryClient } from 'react-query';
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
export { queryClient };
|
@ -1,13 +1,7 @@
|
||||
import React from 'react';
|
||||
import { QueryClient, QueryClientProvider } from 'react-query';
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
import { CacheProvider } from '../cache/Provider';
|
||||
|
||||
export const PawProvider: React.FC = React.memo((props) => {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{props.children}
|
||||
</QueryClientProvider>
|
||||
);
|
||||
return <CacheProvider>{props.children}</CacheProvider>;
|
||||
});
|
||||
PawProvider.displayName = 'PawProvider';
|
||||
|
Loading…
Reference in New Issue