Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: history listener subscription execute multiple times #2230

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"build": "father build",
"build": "father-build",
"doc:dev": "./website/node_modules/.bin/vuepress dev ./docs",
"doc:deploy": "rm -rf ./website/yarn.lock && cd ./website && npm run deploy && cd -",
"changelog": "lerna-changelog",
Expand Down Expand Up @@ -33,7 +33,7 @@
"react-dom": "^16.8.4",
"react-testing-library": "^6.0.0",
"shelljs": "^0.8.1",
"father": "^2.6.6",
"father-build": "^1.14.0",
"umi-test": "^1.5.2"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion packages/dva/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/isomorphic-fetch": "^0.0.35",
"@types/react-redux": "^7.1.0",
"@types/react-router-dom": "^4.3.1",
"connected-react-router": "^6.3.2",
"connected-react-router": "6.3.2",
"dva-core": "2.0.0",
"global": "^4.3.2",
"history": "^4.7.2",
Expand Down
18 changes: 16 additions & 2 deletions packages/dva/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default function(opts = {}) {
},
setupApp(app) {
app._history = patchHistory(history);
// app._history = patchHistory(history);
},
};

Expand Down Expand Up @@ -106,8 +105,23 @@ function render(container, store, app, router) {
function patchHistory(history) {
const oldListen = history.listen;
history.listen = callback => {
// Let ConnectedRouter to sync history to store first
// connected-react-router's version is locked since the check function may be broken
// ref: https://github.com/umijs/umi/issues/2693
const isConnectedRouterHandler =
callback.name === 'handleLocationChange' &&
callback.toString().indexOf('onLocationChanged') > -1;
callback(history.location, history.action);
return oldListen.call(history, callback);
return oldListen.call(history, (...args) => {
if (isConnectedRouterHandler) {
callback(...args);
} else {
// Delay all listeners besides ConnectedRouter
setTimeout(() => {
callback(...args);
});
}
});
};
return history;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/dva/test/index.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { Link, Switch, Route, Router } = router;

afterEach(cleanup);

const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));

test('normal', () => {
const app = dva();
app.model({
Expand Down Expand Up @@ -154,7 +156,9 @@ test('navigate', async () => {
const { getByTestId, getByText } = render(React.createElement(app.start()));
expect(getByTestId('title').innerHTML).toEqual('You are on Home');
fireEvent.click(getByText('Users'));
await delay(100);
expect(getByTestId('title').innerHTML).toEqual('You are on Users');
fireEvent.click(getByText('RouterRedux to Home'));
await delay(100);
expect(getByTestId('title').innerHTML).toEqual('You are on Home');
});