@koordinates/xstate-tree 4.4.0-beta.1 → 4.5.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 +36 -0
- package/lib/builders.js +57 -2
- package/lib/xstate-tree.d.ts +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -185,6 +185,42 @@ These events can be added anywhere, either next to a component for component spe
|
|
|
185
185
|
2. If they are tied to a component they need to be in the index.ts file that imports the view/selectors/actions etc and calls `createXStateTreeMachine`. If they are in the file containing those functions the index.d.ts file will not end up importing them.
|
|
186
186
|
|
|
187
187
|
|
|
188
|
+
### Utilities
|
|
189
|
+
|
|
190
|
+
#### `viewToMachine`
|
|
191
|
+
|
|
192
|
+
This utility accepts a React view that does not take any props and wraps it with an xstate-tree machine so you can easily invoke arbitrary React views in your xstate machines
|
|
193
|
+
|
|
194
|
+
```
|
|
195
|
+
function MyView() {
|
|
196
|
+
return <div>My View</div>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const MyViewMachine = viewToMachine(MyView);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
#### `buildRoutingMachine`
|
|
203
|
+
|
|
204
|
+
This utility aims to reduce boilerplate by generating a common type of state machine, a routing machine. This is a machine that solely consists of routing events that transition to states that invoke xstate-tree machines.
|
|
205
|
+
|
|
206
|
+
The first argument is the array of routes you wish to handle, and the second is an object mapping from those event types to the xstate-tree machine that will be invoked for that routing event
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
const routeA = createRoute.simpleRoute()({
|
|
210
|
+
url: "/a",
|
|
211
|
+
event: "GO_TO_A",
|
|
212
|
+
});
|
|
213
|
+
const routeB = createRoute.simpleRoute()({
|
|
214
|
+
url: "/b",
|
|
215
|
+
event: "GO_TO_B",
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const RoutingMachine = buildRoutingMachine([routeA, routeB], {
|
|
219
|
+
GO_TO_A: MachineA,
|
|
220
|
+
GO_TO_B: MachineB,
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
188
224
|
### Type helpers
|
|
189
225
|
|
|
190
226
|
There are some exported type helpers for use with xstate-tree
|
package/lib/builders.js
CHANGED
|
@@ -3,9 +3,10 @@ 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.viewToMachine = exports.createXStateTreeMachine = exports.buildXStateTreeMachine = exports.buildView = exports.buildActions = exports.buildSelectors = void 0;
|
|
6
|
+
exports.buildRoutingMachine = exports.viewToMachine = exports.createXStateTreeMachine = exports.buildXStateTreeMachine = exports.buildView = exports.buildActions = exports.buildSelectors = void 0;
|
|
7
7
|
const react_1 = __importDefault(require("react"));
|
|
8
8
|
const xstate_1 = require("xstate");
|
|
9
|
+
const slots_1 = require("./slots");
|
|
9
10
|
/**
|
|
10
11
|
* @public
|
|
11
12
|
*
|
|
@@ -158,8 +159,62 @@ exports.createXStateTreeMachine = createXStateTreeMachine;
|
|
|
158
159
|
* @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
|
|
159
160
|
*/
|
|
160
161
|
function viewToMachine(view) {
|
|
161
|
-
return createXStateTreeMachine((0, xstate_1.createMachine)({
|
|
162
|
+
return createXStateTreeMachine((0, xstate_1.createMachine)({
|
|
163
|
+
initial: "idle",
|
|
164
|
+
states: { idle: {} },
|
|
165
|
+
}), {
|
|
162
166
|
View: view,
|
|
163
167
|
});
|
|
164
168
|
}
|
|
165
169
|
exports.viewToMachine = viewToMachine;
|
|
170
|
+
/**
|
|
171
|
+
* @public
|
|
172
|
+
*
|
|
173
|
+
* Utility to aid in reducing boilerplate of mapping route events to xstate-tree machines
|
|
174
|
+
*
|
|
175
|
+
* Takes a list of routes and a mapping of route events to xstate-tree machines and returns an xstate-tree machine
|
|
176
|
+
* that renders the machines based on the routing events
|
|
177
|
+
*
|
|
178
|
+
* @param _routes - the array of routes you wish to map to machines
|
|
179
|
+
* @param mappings - an object mapping the route events to the machine to invoke
|
|
180
|
+
* @returns an xstate-tree machine that will render the right machines based on the routing events
|
|
181
|
+
*/
|
|
182
|
+
function buildRoutingMachine(_routes, mappings) {
|
|
183
|
+
const contentSlot = (0, slots_1.singleSlot)("Content");
|
|
184
|
+
const mappingsToStates = Object.entries(mappings).reduce((acc, [event, _machine]) => {
|
|
185
|
+
return {
|
|
186
|
+
...acc,
|
|
187
|
+
[event]: {
|
|
188
|
+
invoke: {
|
|
189
|
+
src: (_ctx, e) => {
|
|
190
|
+
return mappings[e.type];
|
|
191
|
+
},
|
|
192
|
+
id: contentSlot.getId(),
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}, {});
|
|
197
|
+
const mappingsToEvents = Object.keys(mappings).reduce((acc, event) => ({
|
|
198
|
+
...acc,
|
|
199
|
+
[event]: {
|
|
200
|
+
target: `.${event}`,
|
|
201
|
+
},
|
|
202
|
+
}), {});
|
|
203
|
+
const machine = (0, xstate_1.createMachine)({
|
|
204
|
+
on: {
|
|
205
|
+
...mappingsToEvents,
|
|
206
|
+
},
|
|
207
|
+
initial: "idle",
|
|
208
|
+
states: {
|
|
209
|
+
idle: {},
|
|
210
|
+
...mappingsToStates,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
return createXStateTreeMachine(machine, {
|
|
214
|
+
slots: [contentSlot],
|
|
215
|
+
View: ({ slots }) => {
|
|
216
|
+
return react_1.default.createElement(slots.Content, null);
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
exports.buildRoutingMachine = buildRoutingMachine;
|
package/lib/xstate-tree.d.ts
CHANGED
|
@@ -182,6 +182,20 @@ export declare function buildRootComponent(machine: AnyXstateTreeMachine, routin
|
|
|
182
182
|
rootMachine: AnyXstateTreeMachine;
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
+
/**
|
|
186
|
+
* @public
|
|
187
|
+
*
|
|
188
|
+
* Utility to aid in reducing boilerplate of mapping route events to xstate-tree machines
|
|
189
|
+
*
|
|
190
|
+
* Takes a list of routes and a mapping of route events to xstate-tree machines and returns an xstate-tree machine
|
|
191
|
+
* that renders the machines based on the routing events
|
|
192
|
+
*
|
|
193
|
+
* @param _routes - the array of routes you wish to map to machines
|
|
194
|
+
* @param mappings - an object mapping the route events to the machine to invoke
|
|
195
|
+
* @returns an xstate-tree machine that will render the right machines based on the routing events
|
|
196
|
+
*/
|
|
197
|
+
export declare function buildRoutingMachine<TRoutes extends AnyRoute[]>(_routes: TRoutes, mappings: Record<TRoutes[number]["event"], AnyXstateTreeMachine>): AnyXstateTreeMachine;
|
|
198
|
+
|
|
185
199
|
/**
|
|
186
200
|
* @public
|
|
187
201
|
*
|
package/package.json
CHANGED