Skip to content

Commit

Permalink
feat: alita@2 keep alive support runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni authored Apr 12, 2022
2 parents 9c2cd4c + 9d5cb76 commit 23c838b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.8.32",
"version": "2.8.32-beta.1",
"npmClient": "yarn",
"useWorkspaces": true,
"changelog": {
Expand Down
2 changes: 2 additions & 0 deletions packages/keep-alive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default (api: IApi) => {
return `'${item}'`;
});
};
api.addRuntimePluginKey(() => 'getKeepAlive');

api.onGenerateFiles(() => {
api.writeTmpFile({
path: join(DIR_NAME, 'KeepAliveLayout.tsx'),
Expand Down
25 changes: 22 additions & 3 deletions packages/keep-alive/src/utils/getKeepAliveLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { getRoutes } from '${absTmpPath}/core/routes';
import { setLayoutInstance } from './KeepAliveModel';
import pathToRegexp from '@umijs/deps/compiled/path-to-regexp';
import { matchRoutes } from 'react-router-config';
import * as app from '@/app';
const runTimeConfig = app;
const isKeepPath = (aliveList:any[],path:string)=>{
let isKeep = false;
aliveList.map(item=>{
Expand Down Expand Up @@ -60,17 +62,33 @@ interface PageProps {
export default class BasicLayout extends React.PureComponent<PageProps> {
constructor(props: any) {
super(props);
this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(),props.keepalive);
this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), props.keepalive);
const patchKeepAlive = async (func: (config: anyd[]) => any[]) => {
const keepalive = await func(props.keepalive);
this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), keepalive);
}
this.patchKeepAlive = patchKeepAlive;
this.state = { keepaliveConfig: props.keepalive, keepAliveViewMap: this.keepAliveViewMap };
// TODO: 临时支持动态的 keepalive,将 map 放到 state 中,非常不友好
this.init();
}
async init() {
if (runTimeConfig?.getKeepAlive) {
try {
const keepaliverumtime = await runTimeConfig?.getKeepAlive(this.state.keepaliveConfig);
this.keepAliveViewMap = getKeepAliveViewMap(getRoutes(), keepaliverumtime);
this.setState({
keepaliveConfig: keepaliverumtime,
keepAliveViewMap: this.keepAliveViewMap
})
} catch (error) {
console.error(error);
}
}
}
componentDidMount() {
setLayoutInstance(this);
}
keepAliveViewMap = {};
alivePathnames: string[] = [];
Expand All @@ -79,7 +97,8 @@ export default class BasicLayout extends React.PureComponent<PageProps> {
const {
location: { pathname },
} = this.props;
const showKeepAlive = !!getView(pathname, this.keepAliveViewMap);
const { keepAliveViewMap } = this.state;
const showKeepAlive = !!getView(pathname, keepAliveViewMap);
if (showKeepAlive) {
const index = this.alivePathnames.findIndex(
tPathname => tPathname === pathname.toLowerCase(),
Expand Down

0 comments on commit 23c838b

Please sign in to comment.