@koordinates/xstate-tree 1.0.0-beta.1
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 +90 -0
- package/lib/builders.js +62 -0
- package/lib/index.js +8 -0
- package/lib/lazy.js +51 -0
- package/lib/routing/Link.js +27 -0
- package/lib/routing/createRoute/createRoute.js +185 -0
- package/lib/routing/createRoute/index.js +1 -0
- package/lib/routing/handleLocationChange/handleLocationChange.js +47 -0
- package/lib/routing/handleLocationChange/index.js +1 -0
- package/lib/routing/index.js +6 -0
- package/lib/routing/joinRoutes.js +5 -0
- package/lib/routing/matchRoute/index.js +1 -0
- package/lib/routing/matchRoute/matchRoute.js +35 -0
- package/lib/routing/providers.js +18 -0
- package/lib/routing/routingEvent.js +1 -0
- package/lib/routing/useHref.js +14 -0
- package/lib/setupScript.js +1 -0
- package/lib/slots/index.js +1 -0
- package/lib/slots/slots.js +25 -0
- package/lib/testingUtilities.js +196 -0
- package/lib/types.js +1 -0
- package/lib/useConstant.js +8 -0
- package/lib/useService.js +85 -0
- package/lib/utils.js +22 -0
- package/lib/xstate-tree.d.ts +580 -0
- package/lib/xstateTree.js +204 -0
- package/package.json +94 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { AnyEventObject } from 'xstate';
|
|
2
|
+
import { AnyStateMachine } from 'xstate';
|
|
3
|
+
import { BaseActionObject } from 'xstate';
|
|
4
|
+
import { ComponentPropsWithRef } from 'react';
|
|
5
|
+
import { EventObject } from 'xstate';
|
|
6
|
+
import { History as History_2 } from 'history';
|
|
7
|
+
import { Interpreter } from 'xstate';
|
|
8
|
+
import { JSXElementConstructor } from 'react';
|
|
9
|
+
import { NoInfer } from 'xstate';
|
|
10
|
+
import { ParsedQuery } from 'query-string';
|
|
11
|
+
import { default as React_2 } from 'react';
|
|
12
|
+
import { ResolveTypegenMeta } from 'xstate';
|
|
13
|
+
import { ServiceMap } from 'xstate';
|
|
14
|
+
import { StateMachine } from 'xstate';
|
|
15
|
+
import { TypegenDisabled } from 'xstate';
|
|
16
|
+
import { Typestate } from 'xstate';
|
|
17
|
+
import * as Z from 'zod';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare type AnyRoute = {
|
|
23
|
+
matches: (url: string, search: string) => any;
|
|
24
|
+
reverse: any;
|
|
25
|
+
navigate: any;
|
|
26
|
+
getEvent: any;
|
|
27
|
+
event: string;
|
|
28
|
+
url?: string;
|
|
29
|
+
basePath: string;
|
|
30
|
+
history: XstateTreeHistory;
|
|
31
|
+
parent?: AnyRoute;
|
|
32
|
+
paramsSchema?: Z.ZodObject<any>;
|
|
33
|
+
querySchema?: Z.ZodObject<any>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export declare type ArgumentsForRoute<T> = T extends Route<infer TParams, infer TQuery, any, infer TMeta> ? RouteArguments<TParams, TQuery, TMeta> : never;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export declare function broadcast(event: GlobalEvents): void;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildActions<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TActions, TSelectors, TSend = (send: TEvent) => void>(__machine: StateMachine<TContext, any, TEvent, TTypestate, any, any, any>, __selectors: Selectors<TContext, TEvent, TSelectors, TTypestate["value"]>, actions: (send: TSend, selectors: TSelectors) => TActions): (send: TSend, selectors: TSelectors) => TActions;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export declare function buildCreateRoute(history: XstateTreeHistory, basePath: string): {
|
|
55
|
+
/**
|
|
56
|
+
* Creates a dynamic Route using the supplied options
|
|
57
|
+
*
|
|
58
|
+
* The return value of dynamicRoute is a function that accepts the routes "dynamic" options
|
|
59
|
+
* The argument to dynamicRoute itself is the params/query/meta schemas defining the route
|
|
60
|
+
*
|
|
61
|
+
* The returned function accepts a singular option object with the following fields
|
|
62
|
+
*
|
|
63
|
+
* `event`, the string constant for the routes event
|
|
64
|
+
* `matches`, a function that is passed a url/query string and determines if the route matches
|
|
65
|
+
* if the route is matched it returns the extracted params/query objects
|
|
66
|
+
* `reverse`, a function that is passed params/query objects and turns them into a URL
|
|
67
|
+
*
|
|
68
|
+
* The params and query schemas are ZodSchemas, they both need to be an object (ie Z.object())
|
|
69
|
+
*/
|
|
70
|
+
dynamicRoute: <TOpts extends Options<Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
71
|
+
[x: string]: any;
|
|
72
|
+
}, {
|
|
73
|
+
[x: string]: any;
|
|
74
|
+
}>, Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
75
|
+
[x: string]: any;
|
|
76
|
+
}, {
|
|
77
|
+
[x: string]: any;
|
|
78
|
+
}>, any>>(opts?: TOpts | undefined) => <TEvent extends string, TParamsSchema = Params<TOpts>, TQuerySchema = Query<TOpts>, TMeta = Meta<TOpts>, TParams = TParamsSchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
79
|
+
[x: string]: any;
|
|
80
|
+
}, {
|
|
81
|
+
[x: string]: any;
|
|
82
|
+
}> ? Z.TypeOf<TParamsSchema> : undefined, TQuery = TQuerySchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
83
|
+
[x: string]: any;
|
|
84
|
+
}, {
|
|
85
|
+
[x: string]: any;
|
|
86
|
+
}> ? Z.TypeOf<TQuerySchema> : undefined, TFullMeta = TMeta extends undefined ? SharedMeta : TMeta & SharedMeta>({ event, matches, reverse, }: {
|
|
87
|
+
event: TEvent;
|
|
88
|
+
matches: (url: string, query: ParsedQuery<string>) => false | RouteArguments<TParams, TQuery, TFullMeta>;
|
|
89
|
+
reverse: RouteArgumentFunctions<string, TParams, TQuery, TFullMeta, RouteArguments<TParams, TQuery, TFullMeta>>;
|
|
90
|
+
}) => Route<TParams, TQuery, TEvent, TFullMeta>;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a static Route using the supplied options
|
|
93
|
+
*
|
|
94
|
+
* The return value of staticRoute is a function that accepts the routes options
|
|
95
|
+
* The only argument to staticRoute itself is an optional parent route
|
|
96
|
+
*
|
|
97
|
+
* The returned function accepts 3 arguments
|
|
98
|
+
*
|
|
99
|
+
* 1. URL of the route
|
|
100
|
+
* 2. The event type of the route
|
|
101
|
+
* 3. The routes options, params schema, query schema and meta type
|
|
102
|
+
*
|
|
103
|
+
* The params and query schemas are ZodSchemas, they both need to be an object (ie Z.object())
|
|
104
|
+
*
|
|
105
|
+
* When creating a route that has a parent route, the following happens
|
|
106
|
+
*
|
|
107
|
+
* 1. The parent routes url is prepended to the routes URL
|
|
108
|
+
* 2. The parents params schema is merged with the routes schema
|
|
109
|
+
* 3. The parents meta type is merged with the routes meta type
|
|
110
|
+
*/
|
|
111
|
+
staticRoute: <TBaseRoute extends AnyRoute | undefined = undefined, TBaseParams = RouteParams<TBaseRoute>, TBaseMeta = RouteMeta<TBaseRoute>>(baseRoute?: TBaseRoute | undefined) => <TOpts_1 extends Options<Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
112
|
+
[x: string]: any;
|
|
113
|
+
}, {
|
|
114
|
+
[x: string]: any;
|
|
115
|
+
}>, Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
116
|
+
[x: string]: any;
|
|
117
|
+
}, {
|
|
118
|
+
[x: string]: any;
|
|
119
|
+
}>, any>, TEvent_1 extends string, TParamsSchema_1 = Params<TOpts_1>, TQuerySchema_1 = Query<TOpts_1>, TMeta_1 = Meta<TOpts_1>, TParams_1 = TParamsSchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
120
|
+
[x: string]: any;
|
|
121
|
+
}, {
|
|
122
|
+
[x: string]: any;
|
|
123
|
+
}> ? Z.TypeOf<TParamsSchema_1> : undefined, TQuery_1 = TQuerySchema_1 extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
|
|
124
|
+
[x: string]: any;
|
|
125
|
+
}, {
|
|
126
|
+
[x: string]: any;
|
|
127
|
+
}> ? Z.TypeOf<TQuerySchema_1> : undefined, TFullParams = TParams_1 extends undefined ? TBaseParams extends undefined ? undefined : TBaseParams : TParams_1 & (TBaseParams extends undefined ? {} : TBaseParams), TFullMeta_1 = TMeta_1 extends undefined ? TBaseMeta extends undefined ? SharedMeta : TBaseMeta & SharedMeta : TMeta_1 & (TBaseMeta extends undefined ? {} : TBaseMeta) & SharedMeta>(url: string, event: TEvent_1, opts?: TOpts_1 | undefined) => Route<TFullParams, TQuery_1, TEvent_1, TFullMeta_1>;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @public
|
|
132
|
+
*/
|
|
133
|
+
export declare function buildRootComponent<TContext, TEvent extends EventObject, TTypeState extends Typestate<TContext>, TSelectors, TActions, TSlots extends readonly Slot[]>(machine: StateMachine<TContext, XstateTreeMachineStateSchema<TContext, TEvent, TTypeState, TSelectors, TActions, TSlots>, TEvent, TTypeState>, routing?: {
|
|
134
|
+
routes: Route<any, any, any, any>[];
|
|
135
|
+
history: History_2<{
|
|
136
|
+
meta?: any;
|
|
137
|
+
}>;
|
|
138
|
+
basePath: string;
|
|
139
|
+
getPathName?: () => string;
|
|
140
|
+
getQueryString?: () => string;
|
|
141
|
+
}): {
|
|
142
|
+
(): JSX.Element | null;
|
|
143
|
+
rootMachine: StateMachine<TContext, XstateTreeMachineStateSchema<TContext, TEvent, TTypeState, TSelectors, TActions, TSlots, Interpreter<TContext, any, TEvent, TTypeState, TypegenDisabled>>, TEvent, TTypeState, BaseActionObject, ServiceMap, ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, BaseActionObject, ServiceMap>>;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
export declare function buildSelectors<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TSelectors>(__machine: StateMachine<TContext, any, TEvent, TTypestate, any, any, any>, selectors: Selectors<TContext, TEvent, TSelectors, TTypestate["value"]>): (ctx: TContext, canHandleEvent: (e: TEvent) => boolean, inState: (state: TTypestate["value"]) => boolean, currentState: TTypestate["value"]) => TSelectors;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @internal
|
|
153
|
+
* Builds a root component for use in Storybook
|
|
154
|
+
*
|
|
155
|
+
* Pass in an initial state and context and the machine will start from that state
|
|
156
|
+
*
|
|
157
|
+
* This does _not_ work for any machines using slots, nothing will be invoked unless
|
|
158
|
+
* it would be invoked by the state you have chosen the machine to start in
|
|
159
|
+
*
|
|
160
|
+
* XState will not run any invoke handlers for parent states or sibling states that
|
|
161
|
+
* would be passed through if the machine was executing normally
|
|
162
|
+
*
|
|
163
|
+
* I have no solutions for this
|
|
164
|
+
*/
|
|
165
|
+
export declare function buildStorybookComponent<TContext, TEvent extends EventObject, TTypeState extends Typestate<TContext>, TSelectors, TActions, TSlots extends readonly Slot[]>(machine: StateMachine<TContext, XstateTreeMachineStateSchema<TContext, TEvent, TTypeState, TSelectors, TActions, TSlots>, TEvent, TTypeState>, state?: TTypeState["value"], context?: TContext): () => JSX.Element | null;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @public
|
|
169
|
+
*
|
|
170
|
+
* Sets up a root component for use in an \@xstate/test model backed by \@testing-library/react for the component
|
|
171
|
+
*
|
|
172
|
+
* The logger argument should just be a simple function which forwards the arguments to console.log,
|
|
173
|
+
* this is needed because Wallaby.js only displays console logs in tests that come from source code, not library code,
|
|
174
|
+
* so any logs from inside this file don't show up in the test explorer
|
|
175
|
+
*
|
|
176
|
+
* The returned object has a `rootComponent` property and a function, `awaitTransition`, that returns a Promise
|
|
177
|
+
* when called that is resolved the next time the underlying machine transitions. This can be used in the \@xstate/test
|
|
178
|
+
* model to ensure after an event action is executed the test in the next state doesn't run until after the machine transitions
|
|
179
|
+
*
|
|
180
|
+
* It also delays for 5ms to ensure any React re-rendering happens in response to the state transition
|
|
181
|
+
*/
|
|
182
|
+
export declare function buildTestRootComponent<TContext, TEvent extends EventObject, TTypeState extends Typestate<TContext>, TSelectors, TActions, TSlots extends readonly Slot[]>(machine: StateMachine<TContext, XstateTreeMachineStateSchema<TContext, TEvent, TTypeState, TSelectors, TActions, TSlots>, TEvent, TTypeState>, logger: typeof console.log): {
|
|
183
|
+
rootComponent: () => JSX.Element | null;
|
|
184
|
+
addTransitionListener: (listener: () => void) => void;
|
|
185
|
+
awaitTransition(): Promise<void>;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @public
|
|
190
|
+
*/
|
|
191
|
+
export declare function buildView<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TActions, TSelectors, TSlots extends readonly Slot[] = [], TViewProps = ViewProps<TSelectors, TActions, TSlots, TTypestate["value"]>, TSend = (send: TEvent) => void>(__machine: StateMachine<TContext, any, TEvent, TTypestate, any, any, any>, __selectors: Selectors<TContext, TEvent, TSelectors, TTypestate["value"]>, __actions: (send: TSend, selectors: TSelectors) => TActions, __slots: TSlots, view: React_2.ComponentType<TViewProps>): React_2.ComponentType<TViewProps>;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @public
|
|
195
|
+
*/
|
|
196
|
+
export declare function buildViewProps<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>>(_view: C, props: Pick<InferViewProps<PropsOf<C>>, "actions" | "selectors">): InferViewProps<PropsOf<C>>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @public
|
|
200
|
+
*/
|
|
201
|
+
export declare function buildXStateTreeMachine<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TSelectors = unknown, TActions = unknown, TInterpreter extends Interpreter<TContext, any, TEvent, TTypestate> = Interpreter<TContext, any, TEvent, TTypestate>, TSlots extends readonly Slot[] = Slot[]>(machine: StateMachine<TContext, any, TEvent, TTypestate, any, any, any>, meta: XStateTreeMachineMeta<TContext, TEvent, TTypestate, TSelectors, TActions, TInterpreter, TSlots>): StateMachine<TContext, XstateTreeMachineStateSchema<TContext, TEvent, TTypestate, TSelectors, TActions, TSlots, TInterpreter>, TEvent, TTypestate, any, any, any>;
|
|
202
|
+
|
|
203
|
+
declare type Context = {};
|
|
204
|
+
|
|
205
|
+
declare type EmptyKeys<T> = keyof {
|
|
206
|
+
[K in keyof T as IsEmptyObject<T[K], true> extends true ? K : never]: T[K];
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
declare type EmptyRouteArguments<TParams, TQuery> = IsEmptyObject<TParams, true> extends true ? IsEmptyObject<TQuery, true> extends true ? true : false : false;
|
|
210
|
+
|
|
211
|
+
declare type Events = any;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @public
|
|
215
|
+
*/
|
|
216
|
+
export declare const genericSlotsTestingDummy: any;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @internal
|
|
220
|
+
*/
|
|
221
|
+
export declare type GetSlotNames<TSlots extends readonly Slot[]> = TSlots[number]["name"];
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* @public
|
|
225
|
+
* Extracts the properties defined on the XstateTreeEvents interface and converts them
|
|
226
|
+
* into proper event objects.
|
|
227
|
+
*
|
|
228
|
+
* Properties extending `string` have no payloads, any other type is the payload for the event
|
|
229
|
+
* The property name is extracted as the `type` of the event
|
|
230
|
+
*/
|
|
231
|
+
export declare type GlobalEvents = {
|
|
232
|
+
[I in keyof XstateTreeEvents]: XstateTreeEvents[I] extends string ? {
|
|
233
|
+
type: I;
|
|
234
|
+
} : XstateTreeEvents[I] & {
|
|
235
|
+
type: I;
|
|
236
|
+
};
|
|
237
|
+
}[keyof XstateTreeEvents];
|
|
238
|
+
|
|
239
|
+
declare type InferViewProps<T> = T extends ViewProps<infer TSelectors, infer TActions, never, infer TStates> ? {
|
|
240
|
+
selectors: TSelectors;
|
|
241
|
+
actions: TActions;
|
|
242
|
+
inState: (state: TStates) => (state: TStates) => boolean;
|
|
243
|
+
} : never;
|
|
244
|
+
|
|
245
|
+
declare type IsEmptyObject<Obj, ExcludeOptional extends boolean = false> = [
|
|
246
|
+
keyof (ExcludeOptional extends true ? OmitOptional<Obj> : Obj)
|
|
247
|
+
] extends [never] ? true : false;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @public
|
|
251
|
+
* Wraps an xstate-tree returning Promise (generated by `import()` in an xstate-tree machine responsible for
|
|
252
|
+
* booting up the machine upon resolution
|
|
253
|
+
*
|
|
254
|
+
* @param factory - the factory function that returns the promise that resolves to the machine
|
|
255
|
+
* @param options - configure loading component and context to invoke machine with
|
|
256
|
+
* @returns an xstate-tree machine that wraps the promise, invoking the resulting machine when it resolves
|
|
257
|
+
*/
|
|
258
|
+
export declare function lazy<TMachine extends AnyStateMachine>(factory: () => Promise<TMachine>, { Loader, withContext, }?: Options_2<TMachine["context"]>): StateMachine<Context, any, Events, States, any, any, any>;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @public
|
|
262
|
+
* Renders an anchor tag pointing at the provided Route
|
|
263
|
+
*
|
|
264
|
+
* The query/params/meta props are conditionally required based on the
|
|
265
|
+
* route passed as the To parameter
|
|
266
|
+
*/
|
|
267
|
+
export declare function Link<TRoute extends AnyRoute>({ to, children, testId, ...rest }: LinkProps<TRoute>): JSX.Element;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @public
|
|
271
|
+
*/
|
|
272
|
+
export declare type LinkProps<TRoute extends AnyRoute, TRouteParams = TRoute extends Route<infer TParams, any, any, any> ? TParams : undefined, TRouteQuery = TRoute extends Route<any, infer TQuery, any, any> ? TQuery : undefined, TRouteMeta = TRoute extends Route<any, any, any, infer TMeta> ? TMeta : undefined> = {
|
|
273
|
+
to: TRoute;
|
|
274
|
+
children: React_2.ReactNode;
|
|
275
|
+
testId?: string;
|
|
276
|
+
/**
|
|
277
|
+
* onClick works as normal, but if you return false from it the navigation will not happen
|
|
278
|
+
*/
|
|
279
|
+
onClick?: (e: React_2.MouseEvent<HTMLAnchorElement>) => boolean | void;
|
|
280
|
+
} & RouteArguments<TRouteParams, TRouteQuery, TRouteMeta> & Omit<React_2.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "onClick">;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @public
|
|
284
|
+
*/
|
|
285
|
+
export declare function loggingMetaOptions<TEvents extends EventObject, TContext>(ignoredEvents: TEvents["type"][], ignoreContext?: (keyof TContext)[] | undefined): {
|
|
286
|
+
xstateTree: {
|
|
287
|
+
ignoredEvents: Map<string, boolean>;
|
|
288
|
+
ignoreContext: (keyof TContext)[] | undefined;
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
declare type MakeEmptyObjectPropertiesOptional<T> = Omit<T, EmptyKeys<T>> & Partial<Pick<T, EmptyKeys<T>>>;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @public
|
|
296
|
+
*/
|
|
297
|
+
export declare function matchRoute<TRoutes extends Route<any, any, any, any>[]>(routes: TRoutes, basePath: string, path: string, search: string): Return;
|
|
298
|
+
|
|
299
|
+
declare type Meta<T> = T extends {
|
|
300
|
+
meta: infer TMeta;
|
|
301
|
+
} ? TMeta : undefined;
|
|
302
|
+
|
|
303
|
+
declare type MultiSlot<T> = {
|
|
304
|
+
type: SlotType.MultiSlot;
|
|
305
|
+
name: T;
|
|
306
|
+
getId(id: string): string;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @public
|
|
311
|
+
*/
|
|
312
|
+
export declare function multiSlot<T extends string>(name: T): MultiSlot<T>;
|
|
313
|
+
|
|
314
|
+
declare type OmitOptional<T> = {
|
|
315
|
+
[P in keyof Required<T> as Pick<T, P> extends Required<Pick<T, P>> ? P : never]: T[P];
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @public
|
|
320
|
+
*/
|
|
321
|
+
export declare function onBroadcast(handler: (event: GlobalEvents) => void): () => void;
|
|
322
|
+
|
|
323
|
+
declare type Options<TParamsSchema extends Z.ZodObject<any>, TQuerySchema extends Z.ZodObject<any>, TMetaSchema> = {
|
|
324
|
+
params?: TParamsSchema;
|
|
325
|
+
query?: TQuerySchema;
|
|
326
|
+
meta?: TMetaSchema;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
declare type Options_2<TContext> = {
|
|
330
|
+
/**
|
|
331
|
+
* Displayed while the promise is resolving, defaults to returning null
|
|
332
|
+
*/
|
|
333
|
+
Loader?: React_2.ComponentType;
|
|
334
|
+
/**
|
|
335
|
+
* Allows you to specify an overriden context when the machine is invoked
|
|
336
|
+
* Automatically supplies the machines default context so only requires a partial of overrides
|
|
337
|
+
*/
|
|
338
|
+
withContext?: () => Partial<TContext>;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
declare type Params<T> = T extends {
|
|
342
|
+
params: infer TParams;
|
|
343
|
+
} ? TParams : undefined;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* @public
|
|
347
|
+
*
|
|
348
|
+
* Extracts the event objects for the specified event types from the GlobalEvents union
|
|
349
|
+
*/
|
|
350
|
+
export declare type PickEvent<T extends Extract<GlobalEvents, {
|
|
351
|
+
type: string;
|
|
352
|
+
}>["type"]> = Extract<GlobalEvents, {
|
|
353
|
+
type: T;
|
|
354
|
+
}>;
|
|
355
|
+
|
|
356
|
+
declare type PropsOf<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>> = JSX.LibraryManagedAttributes<C, ComponentPropsWithRef<C>>;
|
|
357
|
+
|
|
358
|
+
declare type Query<T> = T extends {
|
|
359
|
+
query: infer TQuery;
|
|
360
|
+
} ? TQuery : undefined;
|
|
361
|
+
|
|
362
|
+
declare type Return = {
|
|
363
|
+
type: "matched";
|
|
364
|
+
route: AnyRoute;
|
|
365
|
+
event: RoutingEvent<Route<any, any, any, any>>;
|
|
366
|
+
} | {
|
|
367
|
+
type: "no-matches";
|
|
368
|
+
} | {
|
|
369
|
+
type: "match-error";
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @public
|
|
374
|
+
*/
|
|
375
|
+
export declare type Route<TParams, TQuery, TEvent, TMeta> = {
|
|
376
|
+
/**
|
|
377
|
+
* Returns an event object for this route, or undefined if the route does not match
|
|
378
|
+
*
|
|
379
|
+
* The params are automatically extracted out of the url
|
|
380
|
+
* The query data is automatically extracted out the search
|
|
381
|
+
*
|
|
382
|
+
* The params/query objects are validated against the routes Zod schemas
|
|
383
|
+
* if they fail to validate an error is thrown
|
|
384
|
+
*
|
|
385
|
+
* @param url - the pathname portion of a url, this function expects the base path to have been stripped
|
|
386
|
+
* @param search - the query string information (ie "?foo=bar")
|
|
387
|
+
* @returns undefined if the route doesn't match the supplied url, event object otherwise
|
|
388
|
+
* @throws Error if the params or query schemas fail to parse the params/query objects
|
|
389
|
+
*/
|
|
390
|
+
matches: (url: string, search: string) => ({
|
|
391
|
+
type: TEvent;
|
|
392
|
+
originalUrl: string;
|
|
393
|
+
} & RouteArguments<TParams, TQuery, TMeta>) | undefined;
|
|
394
|
+
/**
|
|
395
|
+
* Takes in query/params objects as required by the route and returns a URL for that route
|
|
396
|
+
*
|
|
397
|
+
* The returned URL does not contain the base path
|
|
398
|
+
*
|
|
399
|
+
* Reverse can't do anything with meta arguments, so they are removed from the types
|
|
400
|
+
*/
|
|
401
|
+
reverse: RouteArgumentFunctions<string, TParams, TQuery, undefined>;
|
|
402
|
+
/**
|
|
403
|
+
* Takes in query/params/meta objects as required by the route and updates the
|
|
404
|
+
* url via History.push
|
|
405
|
+
*/
|
|
406
|
+
navigate: RouteArgumentFunctions<void, TParams, TQuery, TMeta>;
|
|
407
|
+
/**
|
|
408
|
+
* Returns an event object for this route based on the supplied params/query/meta
|
|
409
|
+
*
|
|
410
|
+
* Primarily intended for internal use
|
|
411
|
+
*/
|
|
412
|
+
getEvent: RouteArgumentFunctions<{
|
|
413
|
+
type: TEvent;
|
|
414
|
+
} & RouteArguments<TParams, TQuery, TMeta>, TParams, TQuery, TMeta>;
|
|
415
|
+
event: TEvent;
|
|
416
|
+
url?: string;
|
|
417
|
+
history: XstateTreeHistory;
|
|
418
|
+
basePath: string;
|
|
419
|
+
parent?: AnyRoute;
|
|
420
|
+
paramsSchema?: Z.ZodObject<any>;
|
|
421
|
+
querySchema?: Z.ZodObject<any>;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
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: MakeEmptyObjectPropertiesOptional<TArgs>) => TReturn;
|
|
425
|
+
|
|
426
|
+
declare type RouteArguments<TParams, TQuery, TMeta> = TParams extends undefined ? TQuery extends undefined ? TMeta extends undefined ? {} : {
|
|
427
|
+
meta?: TMeta;
|
|
428
|
+
} : TMeta extends undefined ? {
|
|
429
|
+
query: TQuery;
|
|
430
|
+
} : {
|
|
431
|
+
query: TQuery;
|
|
432
|
+
meta?: TMeta;
|
|
433
|
+
} : TQuery extends undefined ? TMeta extends undefined ? {
|
|
434
|
+
params: TParams;
|
|
435
|
+
} : {
|
|
436
|
+
params: TParams;
|
|
437
|
+
meta?: TMeta;
|
|
438
|
+
} : TMeta extends undefined ? {
|
|
439
|
+
params: TParams;
|
|
440
|
+
query: TQuery;
|
|
441
|
+
} : {
|
|
442
|
+
params: TParams;
|
|
443
|
+
query: TQuery;
|
|
444
|
+
meta?: TMeta;
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* @public
|
|
449
|
+
*/
|
|
450
|
+
declare type RouteMeta<T> = T extends Route<any, any, any, infer TMeta> ? TMeta : undefined;
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @public
|
|
454
|
+
*/
|
|
455
|
+
export declare type RouteParams<T> = T extends Route<infer TParams, any, any, any> ? TParams : undefined;
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* @public
|
|
459
|
+
*/
|
|
460
|
+
export declare type Routing404Event = {
|
|
461
|
+
type: "ROUTING_404";
|
|
462
|
+
url: string;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* @public
|
|
467
|
+
* Converts a Route type into the Event that will be broadcast for that route
|
|
468
|
+
*
|
|
469
|
+
* All routes a machine should handle should be added to the machines event union using this type
|
|
470
|
+
*/
|
|
471
|
+
export declare type RoutingEvent<T> = T extends Route<infer TParams, infer TQuery, infer TEvent, infer TMeta> ? {
|
|
472
|
+
type: TEvent;
|
|
473
|
+
originalUrl: string;
|
|
474
|
+
params: TParams;
|
|
475
|
+
query: TQuery;
|
|
476
|
+
meta: TMeta;
|
|
477
|
+
} : never;
|
|
478
|
+
|
|
479
|
+
declare type Selectors<TContext, TEvent extends EventObject, TSelectors, TStates> = (ctx: TContext, canHandleEvent: (e: TEvent) => boolean, inState: (state: TStates) => boolean, __currentState: TStates) => TSelectors;
|
|
480
|
+
|
|
481
|
+
declare type SharedMeta = {
|
|
482
|
+
/**
|
|
483
|
+
* Suppresses this routing change event from being picked up by react-router
|
|
484
|
+
*/
|
|
485
|
+
doNotNotifyReactRouter?: boolean;
|
|
486
|
+
/**
|
|
487
|
+
* True if this was the last routing event in the chain
|
|
488
|
+
*/
|
|
489
|
+
indexEvent?: boolean;
|
|
490
|
+
/**
|
|
491
|
+
* If true, use history.replace instead history.push
|
|
492
|
+
*/
|
|
493
|
+
replace?: boolean;
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
declare type SingleSlot<T> = {
|
|
497
|
+
type: SlotType.SingleSlot;
|
|
498
|
+
name: T;
|
|
499
|
+
getId(): string;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* @public
|
|
504
|
+
*/
|
|
505
|
+
export declare function singleSlot<T extends string>(name: T): SingleSlot<T>;
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* @public
|
|
509
|
+
*/
|
|
510
|
+
export declare type Slot = SingleSlot<any> | MultiSlot<any>;
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* @public
|
|
514
|
+
*/
|
|
515
|
+
export declare function slotTestingDummyFactory(name: string): StateMachine<unknown, XstateTreeMachineStateSchema<unknown, AnyEventObject, {
|
|
516
|
+
value: any;
|
|
517
|
+
context: unknown;
|
|
518
|
+
}, {}, {}, never[], Interpreter<unknown, any, AnyEventObject, {
|
|
519
|
+
value: any;
|
|
520
|
+
context: unknown;
|
|
521
|
+
}, TypegenDisabled>>, AnyEventObject, {
|
|
522
|
+
value: any;
|
|
523
|
+
context: unknown;
|
|
524
|
+
}, any, any, any>;
|
|
525
|
+
|
|
526
|
+
declare enum SlotType {
|
|
527
|
+
SingleSlot = 0,
|
|
528
|
+
MultiSlot = 1
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
declare type States = {
|
|
532
|
+
value: "loading";
|
|
533
|
+
context: Context;
|
|
534
|
+
} | {
|
|
535
|
+
value: "rendering";
|
|
536
|
+
context: Context;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* @public
|
|
541
|
+
*/
|
|
542
|
+
export declare type StyledLink<TStyleProps = {}> = <TRoute extends AnyRoute>(props: LinkProps<TRoute> & TStyleProps) => JSX.Element;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* @public
|
|
546
|
+
*/
|
|
547
|
+
export declare type ViewProps<TSelectors, TActions, TSlots extends readonly Slot[], TState> = {
|
|
548
|
+
slots: Record<GetSlotNames<TSlots>, React_2.ComponentType>;
|
|
549
|
+
actions: TActions;
|
|
550
|
+
selectors: TSelectors;
|
|
551
|
+
inState: (state: TState) => boolean;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* @public
|
|
556
|
+
*/
|
|
557
|
+
export declare type XstateTreeHistory = History_2<{
|
|
558
|
+
meta?: unknown;
|
|
559
|
+
previousUrl?: string;
|
|
560
|
+
}>;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* @public
|
|
564
|
+
*/
|
|
565
|
+
export declare type XStateTreeMachineMeta<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TSelectors = unknown, TActions = unknown, TInterpreter extends Interpreter<TContext, any, TEvent, TTypestate> = Interpreter<TContext, any, TEvent, TTypestate>, TSlots extends readonly Slot[] = Slot[]> = {
|
|
566
|
+
slots: TSlots;
|
|
567
|
+
view: React_2.ComponentType<ViewProps<TSelectors, TActions, TSlots, TTypestate["value"]>>;
|
|
568
|
+
selectors: (ctx: TContext, canHandleEvent: (e: TEvent) => boolean, inState: (state: TTypestate["value"]) => boolean, _state: TTypestate["value"]) => TSelectors;
|
|
569
|
+
actions: (send: TInterpreter["send"], selectors: TSelectors) => TActions;
|
|
570
|
+
xstateTreeMachine?: true;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* @public
|
|
575
|
+
*/
|
|
576
|
+
export declare type XstateTreeMachineStateSchema<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext>, TSelectors = unknown, TActions = unknown, TSlots extends readonly Slot[] = Slot[], TInterpreter extends Interpreter<TContext, any, TEvent, TTypestate> = Interpreter<TContext, any, TEvent, TTypestate>> = {
|
|
577
|
+
meta: XStateTreeMachineMeta<TContext, TEvent, TTypestate, TSelectors, TActions, TInterpreter, TSlots>;
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
export { }
|