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/README.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/reactive-route)
|
|
5
5
|

|
|
6
6
|

|
|
7
|
+

|
|
7
8
|

|
|
8
9
|
|
|
9
10
|
> [!WARNING]
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,53 +15,25 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// packages/core/index.ts
|
|
31
21
|
var core_exports = {};
|
|
32
22
|
__export(core_exports, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
getInitialRoute: () => getInitialRoute,
|
|
23
|
+
RedirectError: () => RedirectError,
|
|
24
|
+
createRouter: () => createRouter,
|
|
25
|
+
createRoutes: () => createRoutes,
|
|
37
26
|
history: () => history,
|
|
38
|
-
isDynamicRoute: () => isDynamicRoute,
|
|
39
27
|
loadComponentToConfig: () => loadComponentToConfig,
|
|
40
28
|
replaceDynamicValues: () => replaceDynamicValues
|
|
41
29
|
});
|
|
42
30
|
module.exports = __toCommonJS(core_exports);
|
|
43
31
|
|
|
44
|
-
// packages/core/utils/addNames.ts
|
|
45
|
-
function addNames(obj) {
|
|
46
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
47
|
-
value.name = key;
|
|
48
|
-
});
|
|
49
|
-
return obj;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// packages/core/createRouterConfig.ts
|
|
53
|
-
function createRouterConfig(config) {
|
|
54
|
-
return addNames(config);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// packages/core/createRouterStore.ts
|
|
58
|
-
var import_query_string2 = __toESM(require("query-string"));
|
|
59
|
-
|
|
60
32
|
// packages/core/utils/constants.ts
|
|
61
33
|
var constants = {
|
|
62
34
|
dynamicSeparator: ":",
|
|
63
35
|
pathPartSeparator: "/",
|
|
64
|
-
isClient: typeof window !== "undefined"
|
|
65
|
-
errorRedirect: "REDIRECT",
|
|
66
|
-
errorPrevent: "PREVENT_REDIRECT"
|
|
36
|
+
isClient: typeof window !== "undefined"
|
|
67
37
|
};
|
|
68
38
|
|
|
69
39
|
// packages/core/utils/dynamic.ts
|
|
@@ -73,9 +43,6 @@ function isDynamic(param) {
|
|
|
73
43
|
function clearDynamic(param) {
|
|
74
44
|
return param.replace(new RegExp(`^${constants.dynamicSeparator}`), "");
|
|
75
45
|
}
|
|
76
|
-
function isDynamicRoute(route) {
|
|
77
|
-
return "params" in route;
|
|
78
|
-
}
|
|
79
46
|
|
|
80
47
|
// packages/core/utils/getDynamicValues.ts
|
|
81
48
|
function getDynamicValues(params) {
|
|
@@ -121,18 +88,34 @@ function findRouteByPathname({
|
|
|
121
88
|
return dynamicRouteMatch;
|
|
122
89
|
}
|
|
123
90
|
|
|
124
|
-
// packages/core/utils/getQueryValues.ts
|
|
125
|
-
var import_query_string = __toESM(require("query-string"));
|
|
126
|
-
|
|
127
91
|
// packages/core/utils/getTypedEntries.ts
|
|
128
92
|
var getTypedEntries = Object.entries;
|
|
129
93
|
|
|
94
|
+
// packages/core/utils/queryString.ts
|
|
95
|
+
function removeHash(input) {
|
|
96
|
+
const hashStart = input.indexOf("#");
|
|
97
|
+
return hashStart === -1 ? input : input.slice(0, hashStart);
|
|
98
|
+
}
|
|
99
|
+
var queryString = {
|
|
100
|
+
extract(input) {
|
|
101
|
+
const inputNoHash = removeHash(input);
|
|
102
|
+
const queryStart = inputNoHash.indexOf("?");
|
|
103
|
+
return queryStart === -1 ? "" : inputNoHash.slice(queryStart + 1);
|
|
104
|
+
},
|
|
105
|
+
parse(input) {
|
|
106
|
+
return Object.fromEntries(new URLSearchParams(input));
|
|
107
|
+
},
|
|
108
|
+
stringify(obj) {
|
|
109
|
+
return new URLSearchParams(obj).toString();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
130
113
|
// packages/core/utils/getQueryValues.ts
|
|
131
114
|
function getQueryValues(params) {
|
|
132
115
|
const { route, pathname } = params;
|
|
133
|
-
const qs =
|
|
116
|
+
const qs = queryString.extract(pathname);
|
|
134
117
|
if (!qs || !route.query) return {};
|
|
135
|
-
const query =
|
|
118
|
+
const query = queryString.parse(qs);
|
|
136
119
|
getTypedEntries(query).forEach(([key, value]) => {
|
|
137
120
|
const validator = route.query[key];
|
|
138
121
|
if (typeof validator !== "function" || !validator(value)) {
|
|
@@ -162,15 +145,30 @@ function loadComponentToConfig(params) {
|
|
|
162
145
|
if (!route.component && route.loader) {
|
|
163
146
|
const loadingFn = route.loader;
|
|
164
147
|
return loadingFn().then((module2) => {
|
|
165
|
-
const { default: component,
|
|
148
|
+
const { default: component, ...rest } = module2;
|
|
166
149
|
route.component = component;
|
|
167
150
|
route.otherExports = rest;
|
|
168
|
-
route.pageName = pageName;
|
|
169
151
|
});
|
|
170
152
|
}
|
|
171
153
|
return Promise.resolve();
|
|
172
154
|
}
|
|
173
155
|
|
|
156
|
+
// packages/core/utils/PreventError.ts
|
|
157
|
+
var PreventError = class extends Error {
|
|
158
|
+
constructor(message) {
|
|
159
|
+
super(message);
|
|
160
|
+
this.name = "PreventError";
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// packages/core/utils/RedirectError.ts
|
|
165
|
+
var RedirectError = class extends Error {
|
|
166
|
+
constructor(message) {
|
|
167
|
+
super(message);
|
|
168
|
+
this.name = "RedirectError";
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
174
172
|
// packages/core/utils/replaceDynamicValues.ts
|
|
175
173
|
var re = new RegExp(`[^${constants.pathPartSeparator}]+`, "g");
|
|
176
174
|
function replaceDynamicValues({
|
|
@@ -189,53 +187,52 @@ function replaceDynamicValues({
|
|
|
189
187
|
});
|
|
190
188
|
}
|
|
191
189
|
|
|
192
|
-
// packages/core/
|
|
193
|
-
function
|
|
194
|
-
adapters,
|
|
190
|
+
// packages/core/createRouter.ts
|
|
191
|
+
function createRouter({
|
|
195
192
|
routes,
|
|
193
|
+
adapters,
|
|
196
194
|
lifecycleParams
|
|
197
195
|
}) {
|
|
198
|
-
const
|
|
196
|
+
const router = adapters.makeObservable({
|
|
199
197
|
routesHistory: [],
|
|
200
198
|
currentRoute: {},
|
|
201
199
|
isRedirecting: false,
|
|
202
|
-
|
|
200
|
+
redirect: void 0,
|
|
203
201
|
restoreFromURL: void 0,
|
|
204
202
|
restoreFromServer: void 0,
|
|
205
|
-
adapters
|
|
203
|
+
get adapters() {
|
|
204
|
+
return adapters;
|
|
205
|
+
}
|
|
206
206
|
});
|
|
207
|
-
|
|
207
|
+
router.restoreFromServer = function restoreFromServer(obj) {
|
|
208
208
|
adapters.batch(() => {
|
|
209
|
-
|
|
210
|
-
Object.assign(
|
|
209
|
+
router.routesHistory.push(...obj.routesHistory || []);
|
|
210
|
+
Object.assign(router.currentRoute, obj.currentRoute);
|
|
211
211
|
});
|
|
212
212
|
const preloadedRouteName = Object.keys(routes).find(
|
|
213
|
-
(routeName) =>
|
|
213
|
+
(routeName) => router.currentRoute.name === routeName
|
|
214
214
|
);
|
|
215
215
|
return loadComponentToConfig({ route: routes[preloadedRouteName] });
|
|
216
216
|
};
|
|
217
|
-
|
|
218
|
-
return
|
|
217
|
+
router.restoreFromURL = function restoreFromURL(params) {
|
|
218
|
+
return router.redirect(getInitialRoute({ routes, ...params }));
|
|
219
219
|
};
|
|
220
|
-
|
|
220
|
+
router.redirect = async function redirect(config) {
|
|
221
221
|
const { route: routeName, noHistoryPush } = config;
|
|
222
222
|
let currentRoute;
|
|
223
223
|
let currentPathname;
|
|
224
224
|
let currentUrl;
|
|
225
225
|
let currentSearch;
|
|
226
226
|
let currentQuery;
|
|
227
|
-
if (
|
|
228
|
-
currentRoute = routes[
|
|
227
|
+
if (router.currentRoute?.name) {
|
|
228
|
+
currentRoute = routes[router.currentRoute.name];
|
|
229
229
|
currentPathname = replaceDynamicValues({
|
|
230
230
|
route: currentRoute,
|
|
231
|
-
params:
|
|
231
|
+
params: router.currentRoute.params
|
|
232
232
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
});
|
|
237
|
-
currentQuery = routerStore.currentRoute.query;
|
|
238
|
-
currentSearch = import_query_string2.default.stringify(routerStore.currentRoute.query);
|
|
233
|
+
currentQuery = router.currentRoute.query;
|
|
234
|
+
currentSearch = queryString.stringify(router.currentRoute.query);
|
|
235
|
+
currentUrl = `${currentPathname}${currentSearch ? `?${currentSearch}` : ""}`;
|
|
239
236
|
}
|
|
240
237
|
const nextRoute = routes[routeName];
|
|
241
238
|
const nextPathname = replaceDynamicValues({
|
|
@@ -248,23 +245,23 @@ function createRouterStore({
|
|
|
248
245
|
if ("query" in config && config.query) {
|
|
249
246
|
const clearedQuery = getQueryValues({
|
|
250
247
|
route: nextRoute,
|
|
251
|
-
pathname: `${nextPathname}?${
|
|
248
|
+
pathname: `${nextPathname}?${queryString.stringify(config.query)}`
|
|
252
249
|
});
|
|
253
250
|
if (Object.keys(clearedQuery).length > 0) {
|
|
254
251
|
nextQuery = clearedQuery;
|
|
255
|
-
nextSearch =
|
|
256
|
-
nextUrl =
|
|
252
|
+
nextSearch = queryString.stringify(clearedQuery);
|
|
253
|
+
nextUrl = `${nextPathname}?${nextSearch}`;
|
|
257
254
|
}
|
|
258
255
|
}
|
|
259
256
|
if (currentUrl === nextUrl) return Promise.resolve();
|
|
260
257
|
if (currentPathname === nextPathname) {
|
|
261
258
|
if (currentSearch !== nextSearch) {
|
|
262
259
|
adapters.batch(() => {
|
|
263
|
-
adapters.replaceObject(
|
|
264
|
-
...
|
|
260
|
+
adapters.replaceObject(router.currentRoute, {
|
|
261
|
+
...router.currentRoute,
|
|
265
262
|
query: nextQuery || {}
|
|
266
263
|
});
|
|
267
|
-
|
|
264
|
+
router.routesHistory.push(nextUrl);
|
|
268
265
|
});
|
|
269
266
|
if (history && !noHistoryPush) {
|
|
270
267
|
history.push({
|
|
@@ -277,7 +274,7 @@ function createRouterStore({
|
|
|
277
274
|
return Promise.resolve();
|
|
278
275
|
}
|
|
279
276
|
adapters.batch(() => {
|
|
280
|
-
|
|
277
|
+
router.isRedirecting = true;
|
|
281
278
|
});
|
|
282
279
|
try {
|
|
283
280
|
const config2 = {
|
|
@@ -290,73 +287,92 @@ function createRouterStore({
|
|
|
290
287
|
currentQuery,
|
|
291
288
|
currentRoute,
|
|
292
289
|
currentSearch,
|
|
293
|
-
currentPathname
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
let redirectUrl = replaceDynamicValues({
|
|
302
|
-
params: redirectParams,
|
|
303
|
-
route: redirectRoute
|
|
304
|
-
});
|
|
305
|
-
if ("query" in redirectConfig && redirectConfig.query) {
|
|
306
|
-
const clearedQuery = getQueryValues({
|
|
307
|
-
route: nextRoute,
|
|
308
|
-
pathname: `${nextPathname}?${import_query_string2.default.stringify(redirectConfig.query)}`
|
|
290
|
+
currentPathname,
|
|
291
|
+
redirect: (redirectConfig2) => {
|
|
292
|
+
if (constants.isClient) return redirectConfig2;
|
|
293
|
+
const redirectRoute = routes[redirectConfig2.route];
|
|
294
|
+
const redirectParams = "params" in redirectConfig2 && redirectConfig2.params ? redirectConfig2.params : void 0;
|
|
295
|
+
let redirectUrl = replaceDynamicValues({
|
|
296
|
+
params: redirectParams,
|
|
297
|
+
route: redirectRoute
|
|
309
298
|
});
|
|
310
|
-
if (
|
|
311
|
-
|
|
299
|
+
if ("query" in redirectConfig2 && redirectConfig2.query) {
|
|
300
|
+
const clearedQuery = getQueryValues({
|
|
301
|
+
route: nextRoute,
|
|
302
|
+
pathname: `${nextPathname}?${queryString.stringify(redirectConfig2.query)}`
|
|
303
|
+
});
|
|
304
|
+
if (Object.keys(clearedQuery).length > 0) {
|
|
305
|
+
redirectUrl = `${redirectUrl}?${queryString.stringify(clearedQuery)}`;
|
|
306
|
+
}
|
|
312
307
|
}
|
|
308
|
+
throw new RedirectError(redirectUrl);
|
|
309
|
+
},
|
|
310
|
+
preventRedirect: () => {
|
|
311
|
+
throw new PreventError(`Redirect to ${nextUrl} was prevented`);
|
|
313
312
|
}
|
|
314
|
-
|
|
315
|
-
|
|
313
|
+
};
|
|
314
|
+
await currentRoute?.beforeLeave?.(config2, ...lifecycleParams || []);
|
|
315
|
+
const redirectConfig = await nextRoute.beforeEnter?.(config2, ...lifecycleParams || []);
|
|
316
|
+
if (redirectConfig) return redirect(redirectConfig);
|
|
316
317
|
await loadComponentToConfig({ route: routes[nextRoute.name] });
|
|
317
318
|
} catch (error) {
|
|
318
|
-
if (error
|
|
319
|
-
|
|
319
|
+
if (error instanceof PreventError) {
|
|
320
|
+
return Promise.resolve();
|
|
321
|
+
}
|
|
322
|
+
if (error instanceof RedirectError) {
|
|
320
323
|
throw error;
|
|
321
324
|
}
|
|
322
325
|
console.error(error);
|
|
323
326
|
await loadComponentToConfig({ route: routes.internalError });
|
|
324
327
|
adapters.batch(() => {
|
|
325
|
-
adapters.replaceObject(
|
|
328
|
+
adapters.replaceObject(router.currentRoute, {
|
|
326
329
|
name: routes.internalError.name,
|
|
327
330
|
path: routes.internalError.path,
|
|
328
331
|
props: routes[routes.internalError.name].props,
|
|
329
332
|
query: adapters.makeObservable({}),
|
|
330
333
|
params: adapters.makeObservable({}),
|
|
331
|
-
|
|
334
|
+
pageId: routes[routes.internalError.name].pageId
|
|
332
335
|
});
|
|
333
|
-
|
|
336
|
+
router.isRedirecting = false;
|
|
334
337
|
});
|
|
335
338
|
return Promise.resolve();
|
|
336
339
|
}
|
|
337
340
|
adapters.batch(() => {
|
|
338
|
-
adapters.replaceObject(
|
|
341
|
+
adapters.replaceObject(router.currentRoute, {
|
|
339
342
|
name: nextRoute.name,
|
|
340
343
|
path: nextRoute.path,
|
|
341
344
|
props: routes[nextRoute.name].props,
|
|
342
345
|
query: getQueryValues({ route: nextRoute, pathname: nextUrl }),
|
|
343
346
|
params: getDynamicValues({ route: nextRoute, pathname: nextUrl }),
|
|
344
|
-
|
|
347
|
+
pageId: routes[nextRoute.name].pageId
|
|
345
348
|
});
|
|
346
|
-
const lastUrl =
|
|
349
|
+
const lastUrl = router.routesHistory[router.routesHistory.length - 1];
|
|
347
350
|
if (lastUrl !== nextUrl) {
|
|
348
|
-
|
|
351
|
+
router.routesHistory.push(nextUrl);
|
|
349
352
|
}
|
|
350
353
|
if (history && !noHistoryPush) {
|
|
351
354
|
history.push({
|
|
352
355
|
hash: history.location.hash,
|
|
353
|
-
search: "query" in config ? `?${
|
|
356
|
+
search: "query" in config ? `?${queryString.stringify(config.query)}` : "",
|
|
354
357
|
pathname: nextPathname
|
|
355
358
|
});
|
|
356
359
|
}
|
|
357
|
-
|
|
360
|
+
router.isRedirecting = false;
|
|
358
361
|
});
|
|
359
362
|
return Promise.resolve();
|
|
360
363
|
};
|
|
361
|
-
return
|
|
364
|
+
return router;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// packages/core/utils/addNames.ts
|
|
368
|
+
function addNames(obj) {
|
|
369
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
370
|
+
value.name = key;
|
|
371
|
+
});
|
|
372
|
+
return obj;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// packages/core/createRoutes.ts
|
|
376
|
+
function createRoutes(config) {
|
|
377
|
+
return addNames(config);
|
|
362
378
|
}
|
package/dist/cjs/preact/index.js
CHANGED
|
@@ -34,21 +34,18 @@ function RouterInner(props) {
|
|
|
34
34
|
if (!import_reactive_route.history) return;
|
|
35
35
|
import_reactive_route.history.listen((params) => {
|
|
36
36
|
if (params.action !== "POP") return;
|
|
37
|
-
const previousRoutePathname = props.
|
|
37
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
38
38
|
if (previousRoutePathname === params.location.pathname) {
|
|
39
|
-
props.
|
|
39
|
+
props.router.adapters.batch(() => props.router.routesHistory.pop());
|
|
40
40
|
}
|
|
41
|
-
void props.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
routes: props.routes,
|
|
45
|
-
pathname: import_reactive_route.history.location.pathname
|
|
46
|
-
})
|
|
41
|
+
void props.router.restoreFromURL({
|
|
42
|
+
pathname: import_reactive_route.history.location.pathname,
|
|
43
|
+
noHistoryPush: true
|
|
47
44
|
});
|
|
48
45
|
});
|
|
49
46
|
}, []);
|
|
50
47
|
const [config] = (0, import_hooks.useState)(
|
|
51
|
-
() => props.
|
|
48
|
+
() => props.router.adapters.makeObservable({
|
|
52
49
|
loadedComponentName: void 0,
|
|
53
50
|
loadedComponentPage: void 0,
|
|
54
51
|
currentProps: {}
|
|
@@ -56,34 +53,34 @@ function RouterInner(props) {
|
|
|
56
53
|
);
|
|
57
54
|
const setLoadedComponent = (0, import_hooks.useCallback)(() => {
|
|
58
55
|
const { loadedComponentName, loadedComponentPage } = config;
|
|
59
|
-
const { currentRoute, isRedirecting } = props.
|
|
56
|
+
const { currentRoute, isRedirecting } = props.router;
|
|
60
57
|
const componentConfig = props.routes[currentRoute.name];
|
|
61
58
|
let preventRedirect = false;
|
|
62
59
|
if (isRedirecting) preventRedirect = true;
|
|
63
60
|
else if (loadedComponentName === currentRoute.name) preventRedirect = true;
|
|
64
61
|
else if (loadedComponentPage != null && currentRoute.name != null) {
|
|
65
|
-
if (loadedComponentPage === currentRoute.
|
|
66
|
-
props.
|
|
62
|
+
if (loadedComponentPage === currentRoute.pageId) {
|
|
63
|
+
props.router.adapters.batch(() => {
|
|
67
64
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
68
65
|
});
|
|
69
66
|
preventRedirect = true;
|
|
70
67
|
}
|
|
71
68
|
}
|
|
72
69
|
if (preventRedirect) return;
|
|
73
|
-
props.
|
|
70
|
+
props.router.adapters.batch(() => {
|
|
74
71
|
if (loadedComponentName) props.beforeUpdatePageComponent?.();
|
|
75
72
|
props.beforeSetPageComponent?.(componentConfig);
|
|
76
73
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
77
74
|
config.loadedComponentName = currentRoute.name;
|
|
78
|
-
config.loadedComponentPage = componentConfig.
|
|
75
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
79
76
|
});
|
|
80
77
|
}, []);
|
|
81
78
|
(0, import_hooks.useState)(() => {
|
|
82
|
-
props.
|
|
79
|
+
props.router.adapters.batch(() => {
|
|
83
80
|
props.beforeMount?.();
|
|
84
81
|
redirectOnHistoryPop();
|
|
85
82
|
setLoadedComponent();
|
|
86
|
-
disposerRef.current = props.
|
|
83
|
+
disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
|
|
87
84
|
});
|
|
88
85
|
});
|
|
89
86
|
(0, import_hooks.useEffect)(() => {
|
|
@@ -93,12 +90,13 @@ function RouterInner(props) {
|
|
|
93
90
|
}, []);
|
|
94
91
|
if (!config.loadedComponentName) return null;
|
|
95
92
|
const LoadedComponent = props.routes[config.loadedComponentName]?.component || null;
|
|
96
|
-
if (LoadedComponent)
|
|
93
|
+
if (LoadedComponent)
|
|
94
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadedComponent, { ...config.currentProps, router: props.router });
|
|
97
95
|
return null;
|
|
98
96
|
}
|
|
99
97
|
function Router(props) {
|
|
100
98
|
const [Component] = (0, import_hooks.useState)(
|
|
101
|
-
() => props.
|
|
99
|
+
() => props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner
|
|
102
100
|
);
|
|
103
101
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props });
|
|
104
102
|
}
|
package/dist/cjs/react/index.js
CHANGED
|
@@ -34,21 +34,18 @@ function RouterInner(props) {
|
|
|
34
34
|
if (!import_reactive_route.history) return;
|
|
35
35
|
import_reactive_route.history.listen((params) => {
|
|
36
36
|
if (params.action !== "POP") return;
|
|
37
|
-
const previousRoutePathname = props.
|
|
37
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
38
38
|
if (previousRoutePathname === params.location.pathname) {
|
|
39
|
-
props.
|
|
39
|
+
props.router.adapters.batch(() => props.router.routesHistory.pop());
|
|
40
40
|
}
|
|
41
|
-
void props.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
routes: props.routes,
|
|
45
|
-
pathname: import_reactive_route.history.location.pathname
|
|
46
|
-
})
|
|
41
|
+
void props.router.restoreFromURL({
|
|
42
|
+
pathname: import_reactive_route.history.location.pathname,
|
|
43
|
+
noHistoryPush: true
|
|
47
44
|
});
|
|
48
45
|
});
|
|
49
46
|
}, []);
|
|
50
47
|
const [config] = (0, import_react.useState)(
|
|
51
|
-
() => props.
|
|
48
|
+
() => props.router.adapters.makeObservable({
|
|
52
49
|
loadedComponentName: void 0,
|
|
53
50
|
loadedComponentPage: void 0,
|
|
54
51
|
currentProps: {}
|
|
@@ -56,34 +53,34 @@ function RouterInner(props) {
|
|
|
56
53
|
);
|
|
57
54
|
const setLoadedComponent = (0, import_react.useCallback)(() => {
|
|
58
55
|
const { loadedComponentName, loadedComponentPage } = config;
|
|
59
|
-
const { currentRoute, isRedirecting } = props.
|
|
56
|
+
const { currentRoute, isRedirecting } = props.router;
|
|
60
57
|
const componentConfig = props.routes[currentRoute.name];
|
|
61
58
|
let preventRedirect = false;
|
|
62
59
|
if (isRedirecting) preventRedirect = true;
|
|
63
60
|
else if (loadedComponentName === currentRoute.name) preventRedirect = true;
|
|
64
61
|
else if (loadedComponentPage != null && currentRoute.name != null) {
|
|
65
|
-
if (loadedComponentPage === currentRoute.
|
|
66
|
-
props.
|
|
62
|
+
if (loadedComponentPage === currentRoute.pageId) {
|
|
63
|
+
props.router.adapters.batch(() => {
|
|
67
64
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
68
65
|
});
|
|
69
66
|
preventRedirect = true;
|
|
70
67
|
}
|
|
71
68
|
}
|
|
72
69
|
if (preventRedirect) return;
|
|
73
|
-
props.
|
|
70
|
+
props.router.adapters.batch(() => {
|
|
74
71
|
if (loadedComponentName) props.beforeUpdatePageComponent?.();
|
|
75
72
|
props.beforeSetPageComponent?.(componentConfig);
|
|
76
73
|
config.currentProps = "props" in componentConfig ? componentConfig.props || {} : {};
|
|
77
74
|
config.loadedComponentName = currentRoute.name;
|
|
78
|
-
config.loadedComponentPage = componentConfig.
|
|
75
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
79
76
|
});
|
|
80
77
|
}, []);
|
|
81
78
|
(0, import_react.useState)(() => {
|
|
82
|
-
props.
|
|
79
|
+
props.router.adapters.batch(() => {
|
|
83
80
|
props.beforeMount?.();
|
|
84
81
|
redirectOnHistoryPop();
|
|
85
82
|
setLoadedComponent();
|
|
86
|
-
disposerRef.current = props.
|
|
83
|
+
disposerRef.current = props.router.adapters.autorun(setLoadedComponent);
|
|
87
84
|
});
|
|
88
85
|
});
|
|
89
86
|
(0, import_react.useEffect)(() => {
|
|
@@ -93,11 +90,12 @@ function RouterInner(props) {
|
|
|
93
90
|
}, []);
|
|
94
91
|
if (!config.loadedComponentName) return null;
|
|
95
92
|
const LoadedComponent = props.routes[config.loadedComponentName]?.component || null;
|
|
96
|
-
if (LoadedComponent)
|
|
93
|
+
if (LoadedComponent)
|
|
94
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadedComponent, { ...config.currentProps, router: props.router });
|
|
97
95
|
return null;
|
|
98
96
|
}
|
|
99
97
|
function RouterWrapper(props) {
|
|
100
|
-
const Component = props.
|
|
98
|
+
const Component = props.router.adapters.observer ? props.router.adapters.observer(RouterInner) : RouterInner;
|
|
101
99
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props });
|
|
102
100
|
}
|
|
103
101
|
var Router = (0, import_react.memo)(RouterWrapper);
|
package/dist/cjs/solid/index.js
CHANGED
|
@@ -31,63 +31,63 @@ var import_reactive_route = require("reactive-route");
|
|
|
31
31
|
var import_solid_js = require("solid-js");
|
|
32
32
|
var import_web3 = require("solid-js/web");
|
|
33
33
|
function Router(props) {
|
|
34
|
-
const config = props.
|
|
34
|
+
const config = props.router.adapters.makeObservable({
|
|
35
35
|
loadedComponentName: void 0,
|
|
36
36
|
loadedComponentPage: void 0,
|
|
37
37
|
currentProps: {}
|
|
38
38
|
});
|
|
39
|
+
let currentProps = {};
|
|
39
40
|
function redirectOnHistoryPop() {
|
|
40
41
|
if (!import_reactive_route.history) return;
|
|
41
42
|
import_reactive_route.history.listen((params) => {
|
|
42
43
|
if (params.action !== "POP") return;
|
|
43
|
-
const previousRoutePathname = props.
|
|
44
|
+
const previousRoutePathname = props.router.routesHistory[props.router.routesHistory.length - 2];
|
|
44
45
|
if (previousRoutePathname === params.location.pathname) {
|
|
45
|
-
props.
|
|
46
|
+
props.router.routesHistory.pop();
|
|
46
47
|
}
|
|
47
|
-
void props.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
routes: props.routes,
|
|
51
|
-
pathname: import_reactive_route.history.location.pathname
|
|
52
|
-
})
|
|
48
|
+
void props.router.restoreFromURL({
|
|
49
|
+
pathname: import_reactive_route.history.location.pathname,
|
|
50
|
+
noHistoryPush: true
|
|
53
51
|
});
|
|
54
52
|
});
|
|
55
53
|
}
|
|
56
54
|
function setLoadedComponent() {
|
|
57
|
-
const currentRouteName = props.
|
|
58
|
-
const currentRoutePage = props.
|
|
55
|
+
const currentRouteName = props.router.currentRoute.name;
|
|
56
|
+
const currentRoutePage = props.router.currentRoute.pageId;
|
|
59
57
|
const componentConfig = props.routes[currentRouteName];
|
|
60
58
|
let preventRedirect = false;
|
|
61
|
-
if (props.
|
|
59
|
+
if (props.router.isRedirecting) preventRedirect = true;
|
|
62
60
|
else if (config.loadedComponentName === currentRouteName) {
|
|
63
61
|
preventRedirect = true;
|
|
64
62
|
} else if (config.loadedComponentPage != null && currentRouteName != null) {
|
|
65
63
|
if (config.loadedComponentPage === currentRoutePage) {
|
|
66
|
-
props.
|
|
64
|
+
props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
65
|
+
currentProps = "props" in componentConfig ? componentConfig.props : {};
|
|
67
66
|
preventRedirect = true;
|
|
68
67
|
}
|
|
69
68
|
}
|
|
70
69
|
if (preventRedirect) return;
|
|
71
|
-
props.
|
|
70
|
+
props.router.adapters.batch(() => {
|
|
72
71
|
if (config.loadedComponentName) {
|
|
73
72
|
props.beforeUpdatePageComponent?.();
|
|
74
73
|
}
|
|
75
74
|
props.beforeSetPageComponent?.(componentConfig);
|
|
76
|
-
props.
|
|
77
|
-
props.
|
|
75
|
+
props.router.adapters.batch(() => {
|
|
76
|
+
props.router.adapters.replaceObject(config.currentProps, "props" in componentConfig ? componentConfig.props : {});
|
|
77
|
+
currentProps = "props" in componentConfig ? componentConfig.props : {};
|
|
78
78
|
config.loadedComponentName = currentRouteName;
|
|
79
|
-
config.loadedComponentPage = componentConfig.
|
|
79
|
+
config.loadedComponentPage = componentConfig.pageId;
|
|
80
80
|
});
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
83
|
props.beforeMount?.();
|
|
84
84
|
redirectOnHistoryPop();
|
|
85
|
-
if (props.
|
|
86
|
-
props.
|
|
85
|
+
if (props.router.adapters.immediateSetComponent) {
|
|
86
|
+
props.router.adapters.batch(() => {
|
|
87
87
|
setLoadedComponent();
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
-
props.
|
|
90
|
+
props.router.adapters.autorun(() => setLoadedComponent());
|
|
91
91
|
return (
|
|
92
92
|
// @ts-ignore
|
|
93
93
|
(0, import_web.createComponent)(import_solid_js.Show, {
|
|
@@ -99,7 +99,14 @@ function Router(props) {
|
|
|
99
99
|
get component() {
|
|
100
100
|
return props.routes[config.loadedComponentName]?.component || void 0;
|
|
101
101
|
}
|
|
102
|
-
}, () => config.currentProps))
|
|
102
|
+
}, () => Object.keys(config.currentProps).reduce((acc, key) => {
|
|
103
|
+
acc[key] = currentProps[key];
|
|
104
|
+
return acc;
|
|
105
|
+
}, {}), {
|
|
106
|
+
get router() {
|
|
107
|
+
return props.router;
|
|
108
|
+
}
|
|
109
|
+
}));
|
|
103
110
|
}
|
|
104
111
|
})
|
|
105
112
|
);
|