Skip to content

Commit c04202b

Browse files
committed
fix(reactant-share): fix router issue about visible
1 parent a5061c4 commit c04202b

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

packages/reactant-share/src/client.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { LastAction } from 'reactant-last-action';
88
import { HandleClientOptions } from './interfaces';
99
import { lastActionName, proxyServerActionName } from './constants';
1010
import { PortDetector } from './portDetector';
11-
import { Router } from './router';
1211
import { applyMethod } from './applyMethod';
1312

1413
export const handleClient = ({
@@ -32,24 +31,6 @@ export const handleClient = ({
3231
if (preloadedState) {
3332
lastAction.sequence = preloadedState[lastAction.stateKey]._sequence;
3433
}
35-
if (!portDetector.sharedAppOptions.forcedSyncClient) {
36-
const router: Router = container.get(Router);
37-
const visibilitychange = async () => {
38-
if (document.visibilityState === 'visible') {
39-
portDetector.syncFullState({ forceSync: false });
40-
await portDetector.syncFullStatePromise;
41-
if (router.toBeRouted) {
42-
const fn = router.toBeRouted;
43-
router.toBeRouted = null;
44-
fn();
45-
}
46-
}
47-
};
48-
document.addEventListener('visibilitychange', visibilitychange);
49-
disposeListeners.push(() => {
50-
document.removeEventListener('visibilitychange', visibilitychange);
51-
});
52-
}
5334
disposeListeners.push(
5435
transport.listen(lastActionName, async (action) => {
5536
if (portDetector.disableSyncClient) return;

packages/reactant-share/src/router.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ class ReactantRouter extends BaseReactantRouter {
4848
),
4949
});
5050

51+
this.portDetector.onClient(() => {
52+
if (!portDetector.sharedAppOptions.forcedSyncClient) {
53+
const visibilitychange = async () => {
54+
if (document.visibilityState === 'visible') {
55+
portDetector.syncFullState({ forceSync: false });
56+
await portDetector.syncFullStatePromise;
57+
if (this.toBeRouted) {
58+
const fn = this.toBeRouted;
59+
this.toBeRouted = null;
60+
fn();
61+
}
62+
}
63+
};
64+
document.addEventListener('visibilitychange', visibilitychange);
65+
return () => {
66+
document.removeEventListener('visibilitychange', visibilitychange);
67+
};
68+
}
69+
});
70+
5171
// #region sync init router from clients in Worker mode
5272
this.portDetector.onServer((transport) => {
5373
if (this.portDetector.isWorkerMode) {

0 commit comments

Comments
 (0)