reactive-route 0.0.1-alpha.21 → 0.0.1-alpha.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/cjs/index.js +121 -105
- package/dist/cjs/preact/index.js +16 -18
- package/dist/cjs/react/index.js +16 -18
- package/dist/cjs/solid/index.js +28 -21
- package/dist/core/createRouter.d.ts +9 -0
- package/dist/core/createRouter.d.ts.map +1 -0
- package/dist/core/{createRouterConfig.d.ts → createRoutes.d.ts} +3 -3
- package/dist/core/createRoutes.d.ts.map +1 -0
- package/dist/core/index.d.ts +6 -7
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/types/TypeAdapters.d.ts +9 -0
- package/dist/core/types/TypeAdapters.d.ts.map +1 -0
- package/dist/core/types/TypeCurrentRoute.d.ts +1 -1
- package/dist/core/types/TypeCurrentRoute.d.ts.map +1 -1
- package/dist/core/types/TypeLifecycleConfig.d.ts +2 -0
- package/dist/core/types/TypeLifecycleConfig.d.ts.map +1 -1
- package/dist/core/types/TypePropsRouter.d.ts +2 -2
- package/dist/core/types/TypePropsRouter.d.ts.map +1 -1
- package/dist/core/types/{TypeRedirectToParams.d.ts → TypeRedirectParams.d.ts} +2 -2
- package/dist/core/types/TypeRedirectParams.d.ts.map +1 -0
- package/dist/core/types/TypeRoute.d.ts +0 -1
- package/dist/core/types/TypeRoute.d.ts.map +1 -1
- package/dist/core/types/TypeRouteRaw.d.ts +1 -0
- package/dist/core/types/TypeRouteRaw.d.ts.map +1 -1
- package/dist/core/types/TypeRouter.d.ts +17 -0
- package/dist/core/types/TypeRouter.d.ts.map +1 -0
- package/dist/core/utils/PreventError.d.ts +4 -0
- package/dist/core/utils/PreventError.d.ts.map +1 -0
- package/dist/core/utils/RedirectError.d.ts +4 -0
- package/dist/core/utils/RedirectError.d.ts.map +1 -0
- package/dist/core/utils/constants.d.ts +0 -2
- package/dist/core/utils/constants.d.ts.map +1 -1
- package/dist/core/utils/getInitialRoute.d.ts +2 -2
- package/dist/core/utils/getInitialRoute.d.ts.map +1 -1
- package/dist/core/utils/getQueryValues.d.ts.map +1 -1
- package/dist/core/utils/loadComponentToConfig.d.ts.map +1 -1
- package/dist/core/utils/queryString.d.ts +8 -0
- package/dist/core/utils/queryString.d.ts.map +1 -0
- package/dist/core/utils/replaceDynamicValues.d.ts +1 -2
- package/dist/core/utils/replaceDynamicValues.d.ts.map +1 -1
- package/dist/esm/index.js +119 -93
- package/dist/esm/preact/index.js +17 -19
- package/dist/esm/react/index.js +17 -19
- package/dist/esm/solid/index.js +29 -22
- package/dist/preact/Router.d.ts.map +1 -1
- package/dist/react/Router.d.ts.map +1 -1
- package/dist/solid/Router.d.ts.map +1 -1
- package/dist/tsconfig.types.react.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/pnpm-workspace.yaml +5 -0
- package/dist/core/createRouterConfig.d.ts.map +0 -1
- package/dist/core/createRouterStore.d.ts +0 -4
- package/dist/core/createRouterStore.d.ts.map +0 -1
- package/dist/core/types/InterfaceRouterStore.d.ts +0 -28
- package/dist/core/types/InterfaceRouterStore.d.ts.map +0 -1
- package/dist/core/types/TypeRedirectToParams.d.ts.map +0 -1
- package/dist/core/types/TypeRouteWithParams.d.ts +0 -6
- package/dist/core/types/TypeRouteWithParams.d.ts.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,26 +1,8 @@
|
|
|
1
|
-
// packages/core/utils/addNames.ts
|
|
2
|
-
function addNames(obj) {
|
|
3
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
4
|
-
value.name = key;
|
|
5
|
-
});
|
|
6
|
-
return obj;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// packages/core/createRouterConfig.ts
|
|
10
|
-
function createRouterConfig(config) {
|
|
11
|
-
return addNames(config);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// packages/core/createRouterStore.ts
|
|
15
|
-
import queryString2 from "query-string";
|
|
16
|
-
|
|
17
1
|
// packages/core/utils/constants.ts
|
|
18
2
|
var constants = {
|
|
19
3
|
dynamicSeparator: ":",
|
|
20
4
|
pathPartSeparator: "/",
|
|
21
|
-
isClient: typeof window !== "undefined"
|
|
22
|
-
errorRedirect: "REDIRECT",
|
|
23
|
-
errorPrevent: "PREVENT_REDIRECT"
|
|
5
|
+
isClient: typeof window !== "undefined"
|
|
24
6
|
};
|
|
25
7
|
|
|
26
8
|
// packages/core/utils/dynamic.ts
|
|
@@ -30,9 +12,6 @@ function isDynamic(param) {
|
|
|
30
12
|
function clearDynamic(param) {
|
|
31
13
|
return param.replace(new RegExp(`^${constants.dynamicSeparator}`), "");
|
|
32
14
|
}
|
|
33
|
-
function isDynamicRoute(route) {
|
|
34
|
-
return "params" in route;
|
|
35
|
-
}
|
|
36
15
|
|
|
37
16
|
// packages/core/utils/getDynamicValues.ts
|
|
38
17
|
function getDynamicValues(params) {
|
|
@@ -78,12 +57,28 @@ function findRouteByPathname({
|
|
|
78
57
|
return dynamicRouteMatch;
|
|
79
58
|
}
|
|
80
59
|
|
|
81
|
-
// packages/core/utils/getQueryValues.ts
|
|
82
|
-
import queryString from "query-string";
|
|
83
|
-
|
|
84
60
|
// packages/core/utils/getTypedEntries.ts
|
|
85
61
|
var getTypedEntries = Object.entries;
|
|
86
62
|
|
|
63
|
+
// packages/core/utils/queryString.ts
|
|
64
|
+
function removeHash(input) {
|
|
65
|
+
const hashStart = input.indexOf("#");
|
|
66
|
+
return hashStart === -1 ? input : input.slice(0, hashStart);
|
|
67
|
+
}
|
|
68
|
+
var queryString = {
|
|
69
|
+
extract(input) {
|
|
70
|
+
const inputNoHash = removeHash(input);
|
|
71
|
+
const queryStart = inputNoHash.indexOf("?");
|
|
72
|
+
return queryStart === -1 ? "" : inputNoHash.slice(queryStart + 1);
|
|
73
|
+
},
|
|
74
|
+
parse(input) {
|
|
75
|
+
return Object.fromEntries(new URLSearchParams(input));
|
|
76
|
+
},
|
|
77
|
+
stringify(obj) {
|
|
78
|
+
return new URLSearchParams(obj).toString();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
87
82
|
// packages/core/utils/getQueryValues.ts
|
|
88
83
|
function getQueryValues(params) {
|
|
89
84
|
const { route, pathname } = params;
|
|
@@ -119,15 +114,30 @@ function loadComponentToConfig(params) {
|
|
|
119
114
|
if (!route.component && route.loader) {
|
|
120
115
|
const loadingFn = route.loader;
|
|
121
116
|
return loadingFn().then((module) => {
|
|
122
|
-
const { default: component,
|
|
117
|
+
const { default: component, ...rest } = module;
|
|
123
118
|
route.component = component;
|
|
124
119
|
route.otherExports = rest;
|
|
125
|
-
route.pageName = pageName;
|
|
126
120
|
});
|
|
127
121
|
}
|
|
128
122
|
return Promise.resolve();
|
|
129
123
|
}
|
|
130
124
|
|
|
125
|
+
// packages/core/utils/PreventError.ts
|
|
126
|
+
var PreventError = class extends Error {
|
|
127
|
+
constructor(message) {
|
|
128
|
+
super(message);
|
|
129
|
+
this.name = "PreventError";
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// packages/core/utils/RedirectError.ts
|
|
134
|
+
var RedirectError = class extends Error {
|
|
135
|
+
constructor(message) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.name = "RedirectError";
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
131
141
|
// packages/core/utils/replaceDynamicValues.ts
|
|
132
142
|
var re = new RegExp(`[^${constants.pathPartSeparator}]+`, "g");
|
|
133
143
|
function replaceDynamicValues({
|
|
@@ -146,53 +156,52 @@ function replaceDynamicValues({
|
|
|
146
156
|
});
|
|
147
157
|
}
|
|
148
158
|
|
|
149
|
-
// packages/core/
|
|
150
|
-
function
|
|
151
|
-
adapters,
|
|
159
|
+
// packages/core/createRouter.ts
|
|
160
|
+
function createRouter({
|
|
152
161
|
routes,
|
|
162
|
+
adapters,
|
|
153
163
|
lifecycleParams
|
|
154
164
|
}) {
|
|
155
|
-
const
|
|
165
|
+
const router = adapters.makeObservable({
|
|
156
166
|
routesHistory: [],
|
|
157
167
|
currentRoute: {},
|
|
158
168
|
isRedirecting: false,
|
|
159
|
-
|
|
169
|
+
redirect: void 0,
|
|
160
170
|
restoreFromURL: void 0,
|
|
161
171
|
restoreFromServer: void 0,
|
|
162
|
-
adapters
|
|
172
|
+
get adapters() {
|
|
173
|
+
return adapters;
|
|
174
|
+
}
|
|
163
175
|
});
|
|
164
|
-
|
|
176
|
+
router.restoreFromServer = function restoreFromServer(obj) {
|
|
165
177
|
adapters.batch(() => {
|
|
166
|
-
|
|
167
|
-
Object.assign(
|
|
178
|
+
router.routesHistory.push(...obj.routesHistory || []);
|
|
179
|
+
Object.assign(router.currentRoute, obj.currentRoute);
|
|
168
180
|
});
|
|
169
181
|
const preloadedRouteName = Object.keys(routes).find(
|
|
170
|
-
(routeName) =>
|
|
182
|
+
(routeName) => router.currentRoute.name === routeName
|
|
171
183
|
);
|
|
172
184
|
return loadComponentToConfig({ route: routes[preloadedRouteName] });
|
|
173
185
|
};
|
|
174
|
-
|
|
175
|
-
return
|
|
186
|
+
router.restoreFromURL = function restoreFromURL(params) {
|
|
187
|
+
return router.redirect(getInitialRoute({ routes, ...params }));
|
|
176
188
|
};
|
|
177
|
-
|
|
189
|
+
router.redirect = async function redirect(config) {
|
|
178
190
|
const { route: routeName, noHistoryPush } = config;
|
|
179
191
|
let currentRoute;
|
|
180
192
|
let currentPathname;
|
|
181
193
|
let currentUrl;
|
|
182
194
|
let currentSearch;
|
|
183
195
|
let currentQuery;
|
|
184
|
-
if (
|
|
185
|
-
currentRoute = routes[
|
|
196
|
+
if (router.currentRoute?.name) {
|
|
197
|
+
currentRoute = routes[router.currentRoute.name];
|
|
186
198
|
currentPathname = replaceDynamicValues({
|
|
187
199
|
route: currentRoute,
|
|
188
|
-
params:
|
|
200
|
+
params: router.currentRoute.params
|
|
189
201
|
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
});
|
|
194
|
-
currentQuery = routerStore.currentRoute.query;
|
|
195
|
-
currentSearch = queryString2.stringify(routerStore.currentRoute.query);
|
|
202
|
+
currentQuery = router.currentRoute.query;
|
|
203
|
+
currentSearch = queryString.stringify(router.currentRoute.query);
|
|
204
|
+
currentUrl = `${currentPathname}${currentSearch ? `?${currentSearch}` : ""}`;
|
|
196
205
|
}
|
|
197
206
|
const nextRoute = routes[routeName];
|
|
198
207
|
const nextPathname = replaceDynamicValues({
|
|
@@ -205,23 +214,23 @@ function createRouterStore({
|
|
|
205
214
|
if ("query" in config && config.query) {
|
|
206
215
|
const clearedQuery = getQueryValues({
|
|
207
216
|
route: nextRoute,
|
|
208
|
-
pathname: `${nextPathname}?${
|
|
217
|
+
pathname: `${nextPathname}?${queryString.stringify(config.query)}`
|
|
209
218
|
});
|
|
210
219
|
if (Object.keys(clearedQuery).length > 0) {
|
|
211
220
|
nextQuery = clearedQuery;
|
|
212
|
-
nextSearch =
|
|
213
|
-
nextUrl =
|
|
221
|
+
nextSearch = queryString.stringify(clearedQuery);
|
|
222
|
+
nextUrl = `${nextPathname}?${nextSearch}`;
|
|
214
223
|
}
|
|
215
224
|
}
|
|
216
225
|
if (currentUrl === nextUrl) return Promise.resolve();
|
|
217
226
|
if (currentPathname === nextPathname) {
|
|
218
227
|
if (currentSearch !== nextSearch) {
|
|
219
228
|
adapters.batch(() => {
|
|
220
|
-
adapters.replaceObject(
|
|
221
|
-
...
|
|
229
|
+
adapters.replaceObject(router.currentRoute, {
|
|
230
|
+
...router.currentRoute,
|
|
222
231
|
query: nextQuery || {}
|
|
223
232
|
});
|
|
224
|
-
|
|
233
|
+
router.routesHistory.push(nextUrl);
|
|
225
234
|
});
|
|
226
235
|
if (history && !noHistoryPush) {
|
|
227
236
|
history.push({
|
|
@@ -234,7 +243,7 @@ function createRouterStore({
|
|
|
234
243
|
return Promise.resolve();
|
|
235
244
|
}
|
|
236
245
|
adapters.batch(() => {
|
|
237
|
-
|
|
246
|
+
router.isRedirecting = true;
|
|
238
247
|
});
|
|
239
248
|
try {
|
|
240
249
|
const config2 = {
|
|
@@ -247,83 +256,100 @@ function createRouterStore({
|
|
|
247
256
|
currentQuery,
|
|
248
257
|
currentRoute,
|
|
249
258
|
currentSearch,
|
|
250
|
-
currentPathname
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
let redirectUrl = replaceDynamicValues({
|
|
259
|
-
params: redirectParams,
|
|
260
|
-
route: redirectRoute
|
|
261
|
-
});
|
|
262
|
-
if ("query" in redirectConfig && redirectConfig.query) {
|
|
263
|
-
const clearedQuery = getQueryValues({
|
|
264
|
-
route: nextRoute,
|
|
265
|
-
pathname: `${nextPathname}?${queryString2.stringify(redirectConfig.query)}`
|
|
259
|
+
currentPathname,
|
|
260
|
+
redirect: (redirectConfig2) => {
|
|
261
|
+
if (constants.isClient) return redirectConfig2;
|
|
262
|
+
const redirectRoute = routes[redirectConfig2.route];
|
|
263
|
+
const redirectParams = "params" in redirectConfig2 && redirectConfig2.params ? redirectConfig2.params : void 0;
|
|
264
|
+
let redirectUrl = replaceDynamicValues({
|
|
265
|
+
params: redirectParams,
|
|
266
|
+
route: redirectRoute
|
|
266
267
|
});
|
|
267
|
-
if (
|
|
268
|
-
|
|
268
|
+
if ("query" in redirectConfig2 && redirectConfig2.query) {
|
|
269
|
+
const clearedQuery = getQueryValues({
|
|
270
|
+
route: nextRoute,
|
|
271
|
+
pathname: `${nextPathname}?${queryString.stringify(redirectConfig2.query)}`
|
|
272
|
+
});
|
|
273
|
+
if (Object.keys(clearedQuery).length > 0) {
|
|
274
|
+
redirectUrl = `${redirectUrl}?${queryString.stringify(clearedQuery)}`;
|
|
275
|
+
}
|
|
269
276
|
}
|
|
277
|
+
throw new RedirectError(redirectUrl);
|
|
278
|
+
},
|
|
279
|
+
preventRedirect: () => {
|
|
280
|
+
throw new PreventError(`Redirect to ${nextUrl} was prevented`);
|
|
270
281
|
}
|
|
271
|
-
|
|
272
|
-
|
|
282
|
+
};
|
|
283
|
+
await currentRoute?.beforeLeave?.(config2, ...lifecycleParams || []);
|
|
284
|
+
const redirectConfig = await nextRoute.beforeEnter?.(config2, ...lifecycleParams || []);
|
|
285
|
+
if (redirectConfig) return redirect(redirectConfig);
|
|
273
286
|
await loadComponentToConfig({ route: routes[nextRoute.name] });
|
|
274
287
|
} catch (error) {
|
|
275
|
-
if (error
|
|
276
|
-
|
|
288
|
+
if (error instanceof PreventError) {
|
|
289
|
+
return Promise.resolve();
|
|
290
|
+
}
|
|
291
|
+
if (error instanceof RedirectError) {
|
|
277
292
|
throw error;
|
|
278
293
|
}
|
|
279
294
|
console.error(error);
|
|
280
295
|
await loadComponentToConfig({ route: routes.internalError });
|
|
281
296
|
adapters.batch(() => {
|
|
282
|
-
adapters.replaceObject(
|
|
297
|
+
adapters.replaceObject(router.currentRoute, {
|
|
283
298
|
name: routes.internalError.name,
|
|
284
299
|
path: routes.internalError.path,
|
|
285
300
|
props: routes[routes.internalError.name].props,
|
|
286
301
|
query: adapters.makeObservable({}),
|
|
287
302
|
params: adapters.makeObservable({}),
|
|
288
|
-
|
|
303
|
+
pageId: routes[routes.internalError.name].pageId
|
|
289
304
|
});
|
|
290
|
-
|
|
305
|
+
router.isRedirecting = false;
|
|
291
306
|
});
|
|
292
307
|
return Promise.resolve();
|
|
293
308
|
}
|
|
294
309
|
adapters.batch(() => {
|
|
295
|
-
adapters.replaceObject(
|
|
310
|
+
adapters.replaceObject(router.currentRoute, {
|
|
296
311
|
name: nextRoute.name,
|
|
297
312
|
path: nextRoute.path,
|
|
298
313
|
props: routes[nextRoute.name].props,
|
|
299
314
|
query: getQueryValues({ route: nextRoute, pathname: nextUrl }),
|
|
300
315
|
params: getDynamicValues({ route: nextRoute, pathname: nextUrl }),
|
|
301
|
-
|
|
316
|
+
pageId: routes[nextRoute.name].pageId
|
|
302
317
|
});
|
|
303
|
-
const lastUrl =
|
|
318
|
+
const lastUrl = router.routesHistory[router.routesHistory.length - 1];
|
|
304
319
|
if (lastUrl !== nextUrl) {
|
|
305
|
-
|
|
320
|
+
router.routesHistory.push(nextUrl);
|
|
306
321
|
}
|
|
307
322
|
if (history && !noHistoryPush) {
|
|
308
323
|
history.push({
|
|
309
324
|
hash: history.location.hash,
|
|
310
|
-
search: "query" in config ? `?${
|
|
325
|
+
search: "query" in config ? `?${queryString.stringify(config.query)}` : "",
|
|
311
326
|
pathname: nextPathname
|
|
312
327
|
});
|
|
313
328
|
}
|
|
314
|
-
|
|
329
|
+
router.isRedirecting = false;
|
|
315
330
|
});
|
|
316
331
|
return Promise.resolve();
|
|
317
332
|
};
|
|
318
|
-
return
|
|
333
|
+
return router;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// packages/core/utils/addNames.ts
|
|
337
|
+
function addNames(obj) {
|
|
338
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
339
|
+
value.name = key;
|
|
340
|
+
});
|
|
341
|
+
return obj;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// packages/core/createRoutes.ts
|
|
345
|
+
function createRoutes(config) {
|
|
346
|
+
return addNames(config);
|
|
319
347
|
}
|
|
320
348
|
export {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
getInitialRoute,
|
|
349
|
+
RedirectError,
|
|
350
|
+
createRouter,
|
|
351
|
+
createRoutes,
|
|
325
352
|
history,
|
|
326
|
-
isDynamicRoute,
|
|
327
353
|
loadComponentToConfig,
|
|
328
354
|
replaceDynamicValues
|
|
329
355
|
};
|
package/dist/esm/preact/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/preact/Router.tsx
|
|
2
2
|
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
|
|
3
|
-
import {
|
|
3
|
+
import { history } from "reactive-route";
|
|
4
4
|
import { jsx } from "preact/jsx-runtime";
|
|
5
5
|
function RouterInner(props) {
|
|
6
6
|
const disposerRef = useRef(null);
|
|
@@ -8,21 +8,18 @@ function RouterInner(props) {
|
|
|
8
8
|
if (!history) return;
|
|
9
9
|
history.listen((params) => {
|
|
10
10
|
if (params.action !== "POP") return;
|
|
11
|
-
const previousRoutePathname = props.
|
|
11
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
12
12
|
if (previousRoutePathname === params.location.pathname) {
|
|
13
|
-
props.
|
|
13
|
+
props.router.adapters.batch(() => props.router.routesHistory.pop());
|
|
14
14
|
}
|
|
15
|
-
void props.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
routes: props.routes,
|
|
19
|
-
pathname: history.location.pathname
|
|
20
|
-
})
|
|
15
|
+
void props.router.restoreFromURL({
|
|
16
|
+
pathname: history.location.pathname,
|
|
17
|
+
noHistoryPush: true
|
|
21
18
|
});
|
|
22
19
|
});
|
|
23
20
|
}, []);
|
|
24
21
|
const [config] = useState(
|
|
25
|
-
() => props.
|
|
22
|
+
() => props.router.adapters.makeObservable({
|
|
26
23
|
loadedComponentName: void 0,
|
|
27
24
|
loadedComponentPage: void 0,
|
|
28
25
|
currentProps: {}
|
|
@@ -30,34 +27,34 @@ function RouterInner(props) {
|
|
|
30
27
|
);
|
|
31
28
|
const setLoadedComponent = useCallback(() => {
|
|
32
29
|
const { loadedComponentName, loadedComponentPage } = config;
|
|
33
|
-
const { currentRoute, isRedirecting } = props.
|
|
30
|
+
const { currentRoute, isRedirecting } = props.router;
|
|
34
31
|
const componentConfig = props.routes[currentRoute.name];
|
|
35
32
|
let preventRedirect = false;
|
|
36
33
|
if (isRedirecting) preventRedirect = true;
|
|
37
34
|
else if (loadedComponentName === currentRoute.name) preventRedirect = true;
|
|
38
35
|
else if (loadedComponentPage != null && currentRoute.name != null) {
|
|
39
|
-
if (loadedComponentPage === currentRoute.
|
|
40
|
-
props.
|
|
36
|
+
if (loadedComponentPage === currentRoute.pageId) {
|
|
37
|
+
props.router.adapters.batch(() => {
|
|
41
38
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
42
39
|
});
|
|
43
40
|
preventRedirect = true;
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
if (preventRedirect) return;
|
|
47
|
-
props.
|
|
44
|
+
props.router.adapters.batch(() => {
|
|
48
45
|
if (loadedComponentName) props.beforeUpdatePageComponent?.();
|
|
49
46
|
props.beforeSetPageComponent?.(componentConfig);
|
|
50
47
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
51
48
|
config.loadedComponentName = currentRoute.name;
|
|
52
|
-
config.loadedComponentPage = componentConfig.
|
|
49
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
53
50
|
});
|
|
54
51
|
}, []);
|
|
55
52
|
useState(() => {
|
|
56
|
-
props.
|
|
53
|
+
props.router.adapters.batch(() => {
|
|
57
54
|
props.beforeMount?.();
|
|
58
55
|
redirectOnHistoryPop();
|
|
59
56
|
setLoadedComponent();
|
|
60
|
-
disposerRef.current = props.
|
|
57
|
+
disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
|
|
61
58
|
});
|
|
62
59
|
});
|
|
63
60
|
useEffect(() => {
|
|
@@ -67,12 +64,13 @@ function RouterInner(props) {
|
|
|
67
64
|
}, []);
|
|
68
65
|
if (!config.loadedComponentName) return null;
|
|
69
66
|
const LoadedComponent = props.routes[config.loadedComponentName]?.component || null;
|
|
70
|
-
if (LoadedComponent)
|
|
67
|
+
if (LoadedComponent)
|
|
68
|
+
return /* @__PURE__ */ jsx(LoadedComponent, { ...config.currentProps, router: props.router });
|
|
71
69
|
return null;
|
|
72
70
|
}
|
|
73
71
|
function Router(props) {
|
|
74
72
|
const [Component] = useState(
|
|
75
|
-
() => props.
|
|
73
|
+
() => props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner
|
|
76
74
|
);
|
|
77
75
|
return /* @__PURE__ */ jsx(Component, { ...props });
|
|
78
76
|
}
|
package/dist/esm/react/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/react/Router.tsx
|
|
2
2
|
import { memo, useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { history } from "reactive-route";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
function RouterInner(props) {
|
|
6
6
|
const disposerRef = useRef(null);
|
|
@@ -8,21 +8,18 @@ function RouterInner(props) {
|
|
|
8
8
|
if (!history) return;
|
|
9
9
|
history.listen((params) => {
|
|
10
10
|
if (params.action !== "POP") return;
|
|
11
|
-
const previousRoutePathname = props.
|
|
11
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
12
12
|
if (previousRoutePathname === params.location.pathname) {
|
|
13
|
-
props.
|
|
13
|
+
props.router.adapters.batch(() => props.router.routesHistory.pop());
|
|
14
14
|
}
|
|
15
|
-
void props.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
routes: props.routes,
|
|
19
|
-
pathname: history.location.pathname
|
|
20
|
-
})
|
|
15
|
+
void props.router.restoreFromURL({
|
|
16
|
+
pathname: history.location.pathname,
|
|
17
|
+
noHistoryPush: true
|
|
21
18
|
});
|
|
22
19
|
});
|
|
23
20
|
}, []);
|
|
24
21
|
const [config] = useState(
|
|
25
|
-
() => props.
|
|
22
|
+
() => props.router.adapters.makeObservable({
|
|
26
23
|
loadedComponentName: void 0,
|
|
27
24
|
loadedComponentPage: void 0,
|
|
28
25
|
currentProps: {}
|
|
@@ -30,34 +27,34 @@ function RouterInner(props) {
|
|
|
30
27
|
);
|
|
31
28
|
const setLoadedComponent = useCallback(() => {
|
|
32
29
|
const { loadedComponentName, loadedComponentPage } = config;
|
|
33
|
-
const { currentRoute, isRedirecting } = props.
|
|
30
|
+
const { currentRoute, isRedirecting } = props.router;
|
|
34
31
|
const componentConfig = props.routes[currentRoute.name];
|
|
35
32
|
let preventRedirect = false;
|
|
36
33
|
if (isRedirecting) preventRedirect = true;
|
|
37
34
|
else if (loadedComponentName === currentRoute.name) preventRedirect = true;
|
|
38
35
|
else if (loadedComponentPage != null && currentRoute.name != null) {
|
|
39
|
-
if (loadedComponentPage === currentRoute.
|
|
40
|
-
props.
|
|
36
|
+
if (loadedComponentPage === currentRoute.pageId) {
|
|
37
|
+
props.router.adapters.batch(() => {
|
|
41
38
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
42
39
|
});
|
|
43
40
|
preventRedirect = true;
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
if (preventRedirect) return;
|
|
47
|
-
props.
|
|
44
|
+
props.router.adapters.batch(() => {
|
|
48
45
|
if (loadedComponentName) props.beforeUpdatePageComponent?.();
|
|
49
46
|
props.beforeSetPageComponent?.(componentConfig);
|
|
50
47
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
51
48
|
config.loadedComponentName = currentRoute.name;
|
|
52
|
-
config.loadedComponentPage = componentConfig.
|
|
49
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
53
50
|
});
|
|
54
51
|
}, []);
|
|
55
52
|
useState(() => {
|
|
56
|
-
props.
|
|
53
|
+
props.router.adapters.batch(() => {
|
|
57
54
|
props.beforeMount?.();
|
|
58
55
|
redirectOnHistoryPop();
|
|
59
56
|
setLoadedComponent();
|
|
60
|
-
disposerRef.current = props.
|
|
57
|
+
disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
|
|
61
58
|
});
|
|
62
59
|
});
|
|
63
60
|
useEffect(() => {
|
|
@@ -67,11 +64,12 @@ function RouterInner(props) {
|
|
|
67
64
|
}, []);
|
|
68
65
|
if (!config.loadedComponentName) return null;
|
|
69
66
|
const LoadedComponent = props.routes[config.loadedComponentName]?.component || null;
|
|
70
|
-
if (LoadedComponent)
|
|
67
|
+
if (LoadedComponent)
|
|
68
|
+
return /* @__PURE__ */ jsx(LoadedComponent, { ...config.currentProps, router: props.router });
|
|
71
69
|
return null;
|
|
72
70
|
}
|
|
73
71
|
function RouterWrapper(props) {
|
|
74
|
-
const Component = props.
|
|
72
|
+
const Component = props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner;
|
|
75
73
|
return /* @__PURE__ */ jsx(Component, { ...props });
|
|
76
74
|
}
|
|
77
75
|
var Router = memo(RouterWrapper);
|
package/dist/esm/solid/index.js
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
1
|
// packages/solid/Router.tsx
|
|
2
2
|
import { createComponent as _$createComponent } from "solid-js/web";
|
|
3
3
|
import { mergeProps as _$mergeProps } from "solid-js/web";
|
|
4
|
-
import {
|
|
4
|
+
import { history } from "reactive-route";
|
|
5
5
|
import { Show } from "solid-js";
|
|
6
6
|
import { Dynamic } from "solid-js/web";
|
|
7
7
|
function Router(props) {
|
|
8
|
-
const config = props.
|
|
8
|
+
const config = props.router.adapters.makeObservable({
|
|
9
9
|
loadedComponentName: void 0,
|
|
10
10
|
loadedComponentPage: void 0,
|
|
11
11
|
currentProps: {}
|
|
12
12
|
});
|
|
13
|
+
let currentProps = {};
|
|
13
14
|
function redirectOnHistoryPop() {
|
|
14
15
|
if (!history) return;
|
|
15
16
|
history.listen((params) => {
|
|
16
17
|
if (params.action !== "POP") return;
|
|
17
|
-
const previousRoutePathname = props.
|
|
18
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
18
19
|
if (previousRoutePathname === params.location.pathname) {
|
|
19
|
-
props.
|
|
20
|
+
props.router.routesHistory.pop();
|
|
20
21
|
}
|
|
21
|
-
void props.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
routes: props.routes,
|
|
25
|
-
pathname: history.location.pathname
|
|
26
|
-
})
|
|
22
|
+
void props.router.restoreFromURL({
|
|
23
|
+
pathname: history.location.pathname,
|
|
24
|
+
noHistoryPush: true
|
|
27
25
|
});
|
|
28
26
|
});
|
|
29
27
|
}
|
|
30
28
|
function setLoadedComponent() {
|
|
31
|
-
const currentRouteName = props.
|
|
32
|
-
const currentRoutePage = props.
|
|
29
|
+
const currentRouteName = props.router.currentRoute.name;
|
|
30
|
+
const currentRoutePage = props.router.currentRoute.pageId;
|
|
33
31
|
const componentConfig = props.routes[currentRouteName];
|
|
34
32
|
let preventRedirect = false;
|
|
35
|
-
if (props.
|
|
33
|
+
if (props.router.isRedirecting) preventRedirect = true;
|
|
36
34
|
else if (config.loadedComponentName === currentRouteName) {
|
|
37
35
|
preventRedirect = true;
|
|
38
36
|
} else if (config.loadedComponentPage != null && currentRouteName != null) {
|
|
39
37
|
if (config.loadedComponentPage === currentRoutePage) {
|
|
40
|
-
props.
|
|
38
|
+
props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
39
|
+
currentProps = "props" in componentConfig ? componentConfig.props : {};
|
|
41
40
|
preventRedirect = true;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
if (preventRedirect) return;
|
|
45
|
-
props.
|
|
44
|
+
props.router.adapters.batch(() => {
|
|
46
45
|
if (config.loadedComponentName) {
|
|
47
46
|
props.beforeUpdatePageComponent?.();
|
|
48
47
|
}
|
|
49
48
|
props.beforeSetPageComponent?.(componentConfig);
|
|
50
|
-
props.
|
|
51
|
-
props.
|
|
49
|
+
props.router.adapters.batch(() => {
|
|
50
|
+
props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
51
|
+
currentProps = "props" in componentConfig ? componentConfig.props : {};
|
|
52
52
|
config.loadedComponentName = currentRouteName;
|
|
53
|
-
config.loadedComponentPage = componentConfig.
|
|
53
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
54
54
|
});
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
props.beforeMount?.();
|
|
58
58
|
redirectOnHistoryPop();
|
|
59
|
-
if (props.
|
|
60
|
-
props.
|
|
59
|
+
if (props.router.adapters.immediateSetComponent) {
|
|
60
|
+
props.router.adapters.batch(() => {
|
|
61
61
|
setLoadedComponent();
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
|
-
props.
|
|
64
|
+
props.router.adapters.autorun(() => setLoadedComponent());
|
|
65
65
|
return (
|
|
66
66
|
// @ts-ignore
|
|
67
67
|
_$createComponent(Show, {
|
|
@@ -73,7 +73,14 @@ function Router(props) {
|
|
|
73
73
|
get component() {
|
|
74
74
|
return props.routes[config.loadedComponentName]?.component || void 0;
|
|
75
75
|
}
|
|
76
|
-
}, () => config.currentProps))
|
|
76
|
+
}, () => Object.keys(config.currentProps).reduce((acc, key) => {
|
|
77
|
+
acc[key] = currentProps[key];
|
|
78
|
+
return acc;
|
|
79
|
+
}, {}), {
|
|
80
|
+
get router() {
|
|
81
|
+
return props.router;
|
|
82
|
+
}
|
|
83
|
+
}));
|
|
77
84
|
}
|
|
78
85
|
})
|
|
79
86
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/preact/Router.tsx"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/preact/Router.tsx"],"names":[],"mappings":"AACA,OAAO,EAAW,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAgGrE,wBAAgB,MAAM,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,GAK1D,GAAG,CACzC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/react/Router.tsx"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/react/Router.tsx"],"names":[],"mappings":"AACA,OAAO,EAAW,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAgGrE,iBAAS,aAAa,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,GAK1D,GAAG,CACzC;AAED,eAAO,MAAM,MAAM,EAA0B,OAAO,aAAa,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/solid/Router.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../packages/solid/Router.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAW,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAIrE,wBAAgB,MAAM,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,kCAsGhG"}
|