From 15c8853a0d010c82c3033f6405cacc5599d3e8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Sun, 15 Jul 2018 17:54:54 +0800 Subject: [PATCH] Optimization: Robust code --- config/config.js | 15 +++++++-- src/components/ActiveChart/index.js | 3 +- src/components/Charts/Pie/index.js | 3 +- src/components/Charts/TagCloud/index.js | 3 +- src/components/SettingDarwer/index.js | 8 ++--- src/models/setting.js | 2 +- src/pages/layouts/BasicLayout.js | 41 +++++++++++++++---------- src/pages/layouts/LoadingPage.js | 13 ++++++-- 8 files changed, 58 insertions(+), 30 deletions(-) diff --git a/config/config.js b/config/config.js index 972bb52f..957687ba 100644 --- a/config/config.js +++ b/config/config.js @@ -165,12 +165,18 @@ export default { component: './Account/Center/Center', routes: [ { path: '/account/center', redirect: '/account/center/articles' }, - { path: '/account/center/articles', component: './Account/Center/Articles' }, + { + path: '/account/center/articles', + component: './Account/Center/Articles', + }, { path: '/account/center/applications', component: './Account/Center/Applications', }, - { path: '/account/center/projects', component: './Account/Center/Projects' }, + { + path: '/account/center/projects', + component: './Account/Center/Projects', + }, ], }, { @@ -179,7 +185,10 @@ export default { component: './Account/Settings/Info', routes: [ { path: '/account/settings', redirect: '/account/settings/base' }, - { path: '/account/settings/base', component: './Account/Settings/BaseView' }, + { + path: '/account/settings/base', + component: './Account/Settings/BaseView', + }, { path: '/account/settings/security', component: './Account/Settings/SecurityView', diff --git a/src/components/ActiveChart/index.js b/src/components/ActiveChart/index.js index 10adc266..073f3b16 100644 --- a/src/components/ActiveChart/index.js +++ b/src/components/ActiveChart/index.js @@ -30,10 +30,11 @@ export default class ActiveChart extends Component { componentWillUnmount() { clearTimeout(this.timer); + cancelAnimationFrame(this.requestRef); } loopData = () => { - requestAnimationFrame(() => { + this.requestRef = requestAnimationFrame(() => { this.timer = setTimeout(() => { this.setState( { diff --git a/src/components/Charts/Pie/index.js b/src/components/Charts/Pie/index.js index 134a0934..6cbe881a 100644 --- a/src/components/Charts/Pie/index.js +++ b/src/components/Charts/Pie/index.js @@ -22,7 +22,7 @@ export default class Pie extends Component { window.addEventListener( 'resize', () => { - requestAnimationFrame(() => this.resize()); + this.requestRef = requestAnimationFrame(() => this.resize()); }, { passive: true } ); @@ -38,6 +38,7 @@ export default class Pie extends Component { } componentWillUnmount() { + window.cancelAnimationFrame(this.requestRef); window.removeEventListener('resize', this.resize); this.resize.cancel(); } diff --git a/src/components/Charts/TagCloud/index.js b/src/components/Charts/TagCloud/index.js index 5c299e46..49ebc947 100644 --- a/src/components/Charts/TagCloud/index.js +++ b/src/components/Charts/TagCloud/index.js @@ -35,11 +35,12 @@ class TagCloud extends Component { componentWillUnmount() { this.isUnmount = true; + window.cancelAnimationFrame(this.requestRef); window.removeEventListener('resize', this.resize); } resize = () => { - requestAnimationFrame(() => { + this.requestRef = requestAnimationFrame(() => { this.renderChart(); }); }; diff --git a/src/components/SettingDarwer/index.js b/src/components/SettingDarwer/index.js index 13d457a4..3a5119c0 100644 --- a/src/components/SettingDarwer/index.js +++ b/src/components/SettingDarwer/index.js @@ -24,7 +24,8 @@ class SettingDarwer extends PureComponent { const { setting: { themeColor, colorWeak }, } = this.props; - if (themeColor !== '#1890FF') { + // Determine if the component is remounted + if (themeColor !== '#1890FF' && themeColor !== window['antd_pro_less_color']) { window.less.refresh().then(() => { this.colorChange(themeColor); }); @@ -32,9 +33,6 @@ class SettingDarwer extends PureComponent { if (colorWeak === 'open') { document.body.className = 'colorWeak'; } - requestAnimationFrame(() => { - this.togglerContent(); - }); } getLayOutSetting = () => { @@ -139,6 +137,7 @@ class SettingDarwer extends PureComponent { '@input-hover-border-color': color, }) .then(() => { + window['antd_pro_less_color'] = color; hideMessage(); }) .catch(() => { @@ -152,6 +151,7 @@ class SettingDarwer extends PureComponent { const { collapse, silderTheme, themeColor, layout, colorWeak } = setting; return ( { let currRouterData = null; // match params path @@ -142,7 +143,16 @@ class BasicLayout extends React.PureComponent { payload: collapsed, }); }; - + componentDidMount() { + this.renderRef = requestAnimationFrame(() => { + this.setState({ + rendering: false, + }); + }); + } + componentWillUnmount() { + cancelAnimationFrame(this.renderRef); + } render() { const { isMobile, @@ -170,20 +180,19 @@ class BasicLayout extends React.PureComponent { ); - return ( - - - {params => ( - -
- {layout} - -
-
- )} -
-
+ + + + {params => ( + +
{layout}
+
+ )} +
+
+ {this.state.rendering ? null : } +
); } } diff --git a/src/pages/layouts/LoadingPage.js b/src/pages/layouts/LoadingPage.js index 2a898e9a..1bcce392 100644 --- a/src/pages/layouts/LoadingPage.js +++ b/src/pages/layouts/LoadingPage.js @@ -10,14 +10,21 @@ const menuData = config['routes']; // Conversion router to menu. function formatter(data, parentPath = '', parentAuthority, parentName) { return data.map(item => { - const id = parentName ? `${parentName}.${item.name}` : `menu.${item.name}`; + let locale = 'menu'; + if (parentName && item.name) { + locale = `${parentName}.${item.name}`; + } else if (item.name) { + locale = `menu.${item.name}`; + } else if (parentName) { + locale = parentName; + } const result = { ...item, - locale: id, + locale, authority: item.authority || parentAuthority, }; if (item.routes) { - const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, id); + const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, locale); // Reduce memory usage result.children = children; } -- GitLab