@xmachines/play-xstate 2.0.0 → 2.1.0
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 +66 -65
- package/dist/define-player.d.ts +16 -16
- package/dist/define-player.js +16 -16
- package/dist/errors.d.ts +57 -50
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +63 -55
- package/dist/errors.js.map +1 -1
- package/dist/guards/compose.d.ts +53 -50
- package/dist/guards/compose.d.ts.map +1 -1
- package/dist/guards/compose.js +67 -63
- package/dist/guards/compose.js.map +1 -1
- package/dist/guards/helpers.d.ts +22 -22
- package/dist/guards/helpers.js +23 -23
- package/dist/guards/index.d.ts +9 -9
- package/dist/guards/index.js +9 -9
- package/dist/guards/types.d.ts +9 -8
- package/dist/guards/types.d.ts.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/dist/player-actor.d.ts +162 -146
- package/dist/player-actor.d.ts.map +1 -1
- package/dist/player-actor.js +269 -241
- package/dist/player-actor.js.map +1 -1
- package/dist/routing/build-url.d.ts +19 -16
- package/dist/routing/build-url.d.ts.map +1 -1
- package/dist/routing/build-url.js +62 -59
- package/dist/routing/build-url.js.map +1 -1
- package/dist/routing/derive-current-route.d.ts +42 -36
- package/dist/routing/derive-current-route.d.ts.map +1 -1
- package/dist/routing/derive-current-route.js +57 -49
- package/dist/routing/derive-current-route.js.map +1 -1
- package/dist/routing/derive-initial-route.d.ts +23 -20
- package/dist/routing/derive-initial-route.d.ts.map +1 -1
- package/dist/routing/derive-initial-route.js +27 -24
- package/dist/routing/derive-initial-route.js.map +1 -1
- package/dist/routing/derive-route.d.ts +38 -37
- package/dist/routing/derive-route.d.ts.map +1 -1
- package/dist/routing/derive-route.js +45 -42
- package/dist/routing/derive-route.js.map +1 -1
- package/dist/routing/format-play-route-transitions.d.ts +34 -28
- package/dist/routing/format-play-route-transitions.d.ts.map +1 -1
- package/dist/routing/format-play-route-transitions.js +32 -28
- package/dist/routing/format-play-route-transitions.js.map +1 -1
- package/dist/routing/index.d.ts +3 -3
- package/dist/routing/index.js +3 -3
- package/dist/routing/types.d.ts +12 -11
- package/dist/routing/types.d.ts.map +1 -1
- package/dist/types.d.ts +64 -60
- package/dist/types.d.ts.map +1 -1
- package/dist/view/derive-current-view.d.ts +42 -40
- package/dist/view/derive-current-view.d.ts.map +1 -1
- package/dist/view/derive-current-view.js +51 -47
- package/dist/view/derive-current-view.js.map +1 -1
- package/package.json +7 -6
package/dist/errors.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { PlayError } from "@xmachines/play";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* a null
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* Converts a value from a throw into a string, and it trusts that value not:
|
|
4
|
+
* `String()` itself throws for an object with a null prototype, and also for a value
|
|
5
|
+
* whose `toString` or `Symbol.toPrimitive` throws. This function runs inside the
|
|
6
|
+
* error path. A second throw here therefore replaces the real failure of the machine
|
|
7
|
+
* with a `TypeError`, and it passes around `onError`.
|
|
7
8
|
*/
|
|
8
9
|
const safeString = (value) => {
|
|
9
10
|
try {
|
|
@@ -14,23 +15,23 @@ const safeString = (value) => {
|
|
|
14
15
|
return Object.prototype.toString.call(value);
|
|
15
16
|
}
|
|
16
17
|
catch {
|
|
17
|
-
// A revoked Proxy throws
|
|
18
|
+
// A revoked Proxy throws also on an inspection of the brand.
|
|
18
19
|
return "[unrepresentable value]";
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
};
|
|
22
23
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
24
|
+
* `buildRouteUrl()` throws this error when the context of the actor does not hold a
|
|
25
|
+
* necessary route parameter.
|
|
25
26
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
27
|
+
* Read the `param` field and the `template` field to identify the parameter that is
|
|
28
|
+
* absent. Your code therefore parses no `.message` string.
|
|
28
29
|
*
|
|
29
30
|
* **Error code:** `PLAY_XSTATE_ROUTE_PARAM_MISSING`
|
|
30
31
|
*
|
|
31
|
-
* Note: `actor.currentRoute.get()`
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* Note: `actor.currentRoute.get()` shows this error never. The route derivation
|
|
33
|
+
* catches it, and it returns `null` for the temporary case during a transition. The
|
|
34
|
+
* error leaves a direct `buildRouteUrl()` call only.
|
|
34
35
|
*
|
|
35
36
|
* @example
|
|
36
37
|
* ```typescript
|
|
@@ -49,9 +50,9 @@ const safeString = (value) => {
|
|
|
49
50
|
* ```
|
|
50
51
|
*/
|
|
51
52
|
export class MissingRouteParamError extends PlayError {
|
|
52
|
-
/** The name of the route parameter that was
|
|
53
|
+
/** The name of the route parameter that was absent, for example `"userId"`. */
|
|
53
54
|
param;
|
|
54
|
-
/** The route template that
|
|
55
|
+
/** The route template that needs the absent parameter, for example `"/profile/:userId"`. */
|
|
55
56
|
template;
|
|
56
57
|
constructor(param, template) {
|
|
57
58
|
super("buildRouteUrl", "PLAY_XSTATE_ROUTE_PARAM_MISSING", `Route parameter '${param}' is required by template '${template}' but was not found in context.`);
|
|
@@ -61,16 +62,16 @@ export class MissingRouteParamError extends PlayError {
|
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
/**
|
|
64
|
-
* @deprecated
|
|
65
|
-
* field as `query: {}
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* and
|
|
69
|
-
*
|
|
70
|
-
*
|
|
65
|
+
* @deprecated Nothing throws this error now. `buildRouteUrl` reads an absent `query`
|
|
66
|
+
* field as `query: {}`. The generated `play.route` transitions assign `query` on
|
|
67
|
+
* every navigation, and the loss that this error guarded against therefore cannot
|
|
68
|
+
* happen through the route derivation. A machine that handles `play.route` itself
|
|
69
|
+
* and wants the query must assign `event.query` to its context itself. This class
|
|
70
|
+
* stays in the exports, so that an `instanceof` handler that exists now still
|
|
71
|
+
* compiles. Remove such a handler when you have the time.
|
|
71
72
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
73
|
+
* `buildRouteUrl()` threw this error before, when the context had a `params` field,
|
|
74
|
+
* which means a context that knows the routing, but no `query` field.
|
|
74
75
|
*
|
|
75
76
|
* **Error code:** `PLAY_XSTATE_MISSING_QUERY_CONTEXT`
|
|
76
77
|
*/
|
|
@@ -83,23 +84,24 @@ export class MissingQueryContextError extends PlayError {
|
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
/**
|
|
86
|
-
*
|
|
87
|
-
* but
|
|
87
|
+
* `formatPlayRouteTransitions()` throws this error when a state node declares a
|
|
88
|
+
* `meta.route` field but no explicit `id` field.
|
|
88
89
|
*
|
|
89
|
-
* Without `id`, the function
|
|
90
|
-
* state
|
|
91
|
-
* the state
|
|
92
|
-
*
|
|
90
|
+
* Without an `id`, the function can generate no `play.route` guard that targets the
|
|
91
|
+
* state: the guard compares `event.to` with `#<id>`. An absent `id` therefore makes
|
|
92
|
+
* the state unreachable through the routing, and nothing says so. This error shows
|
|
93
|
+
* the fault of the configuration at the moment of the machine definition, and the
|
|
94
|
+
* function skips no state in silence.
|
|
93
95
|
*
|
|
94
96
|
* **Error code:** `PLAY_XSTATE_MISSING_STATE_ID`
|
|
95
97
|
*
|
|
96
98
|
* @example
|
|
97
99
|
* ```typescript
|
|
98
|
-
* // Bad — meta.route without id:
|
|
100
|
+
* // Bad — a meta.route field without an id field:
|
|
99
101
|
* states: {
|
|
100
102
|
* profile: {
|
|
101
103
|
* meta: { route: "/profile/:username" },
|
|
102
|
-
* // ←
|
|
104
|
+
* // ← the id: "profile" field is absent
|
|
103
105
|
* }
|
|
104
106
|
* }
|
|
105
107
|
*
|
|
@@ -113,9 +115,9 @@ export class MissingQueryContextError extends PlayError {
|
|
|
113
115
|
* ```
|
|
114
116
|
*/
|
|
115
117
|
export class MissingStateIdError extends PlayError {
|
|
116
|
-
/** The state key
|
|
118
|
+
/** The state key, which is the position in the tree of the states, without an explicit `id` field. */
|
|
117
119
|
stateKey;
|
|
118
|
-
/** The route template
|
|
120
|
+
/** The route template of the state, for example `"/profile/:username"`. */
|
|
119
121
|
route;
|
|
120
122
|
constructor(stateKey, route) {
|
|
121
123
|
super("formatPlayRouteTransitions", "PLAY_XSTATE_MISSING_STATE_ID", `State "${stateKey}" declares meta.route "${route}" but has no explicit id. ` +
|
|
@@ -126,8 +128,9 @@ export class MissingStateIdError extends PlayError {
|
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
/**
|
|
129
|
-
*
|
|
130
|
-
* valid XState machine object
|
|
131
|
+
* The `PlayerActor` constructor throws this error when the `machine` argument is not
|
|
132
|
+
* a valid XState machine object, which means that it is null, undefined, or not an
|
|
133
|
+
* object.
|
|
131
134
|
*
|
|
132
135
|
* **Error code:** `PLAY_XSTATE_INVALID_MACHINE`
|
|
133
136
|
*/
|
|
@@ -138,13 +141,13 @@ export class InvalidMachineError extends PlayError {
|
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
143
|
/**
|
|
141
|
-
*
|
|
142
|
-
* string
|
|
144
|
+
* `normalizeRoute()` throws this error when the `meta.route` value of a state is not
|
|
145
|
+
* a string and not an object with a `path` property.
|
|
143
146
|
*
|
|
144
147
|
* **Error code:** `PLAY_XSTATE_INVALID_ROUTE_METADATA`
|
|
145
148
|
*/
|
|
146
149
|
export class InvalidRouteMetadataError extends PlayError {
|
|
147
|
-
/** The
|
|
150
|
+
/** The invalid route value, as a string, for the debug work. */
|
|
148
151
|
detail;
|
|
149
152
|
constructor(route, source = "deriveRoute") {
|
|
150
153
|
const detail = JSON.stringify(route);
|
|
@@ -154,13 +157,13 @@ export class InvalidRouteMetadataError extends PlayError {
|
|
|
154
157
|
}
|
|
155
158
|
}
|
|
156
159
|
/**
|
|
157
|
-
*
|
|
158
|
-
* guards
|
|
160
|
+
* `composeGuards()` and `composeGuardsOr()` throw this error for an empty array of
|
|
161
|
+
* guards. A composition of zero guards has no meaning.
|
|
159
162
|
*
|
|
160
163
|
* **Error code:** `PLAY_XSTATE_EMPTY_GUARD_ARRAY`
|
|
161
164
|
*/
|
|
162
165
|
export class EmptyGuardArrayError extends PlayError {
|
|
163
|
-
/** The combinator
|
|
166
|
+
/** The combinator of the call with the empty array: `"and"` or `"or"`. */
|
|
164
167
|
combinator;
|
|
165
168
|
constructor(combinator) {
|
|
166
169
|
super(combinator === "and" ? "composeGuards" : "composeGuardsOr", "PLAY_XSTATE_EMPTY_GUARD_ARRAY", `${combinator === "and" ? "composeGuards" : "composeGuardsOr"} requires at least one guard`);
|
|
@@ -169,13 +172,16 @@ export class EmptyGuardArrayError extends PlayError {
|
|
|
169
172
|
}
|
|
170
173
|
}
|
|
171
174
|
/**
|
|
172
|
-
*
|
|
175
|
+
* `PlayerActor.send()` throws this error when the event argument is not a plain
|
|
176
|
+
* object.
|
|
173
177
|
*
|
|
174
|
-
* `PlayEvent`
|
|
175
|
-
* a string,
|
|
178
|
+
* A `PlayEvent` needs the shape `{ type: string, ...fields }`. A `null` value, an
|
|
179
|
+
* `undefined` value, a string, and every other value that is not an object are an
|
|
180
|
+
* error of the programmer.
|
|
176
181
|
*
|
|
177
|
-
* The
|
|
178
|
-
* TypeScript
|
|
182
|
+
* The error holds the value in question in its readonly class field `detail`, for
|
|
183
|
+
* the debug work. A TypeScript consumer reads `err.detail` directly, with no unsafe
|
|
184
|
+
* cast.
|
|
179
185
|
*
|
|
180
186
|
* **Error code:** `PLAY_XSTATE_INVALID_EVENT`
|
|
181
187
|
*
|
|
@@ -187,14 +193,14 @@ export class EmptyGuardArrayError extends PlayError {
|
|
|
187
193
|
* actor.send(null as any);
|
|
188
194
|
* } catch (err) {
|
|
189
195
|
* if (err instanceof InvalidEventError) {
|
|
190
|
-
* // err.detail
|
|
196
|
+
* // err.detail has the type `unknown`, and it needs no cast
|
|
191
197
|
* console.error("Invalid event passed to actor.send():", err.detail);
|
|
192
198
|
* }
|
|
193
199
|
* }
|
|
194
200
|
* ```
|
|
195
201
|
*/
|
|
196
202
|
export class InvalidEventError extends PlayError {
|
|
197
|
-
/** The
|
|
203
|
+
/** The value in question, from the `send()` call. */
|
|
198
204
|
detail;
|
|
199
205
|
constructor(detail) {
|
|
200
206
|
super("PlayerActor", "PLAY_XSTATE_INVALID_EVENT", "PlayerActor.send() received a non-object event.");
|
|
@@ -203,13 +209,15 @@ export class InvalidEventError extends PlayError {
|
|
|
203
209
|
}
|
|
204
210
|
}
|
|
205
211
|
/**
|
|
206
|
-
*
|
|
207
|
-
* not an `Error
|
|
212
|
+
* The actor gives this error to `PlayerOptions.onError` when it failed with a value
|
|
213
|
+
* that is not an `Error`, for example after a machine action ran
|
|
214
|
+
* `throw { code: 42 }`.
|
|
208
215
|
*
|
|
209
|
-
* An actor error that *is* an `Error` reaches `onError`
|
|
210
|
-
* identity the machine
|
|
211
|
-
* carries a
|
|
212
|
-
* `"[object Object]"`. The
|
|
216
|
+
* An actor error that *is* an `Error` reaches `onError` without a change, and it
|
|
217
|
+
* keeps the identity of the machine. This case alone builds a new error. The new
|
|
218
|
+
* error therefore carries a code that your code can match, and not a
|
|
219
|
+
* `String(value)` message such as `"[object Object]"`. The `cause` field holds the
|
|
220
|
+
* value from the throw.
|
|
213
221
|
*
|
|
214
222
|
* **Error code:** `PLAY_XSTATE_NON_ERROR_THROWN`
|
|
215
223
|
*
|
|
@@ -222,7 +230,7 @@ export class InvalidEventError extends PlayError {
|
|
|
222
230
|
* options: {
|
|
223
231
|
* onError: (actor, err) => {
|
|
224
232
|
* if (err instanceof ActorThrewNonErrorError) {
|
|
225
|
-
* // err.cause is the value the machine
|
|
233
|
+
* // err.cause is the value that the machine threw
|
|
226
234
|
* console.error("Machine threw a non-Error:", err.cause);
|
|
227
235
|
* }
|
|
228
236
|
* },
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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,CAAC,CAAC,CAAC,iBAAiB,8BAA8B,CAC3F,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC9B,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C,qDAAqD;IAC5C,MAAM,CAAU;IAEzB,YAAY,MAAe;QAC1B,KAAK,CACJ,aAAa,EACb,2BAA2B,EAC3B,iDAAiD,CACjD,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,OAAO,uBAAwB,SAAQ,SAAS;IACrD,YAAY,KAAc;QACzB,KAAK,CACJ,aAAa,EACb,8BAA8B,EAC9B,8CAA8C,UAAU,CAAC,KAAK,CAAC,GAAG,EAClE,EAAE,KAAK,EAAE,KAAK,EAAE,CAChB,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACvC,CAAC;CACD"}
|
package/dist/guards/compose.d.ts
CHANGED
|
@@ -2,37 +2,39 @@ import type { GuardPredicate } from "xstate";
|
|
|
2
2
|
import type { Guard, GuardArray } from "./types.js";
|
|
3
3
|
import type { MachineContext, EventObject, ParameterizedObject } from "xstate";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* The narrowest public return type of the guard composition helpers.
|
|
6
6
|
*
|
|
7
7
|
* `GuardPredicate<MachineContext, EventObject, unknown, ParameterizedObject>` is the
|
|
8
|
-
*
|
|
8
|
+
* concrete XState guard type with the widest compatibility that uses no `any`.
|
|
9
9
|
*
|
|
10
10
|
* @public
|
|
11
|
-
* @deprecated The
|
|
12
|
-
*
|
|
11
|
+
* @deprecated The next major version removes the guard utilities. Use the
|
|
12
|
+
* combinator types of XState directly.
|
|
13
13
|
*/
|
|
14
14
|
export type ComposedGuard = GuardPredicate<MachineContext, EventObject, unknown, ParameterizedObject>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* Composes the guards with the AND logic, through the and() helper of XState
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
* the composition
|
|
20
|
-
* type inference and machine
|
|
18
|
+
* The function joins more than one guard predicate with the AND semantics: every
|
|
19
|
+
* guard must pass, and the composition then succeeds. It uses the built-in `and()`
|
|
20
|
+
* helper of XState. The type inference and the serialization of the machine are
|
|
21
|
+
* therefore correct.
|
|
21
22
|
*
|
|
22
|
-
* **Architectural
|
|
23
|
-
*
|
|
24
|
-
*
|
|
23
|
+
* **Architectural context:** the function supports **Actor Authority (INV-01)**,
|
|
24
|
+
* because it composes the guards of a state machine transition declaratively. A
|
|
25
|
+
* guard enforces a rule of the business logic, and that rule decides if a
|
|
26
|
+
* navigation or an action is valid.
|
|
25
27
|
*
|
|
26
|
-
* @typeParam TContext -
|
|
27
|
-
* @typeParam TEvent -
|
|
28
|
+
* @typeParam TContext - The context type of the state machine
|
|
29
|
+
* @typeParam TEvent - The event type
|
|
28
30
|
*
|
|
29
|
-
* @param guards -
|
|
30
|
-
* @returns
|
|
31
|
+
* @param guards - The array of the guard predicates, or of the guard names as strings
|
|
32
|
+
* @returns The and() guard composition of XState
|
|
31
33
|
*
|
|
32
|
-
* @throws {Error}
|
|
34
|
+
* @throws {Error} When the array of the guards is empty
|
|
33
35
|
*
|
|
34
36
|
* @example
|
|
35
|
-
* AND composition with named guards
|
|
37
|
+
* An AND composition with named guards
|
|
36
38
|
* ```typescript
|
|
37
39
|
* import { setup } from "xstate";
|
|
38
40
|
* import { composeGuards } from "@xmachines/play-xstate";
|
|
@@ -54,7 +56,7 @@ export type ComposedGuard = GuardPredicate<MachineContext, EventObject, unknown,
|
|
|
54
56
|
* ```
|
|
55
57
|
*
|
|
56
58
|
* @example
|
|
57
|
-
* AND composition with inline predicates
|
|
59
|
+
* An AND composition with inline predicates
|
|
58
60
|
* ```typescript
|
|
59
61
|
* import { composeGuards } from "@xmachines/play-xstate";
|
|
60
62
|
*
|
|
@@ -64,29 +66,29 @@ export type ComposedGuard = GuardPredicate<MachineContext, EventObject, unknown,
|
|
|
64
66
|
* ])
|
|
65
67
|
* ```
|
|
66
68
|
*
|
|
67
|
-
* @see {@link composeGuardsOr} for OR composition
|
|
68
|
-
* @see {@link negateGuard} for NOT logic
|
|
69
|
-
* @deprecated Use
|
|
70
|
-
* compose with `setup()
|
|
69
|
+
* @see {@link composeGuardsOr} for the OR composition
|
|
70
|
+
* @see {@link negateGuard} for the NOT logic
|
|
71
|
+
* @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
|
|
72
|
+
* does not compose with a guard slot that `setup()` types, and the next major version removes it.
|
|
71
73
|
*/
|
|
72
74
|
export declare const composeGuards: <TContext = any, TEvent = any>(guards: GuardArray<TContext, TEvent>) => ComposedGuard;
|
|
73
75
|
/**
|
|
74
|
-
*
|
|
76
|
+
* Composes the guards with the OR logic, through the or() helper of XState
|
|
75
77
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* type inference.
|
|
78
|
+
* The function joins more than one guard predicate with the OR semantics: one guard
|
|
79
|
+
* must pass at least, and the composition then succeeds. It uses the built-in `or()`
|
|
80
|
+
* helper of XState, and the type inference is therefore correct.
|
|
79
81
|
*
|
|
80
|
-
* @typeParam TContext -
|
|
81
|
-
* @typeParam TEvent -
|
|
82
|
+
* @typeParam TContext - The context type of the state machine
|
|
83
|
+
* @typeParam TEvent - The event type
|
|
82
84
|
*
|
|
83
|
-
* @param guards -
|
|
84
|
-
* @returns
|
|
85
|
+
* @param guards - The array of the guard predicates, or of the guard names
|
|
86
|
+
* @returns The or() guard composition of XState
|
|
85
87
|
*
|
|
86
|
-
* @throws {Error}
|
|
88
|
+
* @throws {Error} When the array of the guards is empty
|
|
87
89
|
*
|
|
88
90
|
* @example
|
|
89
|
-
* OR composition with named guards
|
|
91
|
+
* An OR composition with named guards
|
|
90
92
|
* ```typescript
|
|
91
93
|
* import { setup } from "xstate";
|
|
92
94
|
* import { composeGuardsOr } from "@xmachines/play-xstate";
|
|
@@ -99,7 +101,7 @@ export declare const composeGuards: <TContext = any, TEvent = any>(guards: Guard
|
|
|
99
101
|
* }).createMachine({
|
|
100
102
|
* on: {
|
|
101
103
|
* deleteResource: {
|
|
102
|
-
* //
|
|
104
|
+
* // One guard is sufficient
|
|
103
105
|
* guard: composeGuardsOr(['isOwner', 'isAdmin']),
|
|
104
106
|
* actions: 'delete'
|
|
105
107
|
* }
|
|
@@ -107,26 +109,27 @@ export declare const composeGuards: <TContext = any, TEvent = any>(guards: Guard
|
|
|
107
109
|
* });
|
|
108
110
|
* ```
|
|
109
111
|
*
|
|
110
|
-
* @see {@link composeGuards} for AND composition
|
|
111
|
-
* @see {@link negateGuard} for NOT logic
|
|
112
|
-
* @deprecated Use
|
|
113
|
-
* compose with `setup()
|
|
112
|
+
* @see {@link composeGuards} for the AND composition
|
|
113
|
+
* @see {@link negateGuard} for the NOT logic
|
|
114
|
+
* @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
|
|
115
|
+
* does not compose with a guard slot that `setup()` types, and the next major version removes it.
|
|
114
116
|
*/
|
|
115
117
|
export declare const composeGuardsOr: <TContext = any, TEvent = any>(guards: GuardArray<TContext, TEvent>) => ComposedGuard;
|
|
116
118
|
/**
|
|
117
|
-
*
|
|
119
|
+
* Negates a guard, through the not() helper of XState
|
|
118
120
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
+
* The function inverts the result of a guard: the guard passes, and NOT then fails;
|
|
122
|
+
* the guard fails, and NOT then passes. It uses the built-in `not()` helper of
|
|
123
|
+
* XState, and the serialization is therefore correct.
|
|
121
124
|
*
|
|
122
|
-
* @typeParam TContext -
|
|
123
|
-
* @typeParam TEvent -
|
|
125
|
+
* @typeParam TContext - The context type of the state machine
|
|
126
|
+
* @typeParam TEvent - The event type
|
|
124
127
|
*
|
|
125
|
-
* @param guard -
|
|
126
|
-
* @returns
|
|
128
|
+
* @param guard - The guard predicate to negate, or its name
|
|
129
|
+
* @returns The not() guard negation of XState
|
|
127
130
|
*
|
|
128
131
|
* @example
|
|
129
|
-
* NOT composition with named guard
|
|
132
|
+
* A NOT composition with a named guard
|
|
130
133
|
* ```typescript
|
|
131
134
|
* import { setup } from "xstate";
|
|
132
135
|
* import { negateGuard } from "@xmachines/play-xstate";
|
|
@@ -138,7 +141,7 @@ export declare const composeGuardsOr: <TContext = any, TEvent = any>(guards: Gua
|
|
|
138
141
|
* }).createMachine({
|
|
139
142
|
* on: {
|
|
140
143
|
* accessDashboard: {
|
|
141
|
-
* //
|
|
144
|
+
* // Permit the transition when the user is NOT a guest, which means an authenticated user
|
|
142
145
|
* guard: negateGuard('isGuest'),
|
|
143
146
|
* target: 'dashboard'
|
|
144
147
|
* }
|
|
@@ -146,10 +149,10 @@ export declare const composeGuardsOr: <TContext = any, TEvent = any>(guards: Gua
|
|
|
146
149
|
* });
|
|
147
150
|
* ```
|
|
148
151
|
*
|
|
149
|
-
* @see {@link composeGuards} for AND composition
|
|
150
|
-
* @see {@link composeGuardsOr} for OR composition
|
|
151
|
-
* @deprecated Use
|
|
152
|
-
* compose with `setup()
|
|
152
|
+
* @see {@link composeGuards} for the AND composition
|
|
153
|
+
* @see {@link composeGuardsOr} for the OR composition
|
|
154
|
+
* @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
|
|
155
|
+
* does not compose with a guard slot that `setup()` types, and the next major version removes it.
|
|
153
156
|
*/
|
|
154
157
|
export declare const negateGuard: <TContext = any, TEvent = any>(guard: Guard<TContext, TEvent> | string) => ComposedGuard;
|
|
155
158
|
//# sourceMappingURL=compose.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../src/guards/compose.ts"],"names":[],"mappings":"AACA,OAA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