Skip to content

Commit 869f530

Browse files
authored
fix: history listener subscription execute multiple times (#2230)
* fix: history listener subscription execute multiple times * chore: father -> father-build
1 parent 0e763de commit 869f530

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"scripts": {
4-
"build": "father build",
4+
"build": "father-build",
55
"doc:dev": "./website/node_modules/.bin/vuepress dev ./docs",
66
"doc:deploy": "rm -rf ./website/yarn.lock && cd ./website && npm run deploy && cd -",
77
"changelog": "lerna-changelog",
@@ -33,7 +33,7 @@
3333
"react-dom": "^16.8.4",
3434
"react-testing-library": "^6.0.0",
3535
"shelljs": "^0.8.1",
36-
"father": "^2.6.6",
36+
"father-build": "^1.14.0",
3737
"umi-test": "^1.5.2"
3838
},
3939
"lint-staged": {

packages/dva/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@types/isomorphic-fetch": "^0.0.35",
3535
"@types/react-redux": "^7.1.0",
3636
"@types/react-router-dom": "^4.3.1",
37-
"connected-react-router": "^6.3.2",
37+
"connected-react-router": "6.3.2",
3838
"dva-core": "2.0.0",
3939
"global": "^4.3.2",
4040
"history": "^4.7.2",

packages/dva/src/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default function(opts = {}) {
3030
},
3131
setupApp(app) {
3232
app._history = patchHistory(history);
33-
// app._history = patchHistory(history);
3433
},
3534
};
3635

@@ -106,8 +105,23 @@ function render(container, store, app, router) {
106105
function patchHistory(history) {
107106
const oldListen = history.listen;
108107
history.listen = callback => {
108+
// Let ConnectedRouter to sync history to store first
109+
// connected-react-router's version is locked since the check function may be broken
110+
// ref: https://github.com/umijs/umi/issues/2693
111+
const isConnectedRouterHandler =
112+
callback.name === 'handleLocationChange' &&
113+
callback.toString().indexOf('onLocationChanged') > -1;
109114
callback(history.location, history.action);
110-
return oldListen.call(history, callback);
115+
return oldListen.call(history, (...args) => {
116+
if (isConnectedRouterHandler) {
117+
callback(...args);
118+
} else {
119+
// Delay all listeners besides ConnectedRouter
120+
setTimeout(() => {
121+
callback(...args);
122+
});
123+
}
124+
});
111125
};
112126
return history;
113127
}

packages/dva/test/index.e2e.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const { Link, Switch, Route, Router } = router;
1515

1616
afterEach(cleanup);
1717

18+
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
19+
1820
test('normal', () => {
1921
const app = dva();
2022
app.model({
@@ -154,7 +156,9 @@ test('navigate', async () => {
154156
const { getByTestId, getByText } = render(React.createElement(app.start()));
155157
expect(getByTestId('title').innerHTML).toEqual('You are on Home');
156158
fireEvent.click(getByText('Users'));
159+
await delay(100);
157160
expect(getByTestId('title').innerHTML).toEqual('You are on Users');
158161
fireEvent.click(getByText('RouterRedux to Home'));
162+
await delay(100);
159163
expect(getByTestId('title').innerHTML).toEqual('You are on Home');
160164
});

0 commit comments

Comments
 (0)