@koordinates/xstate-tree 2.0.5 → 2.0.7
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 +106 -6
- package/lib/builders.js +47 -1
- package/lib/lazy.js +1 -0
- package/lib/routing/Link.js +1 -0
- package/lib/routing/createRoute/createRoute.js +5 -0
- package/lib/testingUtilities.js +13 -0
- package/lib/xstate-tree.d.ts +99 -1
- package/lib/xstateTree.js +13 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,13 +1,112 @@
|
|
|
1
1
|
# xstate-tree
|
|
2
2
|
|
|
3
|
-
xstate-tree
|
|
3
|
+
xstate-tree was born as an answer to the question "What would a UI framework that uses [Actors](https://en.wikipedia.org/wiki/Actor_model) as the building block look like?". Inspired by Thomas Weber's [Master Thesis](https://links-lang.org/papers/mscs/Master_Thesis_Thomas_Weber_1450761.pdf) on the topic. [XState](https://xstate.js.org/) was chosen to power the actors with [React](https://reactjs.org) powering the UI derived from the actors.
|
|
4
|
+
|
|
5
|
+
xstate-tree was designed to enable modeling large applications as a single tree of xstate machines, with each machine being responsible for smaller and smaller sub sections of the UI. This allows modeling the entire UI structure in state machines, but without having to worry about the complexity of managing the state of the entire application in a single large machine.
|
|
6
|
+
|
|
7
|
+
Each machine has an associated, but loosely coupled, React view associated with it. The loose coupling allows the view to have no knowledge of the state machine for ease of testing and re-use in tools like [Storybook](https://storybook.js.org). Actors views are composed together via "slots", which can be rendered in the view to provide child actors a place to render their views in the parent's view.
|
|
8
|
+
|
|
9
|
+
While xstate-tree manages your application state, it does not have a mechanism for providing global application state accessible by multiple actors, this must be provided with another library like [Redux](https://redux.js.org/) or [GraphQL](https://graphql.org/). It does provide a routing solution, outlined [here](https://github.com/koordinates/xstate-tree/blob/master/src/routing/README.md).
|
|
10
|
+
|
|
11
|
+
At Koordinates we use xstate-tree for all new UI development. Our desktop application, built on top of [Kart](https://kartproject.org/) our Geospatial version control system, is built entirely with xstate-tree using GraphQL for global state.
|
|
12
|
+
|
|
13
|
+
A minimal example of a single machine tree
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import React from "react";
|
|
17
|
+
import { createRoot } from "react-dom/client";
|
|
18
|
+
import { createMachine } from "xstate";
|
|
19
|
+
import { assign } from "@xstate/immer";
|
|
20
|
+
import { buildSelectors, buildActions, buildView, buildXstateTreeMachine, buildRootComponent } from "@koordinates/xstate-tree";
|
|
21
|
+
|
|
22
|
+
type Events = { type: "SWITCH_CLICKED" } | { type: "INCREMENT", amount: number };
|
|
23
|
+
type Context = { incremented: number };
|
|
24
|
+
|
|
25
|
+
// If this tree had more than a single machine the slots to render child machines into would be defined here
|
|
26
|
+
const slots = [];
|
|
27
|
+
|
|
28
|
+
// A standard xstate machine, nothing extra is needed for xstate-tree
|
|
29
|
+
const machine = createMachine<Context, Events>({
|
|
30
|
+
id: "root",
|
|
31
|
+
initial: "inactive",
|
|
32
|
+
context: {
|
|
33
|
+
incremented: 0,
|
|
34
|
+
},
|
|
35
|
+
states: {
|
|
36
|
+
inactive: {
|
|
37
|
+
on: {
|
|
38
|
+
SWITCH_CLICKED: "active",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
active: {
|
|
42
|
+
on: {
|
|
43
|
+
SWITCH_CLICKED: "idle",
|
|
44
|
+
INCREMENT: { actions: "increment" },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
}, {
|
|
49
|
+
actions: {
|
|
50
|
+
increment: assign((context, event) => {
|
|
51
|
+
context.incremented += event.amount;
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Selectors to transform the machines state into a representation useful for the view
|
|
57
|
+
const selectors = buildSelectors(machine, (ctx, canHandleEvent) => ({
|
|
58
|
+
canIncrement: canHandleEvent({type: "INCREMENT", count: 1 }),
|
|
59
|
+
showSecret: ctx.incremented > 10,
|
|
60
|
+
count: ctx.incremented,
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
// Actions to abstract away the details of sending events to the machine
|
|
64
|
+
const actions = buildActions(machine, actions, (send, selectors) => ({
|
|
65
|
+
increment(amount: number) {
|
|
66
|
+
send({ type: "INCREMENT", amount: selectors.count > 4 ? amount * 2 : amount });
|
|
67
|
+
},
|
|
68
|
+
switch() {
|
|
69
|
+
send({ type: "SWITCH_CLICKED" });
|
|
70
|
+
},
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
// A view to bring it all together
|
|
74
|
+
// the return value is a plain React view that can be rendered anywhere by passing in the needed props
|
|
75
|
+
// the view has no knowledge of the machine it's bound to
|
|
76
|
+
const view = buildView(machine, actions, selectors, slots, ({ actions, selectors, inState }) => {
|
|
77
|
+
return (
|
|
78
|
+
<div>
|
|
79
|
+
<button onClick={() => actions.switch()}>{inState("active") ? "Deactivate" : "Activate"}</button>
|
|
80
|
+
<p>Count: {selectors.count}</p>
|
|
81
|
+
<button onClick={() => actions.increment(1)} disabled={!selectors.canIncrement}>Increment</button>
|
|
82
|
+
{selectors.showSecret && <p>The secret password is hunter2</p>}
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Stapling the machine, selectors, actions, view, and slots together
|
|
88
|
+
const RootMachine = buildXstateTreeMachine(machine, {
|
|
89
|
+
selectors,
|
|
90
|
+
actions,
|
|
91
|
+
view,
|
|
92
|
+
slots
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Build the React host for the tree
|
|
96
|
+
const XstateTreeRoot = buildRootComponent(RootMachine);
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
// Rendering it with React
|
|
100
|
+
const ReactRoot = createRoot(document.getElementById("root"));
|
|
101
|
+
ReactRoot.render(<XstateTreeRoot />);
|
|
102
|
+
```
|
|
4
103
|
|
|
5
104
|
## Overview
|
|
6
105
|
|
|
7
106
|
Each machine that forms the tree representing your UI has an associated set of selector, action, view functions, and "slots"
|
|
8
107
|
- Selector functions are provided with the current context of the machine, a function to determine if it can handle a given event and a function to determine if it is in a given state, and expose the returned result to the view.
|
|
9
108
|
- Action functions are provided with the `send` method bound to the machines interpreter and the result of calling the selector function
|
|
10
|
-
- Slots are how children of the machine are exposed to the view. They can be either single slot
|
|
109
|
+
- Slots are how children of the machine are exposed to the view. They can be either single slot for a single actor, or multi slot for when you have a list of actors.
|
|
11
110
|
- View functions are React views provided with the output of the selector and action functions, a function to determine if the machine is in a given state, and the currently active slots
|
|
12
111
|
|
|
13
112
|
## API
|
|
@@ -19,6 +118,8 @@ To assist in making xstate-tree easy to use with TypeScript there are "builder"
|
|
|
19
118
|
* `buildView`, first argument is the machine we are creating a view for, second argument is the selector factory, third argument is the actions factory, fourth argument is the array of slots and the fifth argument is the view function itself which gets supplied the selectors, actions, slots and `inState` method as props. It wraps the view in a React.memo
|
|
20
119
|
* `buildXStateTreeMachine` takes the results of `buildSelectors`, `buildActions`, `buildView` and the list of slots and returns an xstate-tree compatible machine
|
|
21
120
|
|
|
121
|
+
Full API docs coming soon, see [#20](https://github.com/koordinates/xstate-tree/issues/20)
|
|
122
|
+
|
|
22
123
|
### Slots
|
|
23
124
|
|
|
24
125
|
Slots are how invoked/spawned children of the machine are supplied to the Machines view. The child machines get wrapped into a React component responsible for rendering the machine itself. Since the view is provided with these components it is responsible for determining where in the view output they show up. This leaves the view in complete control of where the child views are placed.
|
|
@@ -58,10 +159,9 @@ These events can be added anywhere, either next to a component for component spe
|
|
|
58
159
|
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 `buildXstateTreeMachine`. If they are in the file containing those functions the index.d.ts file will not end up importing them.
|
|
59
160
|
|
|
60
161
|
|
|
61
|
-
### Storybook
|
|
162
|
+
### [Storybook](https://storybook.js.org)
|
|
62
163
|
|
|
63
|
-
It
|
|
64
|
-
that accept selectors/actions/slots/inState as props you can just import the view and render it in a Story
|
|
164
|
+
It is relatively simple to display xstate-tree views directly in Storybook. Since the views are plain React components that accept selectors/actions/slots/inState as props you can just import the view and render it in a Story
|
|
65
165
|
|
|
66
166
|
There are a few utilities in xstate-tree to make this easier
|
|
67
167
|
|
|
@@ -87,4 +187,4 @@ are planning on rendering the view using the xstate-tree machine itself, or test
|
|
|
87
187
|
via the view.
|
|
88
188
|
|
|
89
189
|
It's a simple function that takes a name argument and returns a basic xstate-tree machine that you
|
|
90
|
-
can replace slot services with. It just renders a div containing the name supplied
|
|
190
|
+
can replace slot services with. It just renders a div containing the name supplied
|
package/lib/builders.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
/**
|
|
3
3
|
* @public
|
|
4
|
+
*
|
|
5
|
+
* Factory function for selectors. The selectors function is passed three arguments:
|
|
6
|
+
* - `ctx` - the current context of the machines state
|
|
7
|
+
* - `canHandleEvent` - a function that can be used to determine if the machine can handle a
|
|
8
|
+
* given event, by simulating sending the event and seeing if a stat change would happen.
|
|
9
|
+
* Handles guards
|
|
10
|
+
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
11
|
+
*
|
|
12
|
+
* The resulting selector function has memoization. It will return the same value until the
|
|
13
|
+
* machine's state changes or the machine's context changes
|
|
14
|
+
* @param machine - The machine to create the selectors for
|
|
15
|
+
* @param selectors - The selector function
|
|
16
|
+
* @returns The selectors - ready to be passed to {@link buildActions}
|
|
4
17
|
*/
|
|
5
18
|
export function buildSelectors(__machine, selectors) {
|
|
6
19
|
let lastState = undefined;
|
|
@@ -27,7 +40,19 @@ export function buildSelectors(__machine, selectors) {
|
|
|
27
40
|
}
|
|
28
41
|
/**
|
|
29
42
|
* @public
|
|
30
|
-
|
|
43
|
+
*
|
|
44
|
+
* Factory function for actions. The actions function is passed two arguments:
|
|
45
|
+
* - `send` - the interpreters send function, which can be used to send events to the machine
|
|
46
|
+
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
47
|
+
*
|
|
48
|
+
* The resulting action function has memoization. It will return the same value until the
|
|
49
|
+
* selectors reference changes or the send reference changes
|
|
50
|
+
*
|
|
51
|
+
* @param machine - The machine to create the actions for
|
|
52
|
+
* @param selectors - The selectors function
|
|
53
|
+
* @param actions - The action function
|
|
54
|
+
* @returns The actions function - ready to be passed to {@link buildView}
|
|
55
|
+
* */
|
|
31
56
|
export function buildActions(__machine, __selectors, actions) {
|
|
32
57
|
let lastSelectorResult = undefined;
|
|
33
58
|
let lastCachedResult = undefined;
|
|
@@ -46,12 +71,33 @@ export function buildActions(__machine, __selectors, actions) {
|
|
|
46
71
|
}
|
|
47
72
|
/**
|
|
48
73
|
* @public
|
|
74
|
+
*
|
|
75
|
+
* Factory function for views. The view is passed four props:
|
|
76
|
+
* - `slots` - the slots object, which can be used to render the children of the view invoked by the machine
|
|
77
|
+
* - `actions` - the output of the actions function from {@link buildActions}
|
|
78
|
+
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
79
|
+
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
80
|
+
*
|
|
81
|
+
* The resulting view is wrapped in React.memo, it will re-render when the actions or selectors reference changes
|
|
82
|
+
*
|
|
83
|
+
* @param machine - The machine to create the view for
|
|
84
|
+
* @param selectors - The selectors function from {@link buildSelectors}
|
|
85
|
+
* @param actions - The actions function from {@link buildActions}
|
|
86
|
+
* @param slots - The array of slots that can be rendered by the view
|
|
87
|
+
* @param view - The view function
|
|
88
|
+
* @returns The React view
|
|
49
89
|
*/
|
|
50
90
|
export function buildView(__machine, __selectors, __actions, __slots, view) {
|
|
51
91
|
return React.memo(view);
|
|
52
92
|
}
|
|
53
93
|
/**
|
|
54
94
|
* @public
|
|
95
|
+
*
|
|
96
|
+
* staples xstate machine and xstate-tree metadata together into an xstate-tree machine
|
|
97
|
+
*
|
|
98
|
+
* @param machine - The machine to staple the selectors/actions/slots/view to
|
|
99
|
+
* @param metadata - The xstate-tree metadata to staple to the machine
|
|
100
|
+
* @returns The xstate-tree machine, ready to be invoked by other xstate-machines or used with `buildRootComponent`
|
|
55
101
|
*/
|
|
56
102
|
export function buildXStateTreeMachine(machine, meta) {
|
|
57
103
|
const copiedMeta = { ...meta };
|
package/lib/lazy.js
CHANGED
|
@@ -4,6 +4,7 @@ import { buildActions, buildSelectors, buildView, buildXStateTreeMachine, } from
|
|
|
4
4
|
import { singleSlot } from "./slots";
|
|
5
5
|
/**
|
|
6
6
|
* @public
|
|
7
|
+
*
|
|
7
8
|
* Wraps an xstate-tree returning Promise (generated by `import()` in an xstate-tree machine responsible for
|
|
8
9
|
* booting up the machine upon resolution
|
|
9
10
|
*
|
package/lib/routing/Link.js
CHANGED
|
@@ -4,6 +4,11 @@ import { isNil } from "../../utils";
|
|
|
4
4
|
import { joinRoutes } from "../joinRoutes";
|
|
5
5
|
/**
|
|
6
6
|
* @public
|
|
7
|
+
*
|
|
8
|
+
* Creates a route factory
|
|
9
|
+
*
|
|
10
|
+
* @param history - the history object to use for this route factory, this needs to be the same one used in the trees root component
|
|
11
|
+
* @param basePath - the base path for this route factory
|
|
7
12
|
*/
|
|
8
13
|
export function buildCreateRoute(history, basePath) {
|
|
9
14
|
function navigate({ history, url, meta, }) {
|
package/lib/testingUtilities.js
CHANGED
|
@@ -8,6 +8,11 @@ import { difference } from "./utils";
|
|
|
8
8
|
import { emitter, recursivelySend, XstateTreeView } from "./xstateTree";
|
|
9
9
|
/**
|
|
10
10
|
* @public
|
|
11
|
+
*
|
|
12
|
+
* Creates a dummy machine that just renders the supplied string - useful for rendering xstate-tree views in isolation
|
|
13
|
+
*
|
|
14
|
+
* @param name - the string to render in the machines view
|
|
15
|
+
* @returns a dummy machine that renders a div containing the supplied string
|
|
11
16
|
*/
|
|
12
17
|
export function slotTestingDummyFactory(name) {
|
|
13
18
|
return buildXStateTreeMachine(createMachine({
|
|
@@ -26,6 +31,8 @@ export function slotTestingDummyFactory(name) {
|
|
|
26
31
|
}
|
|
27
32
|
/**
|
|
28
33
|
* @public
|
|
34
|
+
*
|
|
35
|
+
* Can be used as the slots prop for an xstate-tree view, will render a div containing a <p>slotName-slot<p> for each slot
|
|
29
36
|
*/
|
|
30
37
|
export const genericSlotsTestingDummy = new Proxy({}, {
|
|
31
38
|
get(_target, prop) {
|
|
@@ -38,6 +45,12 @@ export const genericSlotsTestingDummy = new Proxy({}, {
|
|
|
38
45
|
});
|
|
39
46
|
/**
|
|
40
47
|
* @public
|
|
48
|
+
*
|
|
49
|
+
* Aids in type inference for creating props objects for xstate-tree views.
|
|
50
|
+
*
|
|
51
|
+
* @param view - The view to create props for
|
|
52
|
+
* @param props - The actions/selectors props to pass to the view
|
|
53
|
+
* @returns An object with the view's selectors, actions, and inState function props
|
|
41
54
|
*/
|
|
42
55
|
export function buildViewProps(_view, props) {
|
|
43
56
|
return {
|
package/lib/xstate-tree.d.ts
CHANGED
|
@@ -57,16 +57,37 @@ export declare type ArgumentsForRoute<T> = T extends Route<infer TParams, infer
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* @public
|
|
60
|
+
*
|
|
61
|
+
* Broadcasts a global event to all xstate-tree machines
|
|
62
|
+
*
|
|
63
|
+
* @param event - the event to broadcast
|
|
60
64
|
*/
|
|
61
65
|
export declare function broadcast(event: GlobalEvents): void;
|
|
62
66
|
|
|
63
67
|
/**
|
|
64
68
|
* @public
|
|
65
|
-
|
|
69
|
+
*
|
|
70
|
+
* Factory function for actions. The actions function is passed two arguments:
|
|
71
|
+
* - `send` - the interpreters send function, which can be used to send events to the machine
|
|
72
|
+
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
73
|
+
*
|
|
74
|
+
* The resulting action function has memoization. It will return the same value until the
|
|
75
|
+
* selectors reference changes or the send reference changes
|
|
76
|
+
*
|
|
77
|
+
* @param machine - The machine to create the actions for
|
|
78
|
+
* @param selectors - The selectors function
|
|
79
|
+
* @param actions - The action function
|
|
80
|
+
* @returns The actions function - ready to be passed to {@link buildView}
|
|
81
|
+
* */
|
|
66
82
|
export declare function buildActions<TMachine extends AnyStateMachine, TActions, TSelectors, TSend = InterpreterFrom<TMachine>["send"]>(__machine: TMachine, __selectors: TSelectors, actions: (send: TSend, selectors: OutputFromSelector<TSelectors>) => TActions): (send: TSend, selectors: OutputFromSelector<TSelectors>) => TActions;
|
|
67
83
|
|
|
68
84
|
/**
|
|
69
85
|
* @public
|
|
86
|
+
*
|
|
87
|
+
* Creates a route factory
|
|
88
|
+
*
|
|
89
|
+
* @param history - the history object to use for this route factory, this needs to be the same one used in the trees root component
|
|
90
|
+
* @param basePath - the base path for this route factory
|
|
70
91
|
*/
|
|
71
92
|
export declare function buildCreateRoute(history: XstateTreeHistory, basePath: string): {
|
|
72
93
|
/**
|
|
@@ -146,6 +167,11 @@ export declare function buildCreateRoute(history: XstateTreeHistory, basePath: s
|
|
|
146
167
|
|
|
147
168
|
/**
|
|
148
169
|
* @public
|
|
170
|
+
*
|
|
171
|
+
* Builds a React host component for the root machine of an xstate-tree
|
|
172
|
+
*
|
|
173
|
+
* @param machine - The root machine of the tree
|
|
174
|
+
* @param routing - The routing configuration for the tree
|
|
149
175
|
*/
|
|
150
176
|
export declare function buildRootComponent(machine: AnyXstateTreeMachine, routing?: {
|
|
151
177
|
routes: AnyRoute[];
|
|
@@ -160,6 +186,19 @@ export declare function buildRootComponent(machine: AnyXstateTreeMachine, routin
|
|
|
160
186
|
|
|
161
187
|
/**
|
|
162
188
|
* @public
|
|
189
|
+
*
|
|
190
|
+
* Factory function for selectors. The selectors function is passed three arguments:
|
|
191
|
+
* - `ctx` - the current context of the machines state
|
|
192
|
+
* - `canHandleEvent` - a function that can be used to determine if the machine can handle a
|
|
193
|
+
* given event, by simulating sending the event and seeing if a stat change would happen.
|
|
194
|
+
* Handles guards
|
|
195
|
+
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
196
|
+
*
|
|
197
|
+
* The resulting selector function has memoization. It will return the same value until the
|
|
198
|
+
* machine's state changes or the machine's context changes
|
|
199
|
+
* @param machine - The machine to create the selectors for
|
|
200
|
+
* @param selectors - The selector function
|
|
201
|
+
* @returns The selectors - ready to be passed to {@link buildActions}
|
|
163
202
|
*/
|
|
164
203
|
export declare function buildSelectors<TMachine extends AnyStateMachine, TSelectors, TContext = ContextFrom<TMachine>>(__machine: TMachine, selectors: (ctx: TContext, canHandleEvent: CanHandleEvent<TMachine>, inState: MatchesFrom<TMachine>, __currentState: never) => TSelectors): Selectors<TContext, EventFrom<TMachine>, TSelectors, MatchesFrom<TMachine>>;
|
|
165
204
|
|
|
@@ -186,16 +225,43 @@ export declare function buildTestRootComponent<TMachine extends AnyStateMachine,
|
|
|
186
225
|
|
|
187
226
|
/**
|
|
188
227
|
* @public
|
|
228
|
+
*
|
|
229
|
+
* Factory function for views. The view is passed four props:
|
|
230
|
+
* - `slots` - the slots object, which can be used to render the children of the view invoked by the machine
|
|
231
|
+
* - `actions` - the output of the actions function from {@link buildActions}
|
|
232
|
+
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
233
|
+
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
234
|
+
*
|
|
235
|
+
* The resulting view is wrapped in React.memo, it will re-render when the actions or selectors reference changes
|
|
236
|
+
*
|
|
237
|
+
* @param machine - The machine to create the view for
|
|
238
|
+
* @param selectors - The selectors function from {@link buildSelectors}
|
|
239
|
+
* @param actions - The actions function from {@link buildActions}
|
|
240
|
+
* @param slots - The array of slots that can be rendered by the view
|
|
241
|
+
* @param view - The view function
|
|
242
|
+
* @returns The React view
|
|
189
243
|
*/
|
|
190
244
|
export declare function buildView<TMachine extends AnyStateMachine, TEvent extends EventObject, TActions, TSelectors extends AnySelector, TSlots extends readonly Slot[] = [], TMatches extends AnyFunction = MatchesFrom<TMachine>, TViewProps = ViewProps<OutputFromSelector<TSelectors>, TActions, TSlots, TMatches>, TSend = (send: TEvent) => void>(__machine: TMachine, __selectors: TSelectors, __actions: (send: TSend, selectors: OutputFromSelector<TSelectors>) => TActions, __slots: TSlots, view: React_2.ComponentType<TViewProps>): React_2.ComponentType<TViewProps>;
|
|
191
245
|
|
|
192
246
|
/**
|
|
193
247
|
* @public
|
|
248
|
+
*
|
|
249
|
+
* Aids in type inference for creating props objects for xstate-tree views.
|
|
250
|
+
*
|
|
251
|
+
* @param view - The view to create props for
|
|
252
|
+
* @param props - The actions/selectors props to pass to the view
|
|
253
|
+
* @returns An object with the view's selectors, actions, and inState function props
|
|
194
254
|
*/
|
|
195
255
|
export declare function buildViewProps<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>>(_view: C, props: Pick<InferViewProps<PropsOf<C>>, "actions" | "selectors">): InferViewProps<PropsOf<C>>;
|
|
196
256
|
|
|
197
257
|
/**
|
|
198
258
|
* @public
|
|
259
|
+
*
|
|
260
|
+
* staples xstate machine and xstate-tree metadata together into an xstate-tree machine
|
|
261
|
+
*
|
|
262
|
+
* @param machine - The machine to staple the selectors/actions/slots/view to
|
|
263
|
+
* @param metadata - The xstate-tree metadata to staple to the machine
|
|
264
|
+
* @returns The xstate-tree machine, ready to be invoked by other xstate-machines or used with `buildRootComponent`
|
|
199
265
|
*/
|
|
200
266
|
export declare function buildXStateTreeMachine<TMachine extends AnyStateMachine, TSelectors extends AnySelector, TActions extends AnyActions>(machine: TMachine, meta: XStateTreeMachineMeta<TMachine, TSelectors, TActions>): StateMachine<ContextFrom<TMachine>, XstateTreeMachineStateSchema<TMachine, TSelectors, TActions>, EventFrom<TMachine>, any, any, any, any>;
|
|
201
267
|
|
|
@@ -213,6 +279,8 @@ declare type Events = any;
|
|
|
213
279
|
|
|
214
280
|
/**
|
|
215
281
|
* @public
|
|
282
|
+
*
|
|
283
|
+
* Can be used as the slots prop for an xstate-tree view, will render a div containing a <p>slotName-slot<p> for each slot
|
|
216
284
|
*/
|
|
217
285
|
export declare const genericSlotsTestingDummy: any;
|
|
218
286
|
|
|
@@ -249,6 +317,7 @@ keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)
|
|
|
249
317
|
|
|
250
318
|
/**
|
|
251
319
|
* @public
|
|
320
|
+
*
|
|
252
321
|
* Wraps an xstate-tree returning Promise (generated by `import()` in an xstate-tree machine responsible for
|
|
253
322
|
* booting up the machine upon resolution
|
|
254
323
|
*
|
|
@@ -260,6 +329,7 @@ export declare function lazy<TMachine extends AnyStateMachine>(factory: () => Pr
|
|
|
260
329
|
|
|
261
330
|
/**
|
|
262
331
|
* @public
|
|
332
|
+
*
|
|
263
333
|
* Renders an anchor tag pointing at the provided Route
|
|
264
334
|
*
|
|
265
335
|
* The query/params/meta props are conditionally required based on the
|
|
@@ -304,6 +374,8 @@ export declare function matchRoute<TRoutes extends Route<any, any, any, any>[]>(
|
|
|
304
374
|
|
|
305
375
|
/**
|
|
306
376
|
* @public
|
|
377
|
+
*
|
|
378
|
+
* Extract meta type from route object
|
|
307
379
|
*/
|
|
308
380
|
export declare type Meta<T> = T extends {
|
|
309
381
|
meta: infer TMeta;
|
|
@@ -329,6 +401,10 @@ declare type OmitOptional<T> = {
|
|
|
329
401
|
|
|
330
402
|
/**
|
|
331
403
|
* @public
|
|
404
|
+
*
|
|
405
|
+
* Allows hooking in to the global events sent between machines
|
|
406
|
+
*
|
|
407
|
+
* @param handler - the handler to call when an event is broadcast
|
|
332
408
|
*/
|
|
333
409
|
export declare function onBroadcast(handler: (event: GlobalEvents) => void): () => void;
|
|
334
410
|
|
|
@@ -360,6 +436,8 @@ export declare type OutputFromSelector<T> = T extends Selectors<any, any, infer
|
|
|
360
436
|
|
|
361
437
|
/**
|
|
362
438
|
* @public
|
|
439
|
+
*
|
|
440
|
+
* Extract params type from route object object
|
|
363
441
|
*/
|
|
364
442
|
export declare type Params<T> = T extends {
|
|
365
443
|
params: infer TParams;
|
|
@@ -380,6 +458,8 @@ declare type PropsOf<C extends keyof JSX.IntrinsicElements | JSXElementConstruct
|
|
|
380
458
|
|
|
381
459
|
/**
|
|
382
460
|
* @public
|
|
461
|
+
*
|
|
462
|
+
* Extract query type from route object
|
|
383
463
|
*/
|
|
384
464
|
export declare type Query<T> = T extends {
|
|
385
465
|
query: infer TQuery;
|
|
@@ -397,6 +477,8 @@ declare type Return<TRoutes extends Route<any, any, any, any>[]> = {
|
|
|
397
477
|
|
|
398
478
|
/**
|
|
399
479
|
* @public
|
|
480
|
+
*
|
|
481
|
+
* xstate-tree routing event
|
|
400
482
|
*/
|
|
401
483
|
export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
402
484
|
/**
|
|
@@ -438,6 +520,9 @@ export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
|
438
520
|
getEvent: RouteArgumentFunctions<{
|
|
439
521
|
type: TEvent;
|
|
440
522
|
} & RouteArguments<TParams, TQuery, TMeta>, TParams, TQuery, TMeta>;
|
|
523
|
+
/**
|
|
524
|
+
* Event type for this route
|
|
525
|
+
*/
|
|
441
526
|
event: TEvent;
|
|
442
527
|
url?: string;
|
|
443
528
|
history: XstateTreeHistory;
|
|
@@ -478,11 +563,15 @@ export declare type RouteArguments<TParams, TQuery, TMeta> = TParams extends und
|
|
|
478
563
|
|
|
479
564
|
/**
|
|
480
565
|
* @public
|
|
566
|
+
*
|
|
567
|
+
* Extract meta type from route
|
|
481
568
|
*/
|
|
482
569
|
export declare type RouteMeta<T> = T extends Route<any, any, any, infer TMeta> ? TMeta : undefined;
|
|
483
570
|
|
|
484
571
|
/**
|
|
485
572
|
* @public
|
|
573
|
+
*
|
|
574
|
+
* Extract params type from route
|
|
486
575
|
*/
|
|
487
576
|
export declare type RouteParams<T> = T extends Route<infer TParams, any, any, any> ? TParams : undefined;
|
|
488
577
|
|
|
@@ -496,6 +585,7 @@ export declare type Routing404Event = {
|
|
|
496
585
|
|
|
497
586
|
/**
|
|
498
587
|
* @public
|
|
588
|
+
*
|
|
499
589
|
* Converts a Route type into the Event that will be broadcast for that route
|
|
500
590
|
*
|
|
501
591
|
* All routes a machine should handle should be added to the machines event union using this type
|
|
@@ -519,6 +609,9 @@ export declare type Selectors<TContext, TEvent, TSelectors, TMatches> = (ctx: TC
|
|
|
519
609
|
export declare type SharedMeta = {
|
|
520
610
|
/**
|
|
521
611
|
* Suppresses this routing change event from being picked up by react-router
|
|
612
|
+
*
|
|
613
|
+
* This is for a Koordinates specific modification to react-router
|
|
614
|
+
* TODO: Remove once there are user providable shared meta
|
|
522
615
|
*/
|
|
523
616
|
doNotNotifyReactRouter?: boolean;
|
|
524
617
|
/**
|
|
@@ -556,6 +649,11 @@ export declare type Slot = SingleSlot<any> | MultiSlot<any>;
|
|
|
556
649
|
|
|
557
650
|
/**
|
|
558
651
|
* @public
|
|
652
|
+
*
|
|
653
|
+
* Creates a dummy machine that just renders the supplied string - useful for rendering xstate-tree views in isolation
|
|
654
|
+
*
|
|
655
|
+
* @param name - the string to render in the machines view
|
|
656
|
+
* @returns a dummy machine that renders a div containing the supplied string
|
|
559
657
|
*/
|
|
560
658
|
export declare function slotTestingDummyFactory(name: string): StateMachine<unknown, XstateTreeMachineStateSchema<StateMachine<unknown, any, AnyEventObject, {
|
|
561
659
|
value: any;
|
package/lib/xstateTree.js
CHANGED
|
@@ -10,6 +10,10 @@ import { isLikelyPageLoad } from "./utils";
|
|
|
10
10
|
export const emitter = new TinyEmitter();
|
|
11
11
|
/**
|
|
12
12
|
* @public
|
|
13
|
+
*
|
|
14
|
+
* Broadcasts a global event to all xstate-tree machines
|
|
15
|
+
*
|
|
16
|
+
* @param event - the event to broadcast
|
|
13
17
|
*/
|
|
14
18
|
export function broadcast(event) {
|
|
15
19
|
console.debug("[xstate-tree] broadcasting event ", event.type);
|
|
@@ -17,6 +21,10 @@ export function broadcast(event) {
|
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
23
|
* @public
|
|
24
|
+
*
|
|
25
|
+
* Allows hooking in to the global events sent between machines
|
|
26
|
+
*
|
|
27
|
+
* @param handler - the handler to call when an event is broadcast
|
|
20
28
|
*/
|
|
21
29
|
export function onBroadcast(handler) {
|
|
22
30
|
emitter.on("event", handler);
|
|
@@ -138,6 +146,11 @@ export function recursivelySend(service, event) {
|
|
|
138
146
|
}
|
|
139
147
|
/**
|
|
140
148
|
* @public
|
|
149
|
+
*
|
|
150
|
+
* Builds a React host component for the root machine of an xstate-tree
|
|
151
|
+
*
|
|
152
|
+
* @param machine - The root machine of the tree
|
|
153
|
+
* @param routing - The routing configuration for the tree
|
|
141
154
|
*/
|
|
142
155
|
export function buildRootComponent(machine, routing) {
|
|
143
156
|
if (!machine.meta) {
|
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": "2.0.
|
|
5
|
+
"version": "2.0.7",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Build UIs with Actors using xstate and React",
|
|
8
8
|
"keywords": [
|
|
@@ -73,7 +73,8 @@
|
|
|
73
73
|
"build": "rimraf dist && tsc -p tsconfig.build.json",
|
|
74
74
|
"build:watch": "tsc -p tsconfig.build.json -w",
|
|
75
75
|
"api-extractor": "api-extractor run",
|
|
76
|
-
"release": "semantic-release"
|
|
76
|
+
"release": "semantic-release",
|
|
77
|
+
"commitlint": "commitlint --edit"
|
|
77
78
|
},
|
|
78
79
|
"files": [
|
|
79
80
|
"lib/**/*.js",
|