@koordinates/xstate-tree 4.3.0 → 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,8 +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.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
+ const xstate_1 = require("xstate");
9
+ const slots_1 = require("./slots");
8
10
  /**
9
11
  * @public
10
12
  *
@@ -148,3 +150,71 @@ function createXStateTreeMachine(machine, options) {
148
150
  return machine;
149
151
  }
150
152
  exports.createXStateTreeMachine = createXStateTreeMachine;
153
+ /**
154
+ * @public
155
+ *
156
+ * Simple utility builder to aid in integrating existing React views with xstate-tree
157
+ *
158
+ * @param view - the React view you want to invoke in an xstate machine
159
+ * @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
160
+ */
161
+ function viewToMachine(view) {
162
+ return createXStateTreeMachine((0, xstate_1.createMachine)({
163
+ initial: "idle",
164
+ states: { idle: {} },
165
+ }), {
166
+ View: view,
167
+ });
168
+ }
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;
@@ -1,5 +1,5 @@
1
1
  import { AnyEventObject } from 'xstate';
2
- import type { AnyFunction } from 'xstate';
2
+ import { AnyFunction } from 'xstate';
3
3
  import { AnyStateMachine } from 'xstate';
4
4
  import { BaseActionObject } from 'xstate';
5
5
  import { ComponentPropsWithRef } from 'react';
@@ -7,7 +7,7 @@ import { ContextFrom } from 'xstate';
7
7
  import { EventFrom } from 'xstate';
8
8
  import { EventObject } from 'xstate';
9
9
  import { History as History_2 } from 'history';
10
- import type { InterpreterFrom } from 'xstate';
10
+ import { InterpreterFrom } from 'xstate';
11
11
  import { JSXElementConstructor } from 'react';
12
12
  import { ParsedQuery } from 'query-string';
13
13
  import { default as React_2 } from 'react';
@@ -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
  *
@@ -835,6 +849,16 @@ export declare type ViewProps<TSelectors, TActions, TSlots extends readonly Slot
835
849
  inState: TMatches;
836
850
  };
837
851
 
852
+ /**
853
+ * @public
854
+ *
855
+ * Simple utility builder to aid in integrating existing React views with xstate-tree
856
+ *
857
+ * @param view - the React view you want to invoke in an xstate machine
858
+ * @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
859
+ */
860
+ export declare function viewToMachine(view: () => JSX.Element): AnyXstateTreeMachine;
861
+
838
862
  /**
839
863
  * @public
840
864
  */
package/lib/xstateTree.js CHANGED
@@ -74,6 +74,7 @@ const getViewForInterpreter = (0, fast_memoize_1.default)((interpreter) => {
74
74
  (0, react_2.useEffect)(() => {
75
75
  if (activeRouteEvents) {
76
76
  activeRouteEvents.forEach((event) => {
77
+ // @ts-ignore fixed in v5 branch
77
78
  if (interpreter.state.nextEvents.includes(event.type)) {
78
79
  interpreter.send(event);
79
80
  }
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.3.0",
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": [