@koordinates/xstate-tree 5.1.0-next.14 → 5.1.0-next.16
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/lib/builders.js +10 -2
- package/lib/routing/index.js +2 -1
- package/lib/routing/providers.js +13 -2
- package/lib/xstate-tree.d.ts +1 -1
- package/lib/xstateTree.js +5 -3
- package/package.json +1 -1
package/lib/builders.js
CHANGED
|
@@ -74,11 +74,19 @@ exports.viewToMachine = viewToMachine;
|
|
|
74
74
|
* @returns an xstate-tree machine that will render the right machines based on the routing events
|
|
75
75
|
*/
|
|
76
76
|
function buildRoutingMachine(_routes, mappings) {
|
|
77
|
+
/**
|
|
78
|
+
* States in xstate can't contain dots, since the states are named after the routing events
|
|
79
|
+
* if the routing event contains a dot that will make a state with a dot in it
|
|
80
|
+
* this function sanitizes the event name to remove dots and is used for the state names and targets
|
|
81
|
+
*/
|
|
82
|
+
function sanitizeEventName(event) {
|
|
83
|
+
return event.replace(/\.([a-zA-Z])/g, (_, letter) => letter.toUpperCase());
|
|
84
|
+
}
|
|
77
85
|
const contentSlot = (0, slots_1.singleSlot)("Content");
|
|
78
86
|
const mappingsToStates = Object.entries(mappings).reduce((acc, [event, _machine]) => {
|
|
79
87
|
return {
|
|
80
88
|
...acc,
|
|
81
|
-
[event]: {
|
|
89
|
+
[sanitizeEventName(event)]: {
|
|
82
90
|
invoke: {
|
|
83
91
|
src: event,
|
|
84
92
|
id: contentSlot.getId(),
|
|
@@ -89,7 +97,7 @@ function buildRoutingMachine(_routes, mappings) {
|
|
|
89
97
|
const mappingsToEvents = Object.keys(mappings).reduce((acc, event) => ({
|
|
90
98
|
...acc,
|
|
91
99
|
[event]: {
|
|
92
|
-
target: `.${event}`,
|
|
100
|
+
target: `.${sanitizeEventName(event)}`,
|
|
93
101
|
},
|
|
94
102
|
}), {});
|
|
95
103
|
const machine = (0, xstate_1.setup)({
|
package/lib/routing/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useActiveRouteEvents = exports.useInRoutingContext = exports.TestRoutingContext = exports.RoutingContext = exports.useRouteArgsIfActive = exports.useIsRouteActive = exports.handleLocationChange = exports.matchRoute = exports.Link = exports.joinRoutes = exports.buildCreateRoute = void 0;
|
|
3
|
+
exports.useActiveRouteEvents = exports.useInTestRoutingContext = exports.useInRoutingContext = exports.TestRoutingContext = exports.RoutingContext = exports.useRouteArgsIfActive = exports.useIsRouteActive = exports.handleLocationChange = exports.matchRoute = exports.Link = exports.joinRoutes = exports.buildCreateRoute = void 0;
|
|
4
4
|
var createRoute_1 = require("./createRoute");
|
|
5
5
|
Object.defineProperty(exports, "buildCreateRoute", { enumerable: true, get: function () { return createRoute_1.buildCreateRoute; } });
|
|
6
6
|
var joinRoutes_1 = require("./joinRoutes");
|
|
@@ -19,4 +19,5 @@ var providers_1 = require("./providers");
|
|
|
19
19
|
Object.defineProperty(exports, "RoutingContext", { enumerable: true, get: function () { return providers_1.RoutingContext; } });
|
|
20
20
|
Object.defineProperty(exports, "TestRoutingContext", { enumerable: true, get: function () { return providers_1.TestRoutingContext; } });
|
|
21
21
|
Object.defineProperty(exports, "useInRoutingContext", { enumerable: true, get: function () { return providers_1.useInRoutingContext; } });
|
|
22
|
+
Object.defineProperty(exports, "useInTestRoutingContext", { enumerable: true, get: function () { return providers_1.useInTestRoutingContext; } });
|
|
22
23
|
Object.defineProperty(exports, "useActiveRouteEvents", { enumerable: true, get: function () { return providers_1.useActiveRouteEvents; } });
|
package/lib/routing/providers.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.TestRoutingContext = exports.useActiveRouteEvents = exports.useInRoutingContext = exports.RoutingContext = void 0;
|
|
6
|
+
exports.TestRoutingContext = exports.useActiveRouteEvents = exports.useInTestRoutingContext = exports.useInRoutingContext = exports.RoutingContext = void 0;
|
|
7
7
|
const react_1 = __importDefault(require("react"));
|
|
8
8
|
const react_2 = require("react");
|
|
9
9
|
exports.RoutingContext = (0, react_2.createContext)(undefined);
|
|
@@ -22,6 +22,14 @@ function useInRoutingContext() {
|
|
|
22
22
|
return context !== undefined;
|
|
23
23
|
}
|
|
24
24
|
exports.useInRoutingContext = useInRoutingContext;
|
|
25
|
+
/**
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
function useInTestRoutingContext() {
|
|
29
|
+
const context = (0, react_2.useContext)(exports.RoutingContext);
|
|
30
|
+
return context?.isTestRoutingContext ?? false;
|
|
31
|
+
}
|
|
32
|
+
exports.useInTestRoutingContext = useInTestRoutingContext;
|
|
25
33
|
/**
|
|
26
34
|
* @public
|
|
27
35
|
*
|
|
@@ -46,6 +54,9 @@ exports.useActiveRouteEvents = useActiveRouteEvents;
|
|
|
46
54
|
* @param activeRouteEvents - The active route events to use in the context
|
|
47
55
|
*/
|
|
48
56
|
function TestRoutingContext({ activeRouteEvents, children, }) {
|
|
49
|
-
return (react_1.default.createElement(exports.RoutingContext.Provider, { value: {
|
|
57
|
+
return (react_1.default.createElement(exports.RoutingContext.Provider, { value: {
|
|
58
|
+
activeRouteEvents: { current: activeRouteEvents },
|
|
59
|
+
isTestRoutingContext: true,
|
|
60
|
+
} }, children));
|
|
50
61
|
}
|
|
51
62
|
exports.TestRoutingContext = TestRoutingContext;
|
package/lib/xstate-tree.d.ts
CHANGED
|
@@ -712,7 +712,7 @@ export declare type View<TActionsOutput, TSelectorsOutput, TSlots extends readon
|
|
|
712
712
|
* @param view - the React view you want to invoke in an xstate machine
|
|
713
713
|
* @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
|
|
714
714
|
*/
|
|
715
|
-
export declare function viewToMachine(view: () => JSX.Element): AnyXstateTreeMachine;
|
|
715
|
+
export declare function viewToMachine(view: (args?: any) => JSX.Element): AnyXstateTreeMachine;
|
|
716
716
|
|
|
717
717
|
declare type WithParentPath<TCurrent extends string, TParentPath extends string> = `${TParentPath extends "" ? "" : `${TParentPath}.`}${TCurrent}`;
|
|
718
718
|
|
package/lib/xstateTree.js
CHANGED
|
@@ -32,7 +32,6 @@ const fast_memoize_1 = __importDefault(require("fast-memoize"));
|
|
|
32
32
|
const react_2 = __importStar(require("react"));
|
|
33
33
|
const tiny_emitter_1 = require("tiny-emitter");
|
|
34
34
|
const routing_1 = require("./routing");
|
|
35
|
-
const providers_1 = require("./routing/providers");
|
|
36
35
|
const useConstant_1 = require("./useConstant");
|
|
37
36
|
const useService_1 = require("./useService");
|
|
38
37
|
const utils_1 = require("./utils");
|
|
@@ -70,7 +69,7 @@ interpreter) {
|
|
|
70
69
|
}
|
|
71
70
|
const getViewForInterpreter = (0, fast_memoize_1.default)((interpreter) => {
|
|
72
71
|
return react_2.default.memo(function InterpreterView({ children, }) {
|
|
73
|
-
const activeRouteEvents = (0,
|
|
72
|
+
const activeRouteEvents = (0, routing_1.useActiveRouteEvents)();
|
|
74
73
|
(0, react_2.useEffect)(() => {
|
|
75
74
|
if (activeRouteEvents) {
|
|
76
75
|
activeRouteEvents.forEach((event) => {
|
|
@@ -247,7 +246,10 @@ function buildRootComponent(options) {
|
|
|
247
246
|
activeRouteEventsRef.current = events;
|
|
248
247
|
};
|
|
249
248
|
const insideRoutingContext = (0, routing_1.useInRoutingContext)();
|
|
250
|
-
|
|
249
|
+
const inTestRoutingContext = (0, routing_1.useInTestRoutingContext)();
|
|
250
|
+
if (!inTestRoutingContext &&
|
|
251
|
+
insideRoutingContext &&
|
|
252
|
+
typeof routing !== "undefined") {
|
|
251
253
|
const m = "Routing root rendered inside routing context, this implies a bug";
|
|
252
254
|
if (process.env.NODE_ENV !== "production") {
|
|
253
255
|
throw new Error(m);
|
package/package.json
CHANGED