Skip to content

Commit

Permalink
Optimization: cancel the request of the current page when the page ju…
Browse files Browse the repository at this point in the history
…mps.
  • Loading branch information
zuiidea committed Sep 20, 2018
1 parent 4e7795e commit 08f40ef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/layouts/BaseLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ class BaseLayout extends PureComponent {
const currentPath = location.pathname + location.search
if (currentPath !== this.previousPath) {
NProgress.start()
if (!loading.global) {
NProgress.done()
this.previousPath = currentPath
}
}

if (!loading.global) {
NProgress.done()
this.previousPath = currentPath
}

return (
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/PrimaryLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PrimaryLayout extends PureComponent {
<Layout>
<MyLayout.Sider {...siderProps} />
<Layout
style={{ height: '100vh', overflow: 'scroll' }}
style={{ height: '100vh', overflowY: 'scroll' }}
id="primaryLayout"
>
<Header {...headerProps} />
Expand Down
30 changes: 17 additions & 13 deletions src/models/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* global window */
/* global document */
/* global location */
/* eslint no-restricted-globals: ["error", "event"] */

import { router } from 'utils'
import { parse, stringify } from 'qs'
import { stringify } from 'qs'
import store from 'store'
import { RoleType } from 'utils/constant'
import { queryLayout } from 'utils'
import { queryLayout, pathMatchRegexp } from 'utils'
import { CANCEL_REQUEST_MESSAGE } from 'utils/constant'
import { queryRouteList, logoutUser, queryUserInfo } from 'api'
import config from 'config'

Expand Down Expand Up @@ -44,15 +42,21 @@ export default {
})
},

setupRequestCancel({ history }) {
history.listen(() => {
const { cancelRequest = new Map() } = window

cancelRequest.forEach((value, key) => {
if (value.pathname !== window.location.pathname) {
value.cancel(CANCEL_REQUEST_MESSAGE)
cancelRequest.delete(key)
}
})
})
},

setup({ dispatch }) {
dispatch({ type: 'query' })
let tid
window.onresize = () => {
clearTimeout(tid)
tid = setTimeout(() => {
dispatch({ type: 'changeNavbar' })
}, 300)
}
},
},
effects: {
Expand Down Expand Up @@ -89,7 +93,7 @@ export default {
routeList,
},
})
if (location.pathname === '/login') {
if (pathMatchRegexp('/login', window.location.pathname)) {
router.push({
pathname: '/dashboard',
})
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const RoleType = {
DEFAULT: 'admin',
DEVELOPER: 'developer',
}

export const CANCEL_REQUEST_MESSAGE = 'cancle request'
21 changes: 20 additions & 1 deletion src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import axios from 'axios'
import { cloneDeep, isEmpty } from 'lodash'
import pathToRegexp from 'path-to-regexp'
import { message } from 'antd'
import { CANCEL_REQUEST_MESSAGE } from 'utils/constant'
import qs from 'qs'

const { CancelToken } = axios
window.cancelRequest = new Map()

export default function request(options) {
let { data, url, method = 'get' } = options
const cloneData = cloneDeep(data)
Expand Down Expand Up @@ -34,6 +38,13 @@ export default function request(options) {
? `${url}${isEmpty(cloneData) ? '' : '?'}${qs.stringify(cloneData)}`
: url

options.cancelToken = new CancelToken(cancel => {
window.cancelRequest.set(Symbol(Date.now()), {
pathname: window.location.pathname,
cancel,
})
})

return axios(options)
.then(response => {
const { statusText, status, data } = response
Expand All @@ -56,9 +67,17 @@ export default function request(options) {
})
})
.catch(error => {
const { response } = error
const { response, message } = error

if (String(message) === CANCEL_REQUEST_MESSAGE) {
return {
success: false,
}
}

let msg
let statusCode

if (response && response instanceof Object) {
const { data, statusText } = response
statusCode = response.status
Expand Down

0 comments on commit 08f40ef

Please sign in to comment.