@koordinates/xstate-tree 2.0.5 → 2.0.6

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
@@ -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
  *
@@ -2,6 +2,7 @@ import React from "react";
2
2
  import { useHref } from "./useHref";
3
3
  /**
4
4
  * @public
5
+ *
5
6
  * Renders an anchor tag pointing at the provided Route
6
7
  *
7
8
  * The query/params/meta props are conditionally required based on the
@@ -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, }) {
@@ -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 {
@@ -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",
5
+ "version": "2.0.6",
6
6
  "license": "MIT",
7
7
  "description": "Build UIs with Actors using xstate and React",
8
8
  "keywords": [