@koordinates/xstate-tree 4.4.0-beta.1 → 4.4.0-beta.2

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 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)({ initial: "idle", states: { idle: {} } }), {
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;
@@ -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
@@ -2,7 +2,7 @@
2
2
  "name": "@koordinates/xstate-tree",
3
3
  "main": "lib/index.js",
4
4
  "types": "lib/xstate-tree.d.ts",
5
- "version": "4.4.0-beta.1",
5
+ "version": "4.4.0-beta.2",
6
6
  "license": "MIT",
7
7
  "description": "Build UIs with Actors using xstate and React",
8
8
  "keywords": [