Skip to content

Commit 78bcbc8

Browse files
committed
feat(reactant-share): implement enableCacheRouting for router
1 parent 2f867cb commit 78bcbc8

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

packages/reactant-share/src/router.ts

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/* eslint-disable no-shadow */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
/* eslint-disable consistent-return */
4-
import { injectable, inject, watch, state, action } from 'reactant';
4+
import {
5+
injectable,
6+
inject,
7+
watch,
8+
state,
9+
action,
10+
stateKey,
11+
modulesKey,
12+
} from 'reactant';
513
import { Router as BaseReactantRouter, RouterOptions } from 'reactant-router';
614
import type {
715
IRouterOptions as IBaseRouterOptions,
@@ -48,6 +56,20 @@ class ReactantRouter extends BaseReactantRouter {
4856
),
4957
});
5058

59+
if (!this.portDetector.shared) {
60+
this.watchRehydratedRouting();
61+
watch(
62+
this,
63+
() => this.router,
64+
() => {
65+
if (this.router) {
66+
// just update the current router to routers mapping by name
67+
this._setRouters(this.portDetector.name, this.router);
68+
}
69+
}
70+
);
71+
}
72+
5173
this.portDetector.onClient(() => {
5274
if (!this.portDetector.sharedAppOptions.forcedSyncClient) {
5375
const visibilitychange = async () => {
@@ -69,14 +91,16 @@ class ReactantRouter extends BaseReactantRouter {
6991

7092
// #region sync init router from clients in Worker mode
7193
this.portDetector.onServer((transport) => {
72-
if (this.portDetector.isWorkerMode) {
94+
if (this.portDetector.isWorkerMode && !this.enableCacheRouting) {
7395
transport
7496
.emit(syncWorkerRouterName, this.portDetector.name)
7597
.then((router) => {
7698
if (router) {
7799
this._changeRoutingOnSever(this.portDetector.name, router);
78100
}
79101
});
102+
} else if (this.enableCacheRouting) {
103+
this.watchRehydratedRouting();
80104
}
81105
});
82106
this.portDetector.onClient((transport) => {
@@ -125,7 +149,22 @@ class ReactantRouter extends BaseReactantRouter {
125149

126150
// #region sync init router from server port in all modes
127151
this.portDetector.onServer((transport) => {
152+
const rehydratedPromise = this.enableCacheRouting
153+
? new Promise<void>((resolve) => {
154+
const stopWatching = watch(
155+
this,
156+
() => (this as any)[stateKey]._persist?.rehydrated,
157+
(rehydrated) => {
158+
if (rehydrated) {
159+
stopWatching();
160+
resolve();
161+
}
162+
}
163+
);
164+
})
165+
: Promise.resolve();
128166
return transport!.listen(syncRouterName, async (name, router) => {
167+
await rehydratedPromise;
129168
const currentRouter = this._routers[name]!;
130169
if (!currentRouter && router) {
131170
this._changeRoutingOnSever(name, router);
@@ -144,6 +183,22 @@ class ReactantRouter extends BaseReactantRouter {
144183
// #endregion
145184
}
146185

186+
watchRehydratedRouting() {
187+
const stopWatching = watch(
188+
this,
189+
() => (this as any)[stateKey]._persist?.rehydrated,
190+
(rehydrated) => {
191+
if (rehydrated) {
192+
stopWatching();
193+
const router = this._routers[this.portDetector.name];
194+
if (router) {
195+
this._changeRoutingOnSever(this.portDetector.name, router);
196+
}
197+
}
198+
}
199+
);
200+
}
201+
147202
protected _changeRoutingOnSever(name: string, router: RouterState) {
148203
this._setRouters(name, router);
149204
if (name === this.portDetector.name) {
@@ -155,11 +210,13 @@ class ReactantRouter extends BaseReactantRouter {
155210
) {
156211
this.history.push(router.location);
157212
}
158-
fork(this as any, '_changeRoutingOnClient', [
159-
this.portDetector.name,
160-
this.router,
161-
]);
162-
} else {
213+
if (this.portDetector.shared) {
214+
fork(this as any, '_changeRoutingOnClient', [
215+
this.portDetector.name,
216+
this.router,
217+
]);
218+
}
219+
} else if (this.portDetector.shared) {
163220
fork(this as any, '_changeRoutingOnClient', [name, router]);
164221
}
165222
}
@@ -217,14 +274,28 @@ class ReactantRouter extends BaseReactantRouter {
217274

218275
@action
219276
protected _setRouters(name: string, router: RouterState) {
220-
this._routers[name] = router;
277+
if (
278+
!this.enableCacheRouting ||
279+
(this.enableCacheRouting && (this as any)[stateKey]._persist?.rehydrated)
280+
) {
281+
this._routers[name] = router;
282+
}
221283
}
222284

223285
// The server port routing state is received asynchronously, so there should be a default route.
224286
protected get defaultRoute() {
225287
return this.options.defaultRoute ?? '/';
226288
}
227289

290+
protected get enableCacheRouting() {
291+
const { Storage } = (this as any)[modulesKey];
292+
return (
293+
Storage?.persistConfig.Router &&
294+
(Storage.persistConfig.Router!.whitelist?.includes('_routers') ||
295+
Storage.persistConfig.Router!.blacklist?.includes('_routers') === false)
296+
);
297+
}
298+
228299
protected defaultHistory = {
229300
action: 'POP',
230301
location: {

0 commit comments

Comments
 (0)