@koordinates/xstate-tree 4.6.4 → 5.1.0-next.10
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 +13 -129
- package/lib/index.js +1 -4
- package/lib/lazy.js +23 -23
- package/lib/routing/Link.js +5 -6
- package/lib/routing/createRoute/createRoute.js +9 -10
- package/lib/routing/handleLocationChange/handleLocationChange.js +2 -2
- package/lib/routing/matchRoute/matchRoute.js +2 -3
- package/lib/routing/providers.js +2 -3
- package/lib/routing/useRouteArgsIfActive.js +1 -1
- package/lib/testingUtilities.js +7 -145
- package/lib/useService.js +9 -49
- package/lib/utils.js +19 -1
- package/lib/xstate-tree.d.ts +54 -228
- package/lib/xstateTree.js +69 -88
- package/package.json +7 -8
package/lib/useService.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useService = exports.loggingMetaOptions = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
5
|
/**
|
|
7
6
|
* @public
|
|
8
7
|
*/
|
|
@@ -23,9 +22,9 @@ exports.loggingMetaOptions = loggingMetaOptions;
|
|
|
23
22
|
* @internal
|
|
24
23
|
*/
|
|
25
24
|
function useService(service) {
|
|
26
|
-
const [current, setCurrent] = (0, react_1.useState)(service.
|
|
27
|
-
const [children, setChildren] = (0, react_1.useState)(service.children);
|
|
28
|
-
const childrenRef = (0, react_1.useRef)(
|
|
25
|
+
const [current, setCurrent] = (0, react_1.useState)(service.getSnapshot());
|
|
26
|
+
const [children, setChildren] = (0, react_1.useState)(service.getSnapshot().children);
|
|
27
|
+
const childrenRef = (0, react_1.useRef)({});
|
|
29
28
|
(0, react_1.useEffect)(() => {
|
|
30
29
|
childrenRef.current = children;
|
|
31
30
|
}, [children]);
|
|
@@ -33,56 +32,17 @@ function useService(service) {
|
|
|
33
32
|
// Set to current service state as there is a possibility
|
|
34
33
|
// of a transition occurring between the initial useState()
|
|
35
34
|
// initialization and useEffect() commit.
|
|
36
|
-
setCurrent(service.
|
|
37
|
-
setChildren(service.children);
|
|
38
|
-
const listener = function (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!(0, utils_1.isEqual)(childrenRef.current, service.children)) {
|
|
42
|
-
setChildren(new Map(service.children));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
35
|
+
setCurrent(service.getSnapshot());
|
|
36
|
+
setChildren(service.getSnapshot().children);
|
|
37
|
+
const listener = function (snapshot) {
|
|
38
|
+
setCurrent(snapshot);
|
|
39
|
+
setChildren(service.getSnapshot().children);
|
|
45
40
|
};
|
|
46
41
|
const sub = service.subscribe(listener);
|
|
47
42
|
return function () {
|
|
48
43
|
sub.unsubscribe();
|
|
49
44
|
};
|
|
50
45
|
}, [service, setChildren]);
|
|
51
|
-
|
|
52
|
-
function handler(event) {
|
|
53
|
-
var _a, _b, _c;
|
|
54
|
-
if (event.type.includes("done")) {
|
|
55
|
-
const idOfFinishedChild = event.type.split(".")[2];
|
|
56
|
-
childrenRef.current.delete(idOfFinishedChild);
|
|
57
|
-
setChildren(new Map(childrenRef.current));
|
|
58
|
-
}
|
|
59
|
-
console.debug(`[xstate-tree] ${service.id} handling event`, ((_c = (_b = (_a = service.machine.meta) === null || _a === void 0 ? void 0 : _a.xstateTree) === null || _b === void 0 ? void 0 : _b.ignoredEvents) === null || _c === void 0 ? void 0 : _c.has(event.type))
|
|
60
|
-
? event.type
|
|
61
|
-
: event);
|
|
62
|
-
}
|
|
63
|
-
let prevState = undefined;
|
|
64
|
-
function transitionHandler(state) {
|
|
65
|
-
var _a, _b;
|
|
66
|
-
const ignoreContext = (_b = (_a = service.machine.meta) === null || _a === void 0 ? void 0 : _a.xstateTree) === null || _b === void 0 ? void 0 : _b.ignoreContext;
|
|
67
|
-
const context = ignoreContext ? "[context omitted]" : state.context;
|
|
68
|
-
if (prevState) {
|
|
69
|
-
console.debug(`[xstate-tree] ${service.id} transitioning from`, prevState.value, "to", state.value, context);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
console.debug(`[xstate-tree] ${service.id} transitioning to ${state.value}`, context);
|
|
73
|
-
}
|
|
74
|
-
prevState = state;
|
|
75
|
-
}
|
|
76
|
-
service.onEvent(handler);
|
|
77
|
-
service.onTransition(transitionHandler);
|
|
78
|
-
return () => {
|
|
79
|
-
service.off(handler);
|
|
80
|
-
service.off(transitionHandler);
|
|
81
|
-
};
|
|
82
|
-
}, [service, setChildren]);
|
|
83
|
-
return [
|
|
84
|
-
current,
|
|
85
|
-
children,
|
|
86
|
-
];
|
|
46
|
+
return [current, children];
|
|
87
47
|
}
|
|
88
48
|
exports.useService = useService;
|
package/lib/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mergeMeta = exports.isNil = exports.isEqual = exports.difference = exports.assert = exports.assertIsDefined = exports.delay = void 0;
|
|
3
|
+
exports.toJSON = exports.mergeMeta = exports.isNil = exports.isEqual = exports.difference = exports.assert = exports.assertIsDefined = exports.delay = void 0;
|
|
4
4
|
function delay(ms = 0) {
|
|
5
5
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
6
|
}
|
|
@@ -121,3 +121,21 @@ function mergeMeta(meta) {
|
|
|
121
121
|
}, {});
|
|
122
122
|
}
|
|
123
123
|
exports.mergeMeta = mergeMeta;
|
|
124
|
+
function getCircularReplacer() {
|
|
125
|
+
const seen = new WeakSet();
|
|
126
|
+
return (key, value) => {
|
|
127
|
+
if (typeof value === "object" && value !== null) {
|
|
128
|
+
if (seen.has(value)) {
|
|
129
|
+
// Circular reference found, discard key
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// Store value in our set
|
|
133
|
+
seen.add(value);
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function toJSON(value) {
|
|
139
|
+
return JSON.parse(JSON.stringify(value, getCircularReplacer()));
|
|
140
|
+
}
|
|
141
|
+
exports.toJSON = toJSON;
|
package/lib/xstate-tree.d.ts
CHANGED
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnyFunction } from 'xstate';
|
|
1
|
+
import type { ActorRefFrom } from 'xstate';
|
|
3
2
|
import { AnyStateMachine } from 'xstate';
|
|
4
|
-
import { BaseActionObject } from 'xstate';
|
|
5
|
-
import { ComponentPropsWithRef } from 'react';
|
|
6
3
|
import { ContextFrom } from 'xstate';
|
|
7
|
-
import { EventFrom } from 'xstate';
|
|
4
|
+
import type { EventFrom } from 'xstate';
|
|
8
5
|
import { EventObject } from 'xstate';
|
|
9
6
|
import { History as History_2 } from 'history';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
7
|
+
import { InputFrom } from 'xstate';
|
|
8
|
+
import type { IsNever } from 'xstate';
|
|
12
9
|
import { ParsedQuery } from 'query-string';
|
|
13
10
|
import { default as React_2 } from 'react';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import type { StateFrom } from 'xstate';
|
|
17
|
-
import { StateMachine } from 'xstate';
|
|
18
|
-
import { TypegenDisabled } from 'xstate';
|
|
11
|
+
import type { SnapshotFrom } from 'xstate';
|
|
12
|
+
import type { StateValue } from 'xstate';
|
|
19
13
|
import * as Z from 'zod';
|
|
20
14
|
|
|
21
15
|
/**
|
|
22
16
|
* @public
|
|
23
17
|
*/
|
|
24
18
|
export declare type Actions<TMachine extends AnyStateMachine, TSelectorsOutput, TOut> = (args: {
|
|
25
|
-
send:
|
|
19
|
+
send: ActorRefFrom<TMachine>["send"];
|
|
26
20
|
selectors: TSelectorsOutput;
|
|
27
21
|
}) => TOut;
|
|
28
22
|
|
|
@@ -31,16 +25,7 @@ export declare type Actions<TMachine extends AnyStateMachine, TSelectorsOutput,
|
|
|
31
25
|
*
|
|
32
26
|
* Retrieves the actions return type from the xstate-tree machine
|
|
33
27
|
*/
|
|
34
|
-
export declare type ActionsFrom<TMachine extends AnyXstateTreeMachine> = TMachine extends
|
|
35
|
-
meta: {
|
|
36
|
-
actions: infer TOut;
|
|
37
|
-
};
|
|
38
|
-
} ? TOut extends (...args: any) => any ? ReturnType<TOut> : never : never : never;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
export declare type AnyActions = (send: any, selectors: any) => any;
|
|
28
|
+
export declare type ActionsFrom<TMachine extends AnyXstateTreeMachine> = TMachine["_xstateTree"] extends XstateTreeMachineStateSchemaV2<any, any, infer TOut, any> ? TOut : never;
|
|
44
29
|
|
|
45
30
|
/**
|
|
46
31
|
* @public
|
|
@@ -65,12 +50,7 @@ export declare type AnyRoute = {
|
|
|
65
50
|
/**
|
|
66
51
|
* @public
|
|
67
52
|
*/
|
|
68
|
-
export declare type
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @public
|
|
72
|
-
*/
|
|
73
|
-
export declare type AnyXstateTreeMachine = StateMachine<any, XstateTreeMachineStateSchemaV1<AnyStateMachine, AnySelector, AnyActions> | XstateTreeMachineStateSchemaV2<AnyStateMachine, any, any>, any>;
|
|
53
|
+
export declare type AnyXstateTreeMachine = XstateTreeMachine<AnyStateMachine, any, any, any[]>;
|
|
74
54
|
|
|
75
55
|
/**
|
|
76
56
|
* @public
|
|
@@ -86,24 +66,6 @@ export declare type ArgumentsForRoute<T> = T extends Route<infer TParams, infer
|
|
|
86
66
|
*/
|
|
87
67
|
export declare function broadcast(event: GlobalEvents): void;
|
|
88
68
|
|
|
89
|
-
/**
|
|
90
|
-
* @public
|
|
91
|
-
*
|
|
92
|
-
* Factory function for actions. The actions function is passed two arguments:
|
|
93
|
-
* - `send` - the interpreters send function, which can be used to send events to the machine
|
|
94
|
-
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
95
|
-
*
|
|
96
|
-
* The resulting action function will only be called once per invocation of a machine.
|
|
97
|
-
* The selectors are passed in as a proxy to always read the latest selector value
|
|
98
|
-
*
|
|
99
|
-
* @param machine - The machine to create the actions for
|
|
100
|
-
* @param selectors - The selectors function
|
|
101
|
-
* @param actions - The action function
|
|
102
|
-
* @returns The actions function - ready to be passed to {@link buildView}
|
|
103
|
-
* @deprecated use {@link createXStateTreeMachine} instead
|
|
104
|
-
* */
|
|
105
|
-
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;
|
|
106
|
-
|
|
107
69
|
/**
|
|
108
70
|
* @public
|
|
109
71
|
*
|
|
@@ -171,15 +133,11 @@ export declare function buildCreateRoute(history: () => XstateTreeHistory, baseP
|
|
|
171
133
|
* @param machine - The root machine of the tree
|
|
172
134
|
* @param routing - The routing configuration for the tree
|
|
173
135
|
*/
|
|
174
|
-
export declare function buildRootComponent(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
getQueryString?: () => string;
|
|
180
|
-
}): {
|
|
181
|
-
(): JSX.Element | null;
|
|
182
|
-
rootMachine: AnyXstateTreeMachine;
|
|
136
|
+
export declare function buildRootComponent<TMachine extends AnyXstateTreeMachine>(options: {
|
|
137
|
+
machine: TMachine;
|
|
138
|
+
} & MarkOptionalLikePropertiesOptional<RootOptions<InputFrom<TMachine>>>): {
|
|
139
|
+
(): JSX.Element;
|
|
140
|
+
rootMachine: TMachine;
|
|
183
141
|
};
|
|
184
142
|
|
|
185
143
|
/**
|
|
@@ -196,97 +154,11 @@ export declare function buildRootComponent(machine: AnyXstateTreeMachine, routin
|
|
|
196
154
|
*/
|
|
197
155
|
export declare function buildRoutingMachine<TRoutes extends AnyRoute[]>(_routes: TRoutes, mappings: Record<TRoutes[number]["event"], AnyXstateTreeMachine>): AnyXstateTreeMachine;
|
|
198
156
|
|
|
199
|
-
/**
|
|
200
|
-
* @public
|
|
201
|
-
*
|
|
202
|
-
* Factory function for selectors. The selectors function is passed three arguments:
|
|
203
|
-
* - `ctx` - the current context of the machines state
|
|
204
|
-
* - `canHandleEvent` - a function that can be used to determine if the machine can handle a
|
|
205
|
-
* given event, by simulating sending the event and seeing if a stat change would happen.
|
|
206
|
-
* Handles guards
|
|
207
|
-
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
208
|
-
*
|
|
209
|
-
* The resulting selector function has memoization. It will return the same value until the
|
|
210
|
-
* machine's state changes or the machine's context changes
|
|
211
|
-
* @param machine - The machine to create the selectors for
|
|
212
|
-
* @param selectors - The selector function
|
|
213
|
-
* @returns The selectors - ready to be passed to {@link buildActions}
|
|
214
|
-
* @deprecated use {@link createXStateTreeMachine} instead
|
|
215
|
-
*/
|
|
216
|
-
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): V1Selectors<TContext, EventFrom<TMachine>, TSelectors, MatchesFrom<TMachine>>;
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* @public
|
|
220
|
-
*
|
|
221
|
-
* Sets up a root component for use in an \@xstate/test model backed by \@testing-library/react for the component
|
|
222
|
-
*
|
|
223
|
-
* The logger argument should just be a simple function which forwards the arguments to console.log,
|
|
224
|
-
* this is needed because Wallaby.js only displays console logs in tests that come from source code, not library code,
|
|
225
|
-
* so any logs from inside this file don't show up in the test explorer
|
|
226
|
-
*
|
|
227
|
-
* The returned object has a `rootComponent` property and a function, `awaitTransition`, that returns a Promise
|
|
228
|
-
* when called that is resolved the next time the underlying machine transitions. This can be used in the \@xstate/test
|
|
229
|
-
* model to ensure after an event action is executed the test in the next state doesn't run until after the machine transitions
|
|
230
|
-
*
|
|
231
|
-
* It also delays for 5ms to ensure any React re-rendering happens in response to the state transition
|
|
232
|
-
*/
|
|
233
|
-
export declare function buildTestRootComponent<TMachine extends AnyStateMachine, TSelectors extends AnySelector, TActions extends AnyActions, TContext = ContextFrom<TMachine>>(machine: StateMachine<TContext, XstateTreeMachineStateSchemaV1<TMachine, TSelectors, TActions> | XstateTreeMachineStateSchemaV2<TMachine, TSelectors, TActions>, EventFrom<TMachine>>, logger: typeof console.log): {
|
|
234
|
-
rootComponent: () => JSX.Element | null;
|
|
235
|
-
addTransitionListener: (listener: () => void) => void;
|
|
236
|
-
awaitTransition(): Promise<void>;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* @public
|
|
241
|
-
*
|
|
242
|
-
* Factory function for views. The view is passed four props:
|
|
243
|
-
* - `slots` - the slots object, which can be used to render the children of the view invoked by the machine
|
|
244
|
-
* - `actions` - the output of the actions function from {@link buildActions}
|
|
245
|
-
* - `selectors` - the output of the selectors function from {@link buildSelectors}
|
|
246
|
-
* - `inState` - equivalent to xstates `state.matches`, allows checking if the machine is in a given state
|
|
247
|
-
*
|
|
248
|
-
* The resulting view is wrapped in React.memo, it will re-render when the actions or selectors reference changes
|
|
249
|
-
*
|
|
250
|
-
* @param machine - The machine to create the view for
|
|
251
|
-
* @param selectors - The selectors function from {@link buildSelectors}
|
|
252
|
-
* @param actions - The actions function from {@link buildActions}
|
|
253
|
-
* @param slots - The array of slots that can be rendered by the view
|
|
254
|
-
* @param view - The view function
|
|
255
|
-
* @returns The React view
|
|
256
|
-
* @deprecated use {@link createXStateTreeMachine} instead
|
|
257
|
-
*/
|
|
258
|
-
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>;
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @public
|
|
262
|
-
*
|
|
263
|
-
* Aids in type inference for creating props objects for xstate-tree views.
|
|
264
|
-
*
|
|
265
|
-
* @param view - The view to create props for
|
|
266
|
-
* @param props - The actions/selectors props to pass to the view
|
|
267
|
-
* @returns An object with the view's selectors, actions, and inState function props
|
|
268
|
-
*/
|
|
269
|
-
export declare function buildViewProps<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>>(_view: C, props: Pick<InferViewProps<PropsOf<C>>, "actions" | "selectors">): InferViewProps<PropsOf<C>>;
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* @public
|
|
273
|
-
*
|
|
274
|
-
* staples xstate machine and xstate-tree metadata together into an xstate-tree machine
|
|
275
|
-
*
|
|
276
|
-
* @param machine - The machine to staple the selectors/actions/slots/view to
|
|
277
|
-
* @param metadata - The xstate-tree metadata to staple to the machine
|
|
278
|
-
* @returns The xstate-tree machine, ready to be invoked by other xstate-machines or used with `buildRootComponent`
|
|
279
|
-
* @deprecated use {@link createXStateTreeMachine} instead
|
|
280
|
-
*/
|
|
281
|
-
export declare function buildXStateTreeMachine<TMachine extends AnyStateMachine, TSelectors extends AnySelector, TActions extends AnyActions>(machine: TMachine, meta: XStateTreeMachineMetaV1<TMachine, TSelectors, TActions>): StateMachine<ContextFrom<TMachine>, XstateTreeMachineStateSchemaV1<TMachine, TSelectors, TActions>, EventFrom<TMachine>, any, any, any, any>;
|
|
282
|
-
|
|
283
157
|
/**
|
|
284
158
|
* @internal
|
|
285
159
|
*/
|
|
286
160
|
export declare type CanHandleEvent<TMachine extends AnyStateMachine> = (e: EventFrom<TMachine>) => boolean;
|
|
287
161
|
|
|
288
|
-
declare type Context = {};
|
|
289
|
-
|
|
290
162
|
/**
|
|
291
163
|
* @public
|
|
292
164
|
* Creates an xstate-tree machine from an xstate-machine
|
|
@@ -301,7 +173,7 @@ declare type Context = {};
|
|
|
301
173
|
* @param machine - The xstate machine to create the xstate-tree machine from
|
|
302
174
|
* @param options - the xstate-tree options
|
|
303
175
|
*/
|
|
304
|
-
export declare function createXStateTreeMachine<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = []>(machine: TMachine, options: V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots>):
|
|
176
|
+
export declare function createXStateTreeMachine<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = []>(machine: TMachine, options: V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots>): XstateTreeMachine<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
|
|
305
177
|
|
|
306
178
|
declare type EmptyKeys<T> = keyof {
|
|
307
179
|
[K in keyof T as IsEmptyObject<T[K], true> extends true ? K : never]: T[K];
|
|
@@ -309,8 +181,6 @@ declare type EmptyKeys<T> = keyof {
|
|
|
309
181
|
|
|
310
182
|
declare type EmptyRouteArguments<TParams, TQuery> = IsEmptyObject<TParams, true> extends true ? IsEmptyObject<TQuery, true> extends true ? true : false : false;
|
|
311
183
|
|
|
312
|
-
declare type Events = any;
|
|
313
|
-
|
|
314
184
|
/**
|
|
315
185
|
* @public
|
|
316
186
|
*
|
|
@@ -339,16 +209,12 @@ export declare type GlobalEvents = {
|
|
|
339
209
|
};
|
|
340
210
|
}[keyof XstateTreeEvents];
|
|
341
211
|
|
|
342
|
-
declare type InferViewProps<T> = T extends ViewProps<infer TSelectors, infer TActions, never, infer TMatches> ? {
|
|
343
|
-
selectors: TSelectors;
|
|
344
|
-
actions: TActions;
|
|
345
|
-
inState: (state: Parameters<TMatches>[0]) => TMatches;
|
|
346
|
-
} : never;
|
|
347
|
-
|
|
348
212
|
declare type IsEmptyObject<Obj, ExcludeOptional extends boolean = false> = undefined extends Obj ? true : [keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)] extends [
|
|
349
213
|
never
|
|
350
214
|
] ? true : false;
|
|
351
215
|
|
|
216
|
+
declare type IsUnknown<T> = unknown extends T ? true : false;
|
|
217
|
+
|
|
352
218
|
/**
|
|
353
219
|
* @public
|
|
354
220
|
*
|
|
@@ -359,7 +225,7 @@ never
|
|
|
359
225
|
* @param options - configure loading component and context to invoke machine with
|
|
360
226
|
* @returns an xstate-tree machine that wraps the promise, invoking the resulting machine when it resolves
|
|
361
227
|
*/
|
|
362
|
-
export declare function lazy<TMachine extends
|
|
228
|
+
export declare function lazy<TMachine extends AnyXstateTreeMachine>(factory: () => Promise<TMachine>, { Loader, input }?: Options<TMachine>): AnyXstateTreeMachine;
|
|
363
229
|
|
|
364
230
|
/**
|
|
365
231
|
* @public
|
|
@@ -410,12 +276,15 @@ export declare function loggingMetaOptions<TEvents extends EventObject, TContext
|
|
|
410
276
|
};
|
|
411
277
|
};
|
|
412
278
|
|
|
413
|
-
|
|
279
|
+
/**
|
|
280
|
+
* Marks any required property that can accept undefined as optional
|
|
281
|
+
*/
|
|
282
|
+
declare type MarkOptionalLikePropertiesOptional<T> = Omit<T, EmptyKeys<T>> & Partial<Pick<T, EmptyKeys<T>>>;
|
|
414
283
|
|
|
415
284
|
/**
|
|
416
285
|
* @internal
|
|
417
286
|
*/
|
|
418
|
-
export declare type MatchesFrom<T extends AnyStateMachine> =
|
|
287
|
+
export declare type MatchesFrom<T extends AnyStateMachine> = (value: ToStatePaths<SnapshotFrom<T>["value"]>) => boolean;
|
|
419
288
|
|
|
420
289
|
/**
|
|
421
290
|
* @public
|
|
@@ -460,7 +329,7 @@ declare type OmitOptional<T> = {
|
|
|
460
329
|
*/
|
|
461
330
|
export declare function onBroadcast(handler: (event: GlobalEvents) => void): () => void;
|
|
462
331
|
|
|
463
|
-
declare type Options<
|
|
332
|
+
declare type Options<TStateMachine extends AnyStateMachine> = {
|
|
464
333
|
/**
|
|
465
334
|
* Displayed while the promise is resolving, defaults to returning null
|
|
466
335
|
*/
|
|
@@ -469,14 +338,9 @@ declare type Options<TContext> = {
|
|
|
469
338
|
* Allows you to specify an overriden context when the machine is invoked
|
|
470
339
|
* Automatically supplies the machines default context so only requires a partial of overrides
|
|
471
340
|
*/
|
|
472
|
-
|
|
341
|
+
input?: InputFrom<TStateMachine>;
|
|
473
342
|
};
|
|
474
343
|
|
|
475
|
-
/**
|
|
476
|
-
* @public
|
|
477
|
-
*/
|
|
478
|
-
export declare type OutputFromSelector<T> = T extends V1Selectors<any, any, infer O, any> ? O : never;
|
|
479
|
-
|
|
480
344
|
/**
|
|
481
345
|
* @public
|
|
482
346
|
*
|
|
@@ -497,8 +361,6 @@ export declare type PickEvent<T extends Extract<GlobalEvents, {
|
|
|
497
361
|
type: T;
|
|
498
362
|
}>;
|
|
499
363
|
|
|
500
|
-
declare type PropsOf<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>> = JSX.LibraryManagedAttributes<C, ComponentPropsWithRef<C>>;
|
|
501
|
-
|
|
502
364
|
/**
|
|
503
365
|
* @public
|
|
504
366
|
*
|
|
@@ -521,6 +383,17 @@ declare type Return<TRoutes extends Route<any, any, any, any>[]> = {
|
|
|
521
383
|
error: unknown;
|
|
522
384
|
};
|
|
523
385
|
|
|
386
|
+
declare type RootOptions<TInput> = {
|
|
387
|
+
routing: {
|
|
388
|
+
routes: AnyRoute[];
|
|
389
|
+
history: XstateTreeHistory<any>;
|
|
390
|
+
basePath: string;
|
|
391
|
+
getPathName?: () => string;
|
|
392
|
+
getQueryString?: () => string;
|
|
393
|
+
} | undefined;
|
|
394
|
+
input: IsUnknown<TInput> extends true ? undefined : TInput;
|
|
395
|
+
};
|
|
396
|
+
|
|
524
397
|
/**
|
|
525
398
|
* @public
|
|
526
399
|
*
|
|
@@ -596,7 +469,7 @@ export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
|
596
469
|
/**
|
|
597
470
|
* @public
|
|
598
471
|
*/
|
|
599
|
-
export declare type RouteArgumentFunctions<TReturn, TParams, TQuery, TMeta, TArgs = RouteArguments<TParams, TQuery, TMeta>> = IsEmptyObject<TArgs> extends true ? () => TReturn : keyof TArgs extends "meta" ? (args?: TArgs) => TReturn : EmptyRouteArguments<TParams, TQuery> extends true ? (args?: Partial<TArgs>) => TReturn : (args:
|
|
472
|
+
export declare type RouteArgumentFunctions<TReturn, TParams, TQuery, TMeta, TArgs = RouteArguments<TParams, TQuery, TMeta>> = IsEmptyObject<TArgs> extends true ? () => TReturn : keyof TArgs extends "meta" ? (args?: TArgs) => TReturn : EmptyRouteArguments<TParams, TQuery> extends true ? (args?: Partial<TArgs>) => TReturn : (args: MarkOptionalLikePropertiesOptional<TArgs>) => TReturn;
|
|
600
473
|
|
|
601
474
|
/**
|
|
602
475
|
* @public
|
|
@@ -643,7 +516,7 @@ export declare type RouteParams<T> = T extends Route<infer TParams, any, any, an
|
|
|
643
516
|
*/
|
|
644
517
|
export declare type RouteQuery<T> = T extends Route<any, infer TQuery, any, any> ? TQuery : undefined;
|
|
645
518
|
|
|
646
|
-
declare type RouteRedirect<TParams, TQuery, TMeta> = (args:
|
|
519
|
+
declare type RouteRedirect<TParams, TQuery, TMeta> = (args: MarkOptionalLikePropertiesOptional<{
|
|
647
520
|
params: TParams;
|
|
648
521
|
query: TQuery;
|
|
649
522
|
meta?: TMeta;
|
|
@@ -688,11 +561,7 @@ export declare type Selectors<TMachine extends AnyStateMachine, TOut> = (args: {
|
|
|
688
561
|
*
|
|
689
562
|
* Retrieves the selector return type from the xstate-tree machine
|
|
690
563
|
*/
|
|
691
|
-
export declare type SelectorsFrom<TMachine extends AnyXstateTreeMachine> = TMachine extends
|
|
692
|
-
meta: {
|
|
693
|
-
selectors: infer TOut;
|
|
694
|
-
};
|
|
695
|
-
} ? TOut extends (...args: any) => any ? ReturnType<TOut> : never : never : never;
|
|
564
|
+
export declare type SelectorsFrom<TMachine extends AnyXstateTreeMachine> = TMachine["_xstateTree"] extends XstateTreeMachineStateSchemaV2<any, infer TOut, any, any> ? TOut : never;
|
|
696
565
|
|
|
697
566
|
/**
|
|
698
567
|
* @public
|
|
@@ -738,19 +607,6 @@ export declare function singleSlot<T extends string>(name: T): SingleSlot<T>;
|
|
|
738
607
|
*/
|
|
739
608
|
export declare type Slot = SingleSlot<any> | MultiSlot<any>;
|
|
740
609
|
|
|
741
|
-
/**
|
|
742
|
-
* @public
|
|
743
|
-
*
|
|
744
|
-
* Creates a dummy machine that just renders the supplied string - useful for rendering xstate-tree views in isolation
|
|
745
|
-
*
|
|
746
|
-
* @param name - the string to render in the machines view
|
|
747
|
-
* @returns a dummy machine that renders a div containing the supplied string
|
|
748
|
-
*/
|
|
749
|
-
export declare function slotTestingDummyFactory(name: string): StateMachine<unknown, XstateTreeMachineStateSchemaV1<StateMachine<unknown, any, AnyEventObject, {
|
|
750
|
-
value: any;
|
|
751
|
-
context: unknown;
|
|
752
|
-
}, BaseActionObject, ServiceMap, ResolveTypegenMeta<TypegenDisabled, AnyEventObject, BaseActionObject, ServiceMap>>, () => {}, () => {}>, AnyEventObject, any, any, any, any>;
|
|
753
|
-
|
|
754
610
|
/**
|
|
755
611
|
* @public
|
|
756
612
|
*/
|
|
@@ -759,14 +615,6 @@ export declare enum SlotType {
|
|
|
759
615
|
MultiSlot = 1
|
|
760
616
|
}
|
|
761
617
|
|
|
762
|
-
declare type States = {
|
|
763
|
-
value: "loading";
|
|
764
|
-
context: Context;
|
|
765
|
-
} | {
|
|
766
|
-
value: "rendering";
|
|
767
|
-
context: Context;
|
|
768
|
-
};
|
|
769
|
-
|
|
770
618
|
/**
|
|
771
619
|
* @public
|
|
772
620
|
*/
|
|
@@ -781,17 +629,21 @@ export declare type StyledLink<TStyleProps = {}> = <TRoute extends AnyRoute>(pro
|
|
|
781
629
|
* @param activeRouteEvents - The active route events to use in the context
|
|
782
630
|
*/
|
|
783
631
|
export declare function TestRoutingContext({ activeRouteEvents, children, }: {
|
|
784
|
-
activeRouteEvents: RoutingEvent<
|
|
632
|
+
activeRouteEvents: RoutingEvent<AnyRoute>[];
|
|
785
633
|
children: React_2.ReactNode;
|
|
786
634
|
}): JSX.Element;
|
|
787
635
|
|
|
636
|
+
declare type ToStatePaths<TStateValue extends StateValue, TParentPath extends string = ""> = TStateValue extends string ? WithParentPath<TStateValue, TParentPath> : IsNever<keyof TStateValue> extends true ? never : WithParentPath<keyof TStateValue & string, TParentPath> | Values<{
|
|
637
|
+
[K in keyof TStateValue & string]?: ToStatePaths<NonNullable<TStateValue[K]>, WithParentPath<K, TParentPath>>;
|
|
638
|
+
}>;
|
|
639
|
+
|
|
788
640
|
/**
|
|
789
641
|
* @public
|
|
790
642
|
*
|
|
791
643
|
* Returns the list of active routing events, or undefined if there are none / used outside of an xstate-tree routing context
|
|
792
644
|
*/
|
|
793
645
|
export declare function useActiveRouteEvents(): {
|
|
794
|
-
type:
|
|
646
|
+
type: string;
|
|
795
647
|
originalUrl: string;
|
|
796
648
|
params: unknown;
|
|
797
649
|
query: unknown;
|
|
@@ -820,11 +672,6 @@ export declare function useIsRouteActive(...routes: AnyRoute[]): boolean;
|
|
|
820
672
|
*/
|
|
821
673
|
export declare function useRouteArgsIfActive<TRoute extends AnyRoute>(route: TRoute): ArgumentsForRoute<TRoute> | undefined;
|
|
822
674
|
|
|
823
|
-
/**
|
|
824
|
-
* @public
|
|
825
|
-
*/
|
|
826
|
-
export declare type V1Selectors<TContext, TEvent, TSelectors, TMatches> = (ctx: TContext, canHandleEvent: (e: TEvent) => boolean, inState: TMatches, __currentState: never) => TSelectors;
|
|
827
|
-
|
|
828
675
|
/**
|
|
829
676
|
* @public
|
|
830
677
|
*/
|
|
@@ -835,6 +682,8 @@ export declare type V2BuilderMeta<TMachine extends AnyStateMachine, TSelectorsOu
|
|
|
835
682
|
View: View<TActionsOutput, TSelectorsOutput, TSlots>;
|
|
836
683
|
};
|
|
837
684
|
|
|
685
|
+
declare type Values<T> = T[keyof T];
|
|
686
|
+
|
|
838
687
|
/**
|
|
839
688
|
* @public
|
|
840
689
|
*/
|
|
@@ -844,19 +693,6 @@ export declare type View<TActionsOutput, TSelectorsOutput, TSlots extends readon
|
|
|
844
693
|
selectors: TSelectorsOutput;
|
|
845
694
|
}>;
|
|
846
695
|
|
|
847
|
-
/**
|
|
848
|
-
* @public
|
|
849
|
-
*/
|
|
850
|
-
export declare type ViewProps<TSelectors, TActions, TSlots extends readonly Slot[], TMatches extends AnyFunction> = {
|
|
851
|
-
slots: Record<GetSlotNames<TSlots>, React_2.ComponentType>;
|
|
852
|
-
actions: TActions;
|
|
853
|
-
selectors: TSelectors;
|
|
854
|
-
/**
|
|
855
|
-
* @deprecated see https://github.com/koordinates/xstate-tree/issues/33 use `inState` in the selector function instead
|
|
856
|
-
*/
|
|
857
|
-
inState: TMatches;
|
|
858
|
-
};
|
|
859
|
-
|
|
860
696
|
/**
|
|
861
697
|
* @public
|
|
862
698
|
*
|
|
@@ -867,6 +703,8 @@ export declare type ViewProps<TSelectors, TActions, TSlots extends readonly Slot
|
|
|
867
703
|
*/
|
|
868
704
|
export declare function viewToMachine(view: () => JSX.Element): AnyXstateTreeMachine;
|
|
869
705
|
|
|
706
|
+
declare type WithParentPath<TCurrent extends string, TParentPath extends string> = `${TParentPath extends "" ? "" : `${TParentPath}.`}${TCurrent}`;
|
|
707
|
+
|
|
870
708
|
/**
|
|
871
709
|
* @public
|
|
872
710
|
*/
|
|
@@ -878,30 +716,18 @@ export declare type XstateTreeHistory<T = unknown> = History_2<{
|
|
|
878
716
|
/**
|
|
879
717
|
* @public
|
|
880
718
|
*/
|
|
881
|
-
export declare type
|
|
882
|
-
slots: TSlots;
|
|
883
|
-
view: React_2.ComponentType<ViewProps<OutputFromSelector<TSelectors>, ReturnType<TActions>, TSlots, MatchesFrom<TMachine>>>;
|
|
884
|
-
selectors: TSelectors;
|
|
885
|
-
actions: TActions;
|
|
886
|
-
xstateTreeMachine?: true;
|
|
887
|
-
};
|
|
719
|
+
export declare type XstateTreeMachine<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = TMachine & XstateTreeMachineInjection<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
|
|
888
720
|
|
|
889
721
|
/**
|
|
890
|
-
* @
|
|
722
|
+
* @internal
|
|
891
723
|
*/
|
|
892
|
-
export declare type
|
|
893
|
-
|
|
894
|
-
builderVersion: 1;
|
|
895
|
-
};
|
|
724
|
+
export declare type XstateTreeMachineInjection<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = {
|
|
725
|
+
_xstateTree: XstateTreeMachineStateSchemaV2<TMachine, TSelectorsOutput, TActionsOutput, TSlots>;
|
|
896
726
|
};
|
|
897
727
|
|
|
898
728
|
/**
|
|
899
729
|
* @public
|
|
900
730
|
*/
|
|
901
|
-
export declare type XstateTreeMachineStateSchemaV2<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> =
|
|
902
|
-
meta: Required<V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots> & {
|
|
903
|
-
builderVersion: 2;
|
|
904
|
-
}>;
|
|
905
|
-
};
|
|
731
|
+
export declare type XstateTreeMachineStateSchemaV2<TMachine extends AnyStateMachine, TSelectorsOutput = ContextFrom<TMachine>, TActionsOutput = Record<never, string>, TSlots extends readonly Slot[] = Slot[]> = Required<V2BuilderMeta<TMachine, TSelectorsOutput, TActionsOutput, TSlots>>;
|
|
906
732
|
|
|
907
733
|
export { }
|