Skip to content

Commit ea2a416

Browse files
committed
feat(reactant-share): add firstClientSync for router
1 parent 947f818 commit ea2a416

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

examples/reactant-share-sharedworker/src/app.view.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export class AppView extends ViewModule {
7777
transport.emit('test', 42).then((response) => {
7878
console.log(response);
7979
});
80+
this.router.firstClientSync.then(() => {
81+
this.router.push('/counter');
82+
console.log('first client sync', this.router.currentPath);
83+
});
8084
}
8185
);
8286
}

packages/reactant-share/src/modules/router.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ class ReactantRouter extends BaseReactantRouter {
6161

6262
private forwardHistory: RouterState[] = [];
6363

64+
private firstRenderingSync: Promise<void>;
65+
66+
private firstRenderingSyncResolve!: () => void;
67+
68+
private firstActiveSync: Promise<void>;
69+
70+
private firstActiveSyncResolve!: () => void;
71+
72+
/**
73+
* The promise of the first client sync.
74+
*/
75+
public firstClientSync: Promise<[void, void]>;
76+
6477
constructor(
6578
protected portDetector: PortDetector,
6679
@inject(SharedAppOptions) protected sharedAppOptions: ISharedAppOptions,
@@ -75,6 +88,18 @@ class ReactantRouter extends BaseReactantRouter {
7588
),
7689
});
7790

91+
this.firstRenderingSync = new Promise<void>((resolve) => {
92+
this.firstRenderingSyncResolve = resolve;
93+
});
94+
this.firstActiveSync = new Promise<void>((resolve) => {
95+
this.firstActiveSyncResolve = resolve;
96+
});
97+
98+
this.firstClientSync = Promise.all([
99+
this.firstRenderingSync,
100+
this.firstActiveSync,
101+
]);
102+
78103
this.portDetector.onClient(() => {
79104
const stopWatching = watch(
80105
this,
@@ -86,12 +111,13 @@ class ReactantRouter extends BaseReactantRouter {
86111
action.type === LOCATION_CHANGE &&
87112
action.payload.isFirstRendering
88113
) {
114+
stopWatching();
89115
const router = this._routers[this.portDetector.name];
90116
if (router && this.compareRouter(router, this.router!)) {
91-
stopWatching();
92117
// router reducer @@router/LOCATION_CHANGE event and syncFullState event The events may be out of order, so we re-check route consistency after synchronizing the state.
93118
this.history.replace(router.location);
94119
}
120+
this.firstRenderingSyncResolve();
95121
}
96122
}
97123
);
@@ -264,10 +290,14 @@ class ReactantRouter extends BaseReactantRouter {
264290
this.router
265291
)
266292
.then((router) => {
267-
if (!router) return;
293+
if (!router) {
294+
this.firstActiveSyncResolve();
295+
return;
296+
}
268297
this.passiveRoute = true;
269298
this.history.replace(router.location);
270299
this.passiveRoute = false;
300+
this.firstActiveSyncResolve();
271301
});
272302
});
273303
// #endregion

0 commit comments

Comments
 (0)