Skip to content

Commit

Permalink
fix: history listener subscription execute multiple times (#2230)
Browse files Browse the repository at this point in the history
* fix: history listener subscription execute multiple times

* chore: father -> father-build
  • Loading branch information
sorrycc authored Oct 28, 2019
1 parent 0e763de commit 869f530
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
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');
});

1 comment on commit 869f530

@yoyo837
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个修改会导致不跳转或者警告你当前跳转的地址就是当前地址。 Broken!

Please sign in to comment.