@xmachines/play-router 2.0.0 → 2.1.0
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 +93 -76
- package/dist/base-route-map.d.ts +63 -57
- package/dist/base-route-map.d.ts.map +1 -1
- package/dist/base-route-map.js +65 -59
- package/dist/base-route-map.js.map +1 -1
- package/dist/build-tree.d.ts +13 -12
- package/dist/build-tree.d.ts.map +1 -1
- package/dist/build-tree.js +30 -28
- package/dist/build-tree.js.map +1 -1
- package/dist/create-route-map-from-tree.d.ts +15 -15
- package/dist/create-route-map-from-tree.js +15 -15
- package/dist/create-route-map.d.ts +18 -16
- package/dist/create-route-map.d.ts.map +1 -1
- package/dist/create-route-map.js +10 -9
- package/dist/create-route-map.js.map +1 -1
- package/dist/errors.d.ts +40 -38
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +40 -38
- package/dist/errors.js.map +1 -1
- package/dist/extract-routes.d.ts +8 -7
- package/dist/extract-routes.d.ts.map +1 -1
- package/dist/extract-routes.js +31 -27
- package/dist/extract-routes.js.map +1 -1
- package/dist/find-route.d.ts +18 -15
- package/dist/find-route.d.ts.map +1 -1
- package/dist/find-route.js +42 -38
- package/dist/find-route.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -10
- package/dist/index.js.map +1 -1
- package/dist/machine-to-graph.d.ts +3 -2
- package/dist/machine-to-graph.d.ts.map +1 -1
- package/dist/machine-to-graph.js +20 -19
- package/dist/machine-to-graph.js.map +1 -1
- package/dist/query.d.ts +39 -37
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +62 -57
- package/dist/query.js.map +1 -1
- package/dist/router-bridge-base.d.ts +208 -190
- package/dist/router-bridge-base.d.ts.map +1 -1
- package/dist/router-bridge-base.js +235 -211
- package/dist/router-bridge-base.js.map +1 -1
- package/dist/router-sync.d.ts +41 -35
- package/dist/router-sync.d.ts.map +1 -1
- package/dist/router-sync.js +53 -45
- package/dist/router-sync.js.map +1 -1
- package/dist/types.d.ts +165 -147
- package/dist/types.d.ts.map +1 -1
- package/dist/url-pattern-utils.d.ts +53 -47
- package/dist/url-pattern-utils.d.ts.map +1 -1
- package/dist/url-pattern-utils.js +61 -55
- package/dist/url-pattern-utils.js.map +1 -1
- package/dist/validate-routes.d.ts +32 -31
- package/dist/validate-routes.d.ts.map +1 -1
- package/dist/validate-routes.js +30 -29
- package/dist/validate-routes.js.map +1 -1
- package/package.json +6 -5
package/dist/query.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import { getSuccessors, hasNode, hasPath } from "@statelyai/graph";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* transition
|
|
3
|
+
* Returns the successor nodes of a state that have a route: the states of a direct
|
|
4
|
+
* transition edge that hold a `meta.route` field.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* `getTransitionReachableStateIds` and `getTransitionReachableRoutes` share this
|
|
7
|
+
* walk. The function returns the complete nodes. Therefore each caller reads the
|
|
8
|
+
* field that it needs: the stateId, or the raw route string.
|
|
9
9
|
*/
|
|
10
10
|
const getRoutableSuccessors = (graph, stateId) =>
|
|
11
|
-
// A state ID
|
|
12
|
-
// normal "nothing
|
|
13
|
-
//
|
|
11
|
+
// A state ID without a graph node, for example a state without a route or a state
|
|
12
|
+
// that a person removed, is a normal result of "nothing to reach". The code tests
|
|
13
|
+
// that case explicitly, and it matches no message of an error from a throw.
|
|
14
14
|
hasNode(graph, stateId)
|
|
15
15
|
? getSuccessors(graph, stateId).filter((n) => n.data.route !== undefined)
|
|
16
16
|
: [];
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* Returns every route of a navigation from the given state
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* The function returns the child routes of the state. A later version also returns
|
|
21
|
+
* the sibling routes of a transition.
|
|
22
22
|
*
|
|
23
|
-
* @param tree -
|
|
24
|
-
* @param stateId -
|
|
25
|
-
* @returns
|
|
23
|
+
* @param tree - The route tree, from extractMachineRoutes()
|
|
24
|
+
* @param stateId - The ID of the current state
|
|
25
|
+
* @returns The array of the route nodes that the state can reach
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
28
|
* ```typescript
|
|
@@ -35,12 +35,13 @@ export const getNavigableRoutes = (tree, stateId) => {
|
|
|
35
35
|
const node = tree.byStateId.get(stateId);
|
|
36
36
|
if (!node)
|
|
37
37
|
return [];
|
|
38
|
-
const results = [...node.children]; //
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
38
|
+
const results = [...node.children]; // The direct child routes
|
|
39
|
+
// With a graph, the result also holds each route that a transition can reach.
|
|
40
|
+
// The code looks each node up by its state ID: a graph node carries the RAW
|
|
41
|
+
// meta.route string, and that string can be relative, for example "detail". The
|
|
42
|
+
// keys of tree.byPath are the resolved fullPath values, for example
|
|
43
|
+
// "/section/detail". A lookup by the path therefore loses every relative route in
|
|
44
|
+
// silence. A state ID is unambiguous in both structures.
|
|
44
45
|
if (tree.graph) {
|
|
45
46
|
const reachableStateIds = getTransitionReachableStateIds(tree.graph, stateId);
|
|
46
47
|
const existingPaths = new Set(results.map((r) => r.fullPath));
|
|
@@ -55,24 +56,26 @@ export const getNavigableRoutes = (tree, stateId) => {
|
|
|
55
56
|
return results;
|
|
56
57
|
};
|
|
57
58
|
/**
|
|
58
|
-
*
|
|
59
|
+
* Returns the state IDs of the states with a route that a transition from a state
|
|
60
|
+
* can reach.
|
|
59
61
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* `tree.byStateId
|
|
62
|
+
* The walk is the same as the walk of `getTransitionReachableRoutes`, but this
|
|
63
|
+
* function returns the state IDs, and not the raw `meta.route` strings. A caller
|
|
64
|
+
* therefore resolves each node through `tree.byStateId`, with no doubt between a raw
|
|
65
|
+
* path and a resolved path.
|
|
63
66
|
*/
|
|
64
67
|
const getTransitionReachableStateIds = (graph, stateId) => {
|
|
65
68
|
return getRoutableSuccessors(graph, stateId).map((n) => n.data.stateId);
|
|
66
69
|
};
|
|
67
70
|
/**
|
|
68
|
-
*
|
|
71
|
+
* Returns every route of the tree that has a route, in one flat array
|
|
69
72
|
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
+
* The function returns each route with a `meta.route` field. It returns no state
|
|
74
|
+
* without a route, and no synthetic root node. Use it to generate a router
|
|
75
|
+
* configuration in a framework adapter dynamically.
|
|
73
76
|
*
|
|
74
|
-
* @param tree -
|
|
75
|
-
* @returns
|
|
77
|
+
* @param tree - The route tree, from extractMachineRoutes()
|
|
78
|
+
* @returns The array of the route nodes with a route, with their path and their stateId
|
|
76
79
|
*
|
|
77
80
|
* @example
|
|
78
81
|
* ```typescript
|
|
@@ -88,7 +91,7 @@ const getTransitionReachableStateIds = (graph, stateId) => {
|
|
|
88
91
|
export const getRoutableRoutes = (tree) => {
|
|
89
92
|
const routes = [];
|
|
90
93
|
for (const node of tree.byStateId.values()) {
|
|
91
|
-
//
|
|
94
|
+
// Keep the nodes with a route, which means the nodes with a meta.route field, and skip the synthetic root
|
|
92
95
|
if (node.routable && node.id !== "__root__") {
|
|
93
96
|
routes.push(node);
|
|
94
97
|
}
|
|
@@ -96,39 +99,40 @@ export const getRoutableRoutes = (tree) => {
|
|
|
96
99
|
return routes;
|
|
97
100
|
};
|
|
98
101
|
/**
|
|
99
|
-
*
|
|
102
|
+
* Tells you if a route path is in the tree
|
|
100
103
|
*
|
|
101
|
-
*
|
|
104
|
+
* The function tests if the path has a state with a `meta.route` field.
|
|
102
105
|
*
|
|
103
|
-
* @param tree -
|
|
104
|
-
* @param path -
|
|
105
|
-
* @returns true
|
|
106
|
+
* @param tree - The route tree, from extractMachineRoutes()
|
|
107
|
+
* @param path - The complete route path, for example '/dashboard/settings'
|
|
108
|
+
* @returns true when the tree holds the path. In every other case, false
|
|
106
109
|
*/
|
|
107
110
|
export const routeExists = (tree, path) => {
|
|
108
111
|
return tree.byPath.has(path);
|
|
109
112
|
};
|
|
110
113
|
/**
|
|
111
|
-
*
|
|
114
|
+
* Returns the routes that a transition from the current state can reach
|
|
112
115
|
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
+
* The function uses the successor algorithm of @statelyai/graph. It finds every
|
|
117
|
+
* state of a direct transition edge from the given state, then it keeps the states
|
|
118
|
+
* with a route.
|
|
116
119
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* `tree.byPath
|
|
120
|
-
* resolves
|
|
120
|
+
* The values of the return are the RAW `meta.route` strings of the machine. The
|
|
121
|
+
* function does NOT resolve a relative route, for example `"detail"`, to a complete
|
|
122
|
+
* path, and such a value is therefore no key of `tree.byPath`. For a resolved
|
|
123
|
+
* `RouteNode`, use `getNavigableRoutes`: that function resolves each state that the
|
|
124
|
+
* transition reaches through `tree.byStateId`.
|
|
121
125
|
*
|
|
122
|
-
* @param graph -
|
|
123
|
-
* @param stateId -
|
|
124
|
-
* @returns
|
|
126
|
+
* @param graph - The machine graph, from RouteTree.graph
|
|
127
|
+
* @param stateId - The ID of the current state, for example "test.home"
|
|
128
|
+
* @returns The array of the raw route strings that a transition can reach
|
|
125
129
|
*
|
|
126
130
|
* @example
|
|
127
131
|
* ```typescript
|
|
128
132
|
* const tree = extractMachineRoutes(machine);
|
|
129
133
|
* if (tree.graph) {
|
|
130
134
|
* const reachable = getTransitionReachableRoutes(tree.graph, 'auth.loggedIn');
|
|
131
|
-
* // ['/dashboard', '/settings'] — routes
|
|
135
|
+
* // ['/dashboard', '/settings'] — the routes that a transition can reach
|
|
132
136
|
* }
|
|
133
137
|
* ```
|
|
134
138
|
*/
|
|
@@ -136,27 +140,28 @@ export const getTransitionReachableRoutes = (graph, stateId) => {
|
|
|
136
140
|
return getRoutableSuccessors(graph, stateId).map((n) => n.data.route);
|
|
137
141
|
};
|
|
138
142
|
/**
|
|
139
|
-
*
|
|
143
|
+
* Tells you if a transition from the current state can reach a route
|
|
140
144
|
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
145
|
+
* The function uses the path search of @statelyai/graph. It decides if a chain of
|
|
146
|
+
* transition edges is present from the state of the origin to the state of the
|
|
147
|
+
* target.
|
|
143
148
|
*
|
|
144
|
-
* @param graph -
|
|
145
|
-
* @param fromStateId -
|
|
146
|
-
* @param toStateId -
|
|
147
|
-
* @returns true
|
|
149
|
+
* @param graph - The machine graph, from RouteTree.graph
|
|
150
|
+
* @param fromStateId - The ID of the state of the origin
|
|
151
|
+
* @param toStateId - The ID of the state of the target
|
|
152
|
+
* @returns true when a transition path is present. In every other case, false
|
|
148
153
|
*
|
|
149
154
|
* @example
|
|
150
155
|
* ```typescript
|
|
151
156
|
* const tree = extractMachineRoutes(machine);
|
|
152
157
|
* if (tree.graph) {
|
|
153
158
|
* const canReach = isRouteReachable(tree.graph, 'auth.login', 'auth.dashboard');
|
|
154
|
-
* // true
|
|
159
|
+
* // it is true when a transition path from login to dashboard is present
|
|
155
160
|
* }
|
|
156
161
|
* ```
|
|
157
162
|
*/
|
|
158
163
|
export const isRouteReachable = (graph, fromStateId, toStateId) => {
|
|
159
|
-
//
|
|
164
|
+
// An absent endpoint is a normal result of "not reachable".
|
|
160
165
|
return hasNode(graph, fromStateId) && hasNode(graph, toStateId)
|
|
161
166
|
? hasPath(graph, fromStateId, toStateId)
|
|
162
167
|
: false;
|
package/dist/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGnE;;;;;;;GAOG;AACH,MAAM,qBAAqB,GAAG,CAC7B,KAA8C,EAC9C,OAAe,EACgB,EAAE;AACjC,
|
|
1
|
+
{"version":3,"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