@xmachines/play-xstate 3.0.0 → 5.0.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.
Files changed (80) hide show
  1. package/README.md +190 -68
  2. package/dist/capabilities.d.ts +93 -0
  3. package/dist/capabilities.d.ts.map +1 -0
  4. package/dist/capabilities.js +4 -0
  5. package/dist/capabilities.js.map +1 -0
  6. package/dist/define-player.d.ts +7 -1
  7. package/dist/define-player.d.ts.map +1 -1
  8. package/dist/define-player.js +9 -60
  9. package/dist/define-player.js.map +1 -1
  10. package/dist/errors.d.ts +64 -23
  11. package/dist/errors.d.ts.map +1 -1
  12. package/dist/errors.js +93 -31
  13. package/dist/errors.js.map +1 -1
  14. package/dist/index.d.ts +4 -6
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +11 -6
  17. package/dist/index.js.map +1 -1
  18. package/dist/player-actor.d.ts +73 -137
  19. package/dist/player-actor.d.ts.map +1 -1
  20. package/dist/player-actor.js +108 -254
  21. package/dist/player-actor.js.map +1 -1
  22. package/dist/routing/build-url.d.ts +8 -1
  23. package/dist/routing/build-url.d.ts.map +1 -1
  24. package/dist/routing/build-url.js +34 -54
  25. package/dist/routing/build-url.js.map +1 -1
  26. package/dist/routing/derive-current-route.d.ts +12 -38
  27. package/dist/routing/derive-current-route.d.ts.map +1 -1
  28. package/dist/routing/derive-current-route.js +24 -82
  29. package/dist/routing/derive-current-route.js.map +1 -1
  30. package/dist/routing/derive-initial-route.d.ts +2 -1
  31. package/dist/routing/derive-initial-route.d.ts.map +1 -1
  32. package/dist/routing/derive-initial-route.js +13 -2
  33. package/dist/routing/derive-initial-route.js.map +1 -1
  34. package/dist/routing/derive-route.d.ts +81 -2
  35. package/dist/routing/derive-route.d.ts.map +1 -1
  36. package/dist/routing/derive-route.js +97 -3
  37. package/dist/routing/derive-route.js.map +1 -1
  38. package/dist/routing/format-play-route-transitions.d.ts +8 -2
  39. package/dist/routing/format-play-route-transitions.d.ts.map +1 -1
  40. package/dist/routing/format-play-route-transitions.js +175 -43
  41. package/dist/routing/format-play-route-transitions.js.map +1 -1
  42. package/dist/routing/index.d.ts +1 -1
  43. package/dist/routing/index.d.ts.map +1 -1
  44. package/dist/routing/types.d.ts +9 -5
  45. package/dist/routing/types.d.ts.map +1 -1
  46. package/dist/state-meta.d.ts +52 -0
  47. package/dist/state-meta.d.ts.map +1 -0
  48. package/dist/state-meta.js +77 -0
  49. package/dist/state-meta.js.map +1 -0
  50. package/dist/types.d.ts +20 -5
  51. package/dist/types.d.ts.map +1 -1
  52. package/dist/view/derive-current-view.d.ts +7 -6
  53. package/dist/view/derive-current-view.d.ts.map +1 -1
  54. package/dist/view/derive-current-view.js +7 -6
  55. package/dist/view/derive-current-view.js.map +1 -1
  56. package/dist/with-routing.d.ts +45 -0
  57. package/dist/with-routing.d.ts.map +1 -0
  58. package/dist/with-routing.js +140 -0
  59. package/dist/with-routing.js.map +1 -0
  60. package/dist/with-view.d.ts +57 -0
  61. package/dist/with-view.d.ts.map +1 -0
  62. package/dist/with-view.js +158 -0
  63. package/dist/with-view.js.map +1 -0
  64. package/package.json +40 -18
  65. package/dist/guards/compose.d.ts +0 -158
  66. package/dist/guards/compose.d.ts.map +0 -1
  67. package/dist/guards/compose.js +0 -188
  68. package/dist/guards/compose.js.map +0 -1
  69. package/dist/guards/helpers.d.ts +0 -62
  70. package/dist/guards/helpers.d.ts.map +0 -1
  71. package/dist/guards/helpers.js +0 -85
  72. package/dist/guards/helpers.js.map +0 -1
  73. package/dist/guards/index.d.ts +0 -20
  74. package/dist/guards/index.d.ts.map +0 -1
  75. package/dist/guards/index.js +0 -18
  76. package/dist/guards/index.js.map +0 -1
  77. package/dist/guards/types.d.ts +0 -22
  78. package/dist/guards/types.d.ts.map +0 -1
  79. package/dist/guards/types.js +0 -2
  80. package/dist/guards/types.js.map +0 -1
@@ -0,0 +1,158 @@
1
+ import { createAtom } from "@xmachines/play-atom";
2
+ import { shallowEqualExcept } from "@xmachines/play";
3
+ import { reuseComposedState } from "@xmachines/play-view";
4
+ import { toError } from "./player-actor.js";
5
+ import { deriveCurrentView } from "./view/derive-current-view.js";
6
+ /**
7
+ * The structural equality of two derived view specs, with a limit on its depth.
8
+ *
9
+ * The function walks exactly the shape that `deriveCurrentView` builds: the spec
10
+ * fields, then `elements`, then the `props` object of each element. It compares
11
+ * each leaf with `Object.is`. It never enters the VALUE of a prop: a new reference
12
+ * therefore emits the view again, also when the contents are equal. This design
13
+ * keeps two things correct: a prop of a container (a Map, a Set, or an instance of a
14
+ * class, which a structural comparison cannot see), and a cyclic value, which gives
15
+ * a structural comparison a recursion without an end.
16
+ */
17
+ const viewSpecsEquivalent = (a, b) => {
18
+ if (a === b)
19
+ return true;
20
+ if (!a || !b)
21
+ return false;
22
+ if (!shallowEqualExcept(a, b, "elements"))
23
+ return false;
24
+ const aElements = a.elements ?? {};
25
+ const bElements = b.elements ?? {};
26
+ // A derived spec spreads the same static meta.view. Therefore the elements
27
+ // usually have the same reference, and the walk over each element is then not
28
+ // necessary.
29
+ if (aElements === bElements)
30
+ return true;
31
+ const elementKeys = Object.keys(aElements);
32
+ if (elementKeys.length !== Object.keys(bElements).length)
33
+ return false;
34
+ for (const key of elementKeys) {
35
+ const aElement = aElements[key]; // nosemgrep: gitlab.eslint.detect-object-injection
36
+ const bElement = bElements[key]; // nosemgrep: gitlab.eslint.detect-object-injection
37
+ if (aElement === bElement)
38
+ continue;
39
+ if (!aElement || !bElement)
40
+ return false;
41
+ if (!shallowEqualExcept(aElement, bElement, "props"))
42
+ return false;
43
+ if (!shallowEqualExcept(aElement.props ?? {}, bElement.props ?? {}))
44
+ return false;
45
+ }
46
+ return true;
47
+ };
48
+ /**
49
+ * Two WeakMaps stood here, and the derivation below needs neither.
50
+ *
51
+ * `lastViewSnapshots` skipped the derivation for a snapshot that the pipeline read
52
+ * already, because XState notifies each observer on EVERY event that it processes and an
53
+ * event that it ignores delivers the identical snapshot again. A computed atom answers
54
+ * that by itself: `state.set(snapshot)` compares with `Object.is`, an identical reference
55
+ * therefore propagates nothing, and this atom stays valid.
56
+ *
57
+ * `lastEmittedViews` held the previous emission for the equality gate, because a read of
58
+ * `currentView` would have registered it as a dependency of the computation around the
59
+ * call and `@xstate/store` exports no `untrack`. The `previous` parameter of a computed
60
+ * atom is that same value, and the engine hands it in rather than tracking a read.
61
+ */
62
+ /**
63
+ * Adds the view capability to an actor class.
64
+ *
65
+ * The ORDER of the composition does not matter. `currentView` and `currentRoute` are two
66
+ * computed atoms over `state`, and neither reads the other, so the engine evaluates both
67
+ * from one write in topological order. An earlier release wrote `currentView` from an
68
+ * `onSnapshot` override, and the order then decided which capability held its new value
69
+ * first.
70
+ *
71
+ * @param Base - The actor class to extend, normally `PlayerActor` or the result of
72
+ * another capability.
73
+ */
74
+ export function withView(Base) {
75
+ class ViewableActor extends Base {
76
+ /**
77
+ * The reactive atom of the current view spec. The atom derives the spec from the
78
+ * `meta.view` metadata of the active state.
79
+ *
80
+ * It emits a **new object reference** on each real change of the view on the screen:
81
+ * the view of a different state, or a change of a param or of the context that changes
82
+ * the resolved spec. A re-entry with `reenter: true` and new params also changes the
83
+ * spec. A snapshot that changes no view on the screen, such as an assign of the
84
+ * context alone, keeps the previous reference. A provider below the atom therefore
85
+ * mounts the UI again not on every event.
86
+ *
87
+ * The `PlaySpec` of the emission carries the context of the machine in its composed
88
+ * `state` field, under the read-only `/context` subtree. A spec therefore reads the
89
+ * context, and also each URL param, through the ordinary state grammar
90
+ * (`{ $state: "/context/params/section" }`). The context-projection module of
91
+ * `@xmachines/play-view` holds the complete contract.
92
+ *
93
+ * The atom returns `null` when the current state has no `meta.view` metadata.
94
+ *
95
+ * Two states can declare two separate `meta.view` literals with an identical
96
+ * structure. A transition between those two states then emits two different
97
+ * references, and a provider mounts the UI again. Move the shared literal into one
98
+ * `typedSpec` constant, and the identity then removes the duplicate.
99
+ *
100
+ * It is a COMPUTED atom over `state`, and not a written one. One write for each
101
+ * transition therefore reaches the whole actor — `state.set(snapshot)` — and the
102
+ * engine evaluates this atom and `currentRoute` in one propagation, in topological
103
+ * order. A second write would flush a second time, and an observer that reads two
104
+ * atoms would see the new state beside the old view in between.
105
+ */
106
+ currentView = createAtom((previous) => {
107
+ // `previous` is the value that this atom holds already. The engine hands it in,
108
+ // so the gate below reads it without a `get()`, and it therefore registers
109
+ // `currentView` as a dependency of nothing.
110
+ const lastEmittedView = previous ?? null;
111
+ try {
112
+ const view = deriveCurrentView(this.state.get());
113
+ // Emit only after a real change of the view on the screen: deriveCurrentView
114
+ // returns a fresh object on each call, and the identity of the reference
115
+ // therefore tells nothing. A new reference for a snapshot that changes the view
116
+ // not, for example a context-only assign, makes a provider below mount the UI
117
+ // again, and that removes the state of the view. A deep equality test is
118
+ // deliberately NOT here: it sees nothing inside a Map or a Set, and it
119
+ // therefore stops a real change, and it recurses without an end on a cyclic
120
+ // prop.
121
+ //
122
+ // A return of `lastEmittedView` compares equal under `Object.is`, so the atom
123
+ // notifies no subscriber and the propagation stops here.
124
+ const nextView = reuseComposedState(lastEmittedView, view);
125
+ return viewSpecsEquivalent(lastEmittedView, nextView)
126
+ ? lastEmittedView
127
+ : nextView;
128
+ }
129
+ catch (error) {
130
+ // The derivation runs inside the `set` of `state`, which runs inside the XState
131
+ // subscription. A throw that leaves this function therefore leaves that `set`
132
+ // and breaks the transition for every other observer of the actor. Report it,
133
+ // and keep the last valid view.
134
+ //
135
+ // The derivation runs inside the `set` of `state`, and the engine TRACKS every
136
+ // atom that this function reads — the handler of the host included. A report
137
+ // from HERE therefore made whatever that handler reads a dependency of this
138
+ // atom, and an unrelated write of it re-derived and reported again: three
139
+ // reports for one failure. The microtask leaves the evaluation, so the handler
140
+ // reads what it likes and this atom depends on none of it.
141
+ //
142
+ // It leaves the `set` as well, which is what the comment below already needed:
143
+ // a throw from the handler lands on the global handler of the runtime, and it
144
+ // breaks the transition for no other observer of the actor.
145
+ const onError = this.hooks.onError;
146
+ if (onError) {
147
+ const reported = toError(error);
148
+ queueMicrotask(() => {
149
+ onError(this, reported);
150
+ });
151
+ }
152
+ return lastEmittedView;
153
+ }
154
+ });
155
+ }
156
+ return ViewableActor;
157
+ }
158
+ //# sourceMappingURL=with-view.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-view.js","sourceRoot":"","sources":["../src/with-view.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAgC,MAAM,sBAAsB,CAAC;AAExF,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAElE;;;;;;;;;;GAUG;AACH,MAAM,mBAAmB,GAAG,CAAC,CAAkB,EAAE,CAAkB,EAAW,EAAE;IAC/E,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACzB,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3B,IAAI,CAAC,kBAAkB,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC;QAAE,OAAO,KAAK,CAAC;IAExD,MAAM,SAAS,GAAG,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC;IACnC,MAAM,SAAS,GAAG,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC;IACnC,2EAA2E;IAC3E,8EAA8E;IAC9E,aAAa;IACb,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACzC,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,IAAI,WAAW,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACvE,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QA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Ae,EAAE,QAAQ,CAAC;oBACpD,CAAC,CAAC,eAAe;oBACjB,CAAC,CAAC,QAAQ,CAAC;YACb,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,gFAAgF;gBAChF,8EAA8E;gBAC9E,8EAA8E;gBAC9E,gCAAgC;gBAChC,EAAE;gBACF,+EAA+E;gBAC/E,6EAA6E;gBAC7E,4EAA4E;gBAC5E,0EAA0E;gBAC1E,+EAA+E;gBAC/E,2DAA2D;gBAC3D,EAAE;gBACF,+EAA+E;gBAC/E,8EAA8E;gBAC9E,4DAA4D;gBAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;gBACnC,IAAI,OAAO,EAAE,CAAC;oBACb,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;oBAChC,cAAc,CAAC,GAAG,EAAE;wBACnB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;oBACzB,CAAC,CAAC,CAAC;gBACJ,CAAC;gBACD,OAAO,eAAe,CAAC;YACxB,CAAC;QACF,CAAC,CACD,CAAC;KACF;IAED,OAAO,aAAa,CAAC;AACtB,CAAC"}
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@xmachines/play-xstate",
3
- "version": "3.0.0",
3
+ "version": "5.0.0",
4
4
  "description": "XState v5 adapter for Play Architecture",
5
5
  "keywords": [
6
6
  "actor-model",
7
+ "atoms",
7
8
  "play-architecture",
8
- "signals",
9
9
  "state-machine",
10
10
  "xstate"
11
11
  ],
12
12
  "license": "MIT",
13
- "author": "",
13
+ "author": "XMachines Contributors",
14
14
  "repository": {
15
15
  "type": "git",
16
16
  "url": "git+https://gitlab.com/xmachin-es/xmachines-js.git",
@@ -30,6 +30,14 @@
30
30
  "types": "./dist/index.d.ts",
31
31
  "default": "./dist/index.js"
32
32
  },
33
+ "./routing": {
34
+ "types": "./dist/with-routing.d.ts",
35
+ "default": "./dist/with-routing.js"
36
+ },
37
+ "./view": {
38
+ "types": "./dist/with-view.d.ts",
39
+ "default": "./dist/with-view.js"
40
+ },
33
41
  "./errors": {
34
42
  "types": "./dist/errors.d.ts",
35
43
  "default": "./dist/errors.js"
@@ -43,33 +51,47 @@
43
51
  "build": "vite build && tsc --build",
44
52
  "clean": "rm -rf dist *.tsbuildinfo coverage node_modules/.svelte2tsx-* node_modules/.vite*",
45
53
  "lint": "oxlint .",
54
+ "lint:security": "node ../../scripts/semgrep-scan.mjs",
46
55
  "format": "oxfmt .",
47
56
  "test": "vitest",
48
57
  "test:coverage": "vitest run --coverage"
49
58
  },
50
59
  "dependencies": {
51
- "@xmachines/play": "3.0.0",
52
- "@xmachines/play-actor": "3.0.0",
53
- "@xmachines/play-signals": "3.0.0",
54
- "dequal": "^2.0.3"
60
+ "@xmachines/play-actor": "5.0.0",
61
+ "@xmachines/play-pattern": "5.0.0"
55
62
  },
56
63
  "devDependencies": {
57
- "@testing-library/jest-dom": "^6.9.1",
58
- "@types/node": "^26.2.0",
64
+ "@testing-library/jest-dom": "^7.0.1",
65
+ "@types/node": "^26.6.2",
59
66
  "@xmachines/json-render-core": "^0.20.0-xm.4",
60
- "@xmachines/play-router": "3.0.0",
61
- "oxfmt": "^0.64.0",
62
- "oxlint": "^1.79.0",
67
+ "@xmachines/play": "5.0.0",
68
+ "@xmachines/play-atom": "5.0.0",
69
+ "@xmachines/play-router": "5.0.0",
70
+ "@xmachines/play-view": "5.0.0",
71
+ "oxfmt": "^0.70.0",
72
+ "oxlint": "^1.85.0",
63
73
  "typescript": "^5.9.3 || ^6.0.3",
64
- "vite": "^8.0.10",
65
- "vitest": "^4.1.11",
66
- "xstate": "^5.31.0"
74
+ "vite": "^8.3.0",
75
+ "vitest": "^5.0.1",
76
+ "xstate": "^5.33.0"
67
77
  },
68
78
  "peerDependencies": {
69
- "xstate": "^5.31.0"
79
+ "@xmachines/play": "5.0.0",
80
+ "@xmachines/play-atom": "5.0.0",
81
+ "@xmachines/play-router": "5.0.0",
82
+ "@xmachines/play-view": "5.0.0",
83
+ "xstate": "^5.33.0"
84
+ },
85
+ "peerDependenciesMeta": {
86
+ "@xmachines/play-view": {
87
+ "optional": true
88
+ },
89
+ "@xmachines/play-router": {
90
+ "optional": true
91
+ }
70
92
  },
71
93
  "engines": {
72
- "node": ">=22.0.0"
94
+ "node": ">=24.0.0"
73
95
  },
74
- "_devDependencies_note": "xstate appears in both peerDependencies and devDependencies intentionally. devDependencies provides workspace resolution for local builds, tests, and typechecking. peerDependencies declares the consumer version constraint. Both are pinned to ^5.30.0 to prevent drift."
96
+ "_devDependencies_note": "xstate appears in both peerDependencies and devDependencies intentionally. devDependencies provides workspace resolution for local builds, tests, and typechecking. peerDependencies declares the consumer version constraint. Both hold the same range, to prevent drift."
75
97
  }
@@ -1,158 +0,0 @@
1
- import type { GuardPredicate } from "xstate";
2
- import type { Guard, GuardArray } from "./types.js";
3
- import type { MachineContext, EventObject, ParameterizedObject } from "xstate";
4
- /**
5
- * The narrowest public return type of the guard composition helpers.
6
- *
7
- * `GuardPredicate<MachineContext, EventObject, unknown, ParameterizedObject>` is the
8
- * concrete XState guard type with the widest compatibility that uses no `any`.
9
- *
10
- * @public
11
- * @deprecated The next major version removes the guard utilities. Use the
12
- * combinator types of XState directly.
13
- */
14
- export type ComposedGuard = GuardPredicate<MachineContext, EventObject, unknown, ParameterizedObject>;
15
- /**
16
- * Composes the guards with the AND logic, through the and() helper of XState
17
- *
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.
22
- *
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.
27
- *
28
- * @typeParam TContext - The context type of the state machine
29
- * @typeParam TEvent - The event type
30
- *
31
- * @param guards - The array of the guard predicates, or of the guard names as strings
32
- * @returns The and() guard composition of XState
33
- *
34
- * @throws {Error} When the array of the guards is empty
35
- *
36
- * @example
37
- * An AND composition with named guards
38
- * ```typescript
39
- * import { setup } from "xstate";
40
- * import { composeGuards } from "@xmachines/play-xstate";
41
- *
42
- * const machine = setup({
43
- * guards: {
44
- * isLoggedIn: ({ context }) => !!context.userId,
45
- * hasPermission: ({ context }) => context.permissions.includes('admin'),
46
- * }
47
- * }).createMachine({
48
- * on: {
49
- * accessAdmin: {
50
- * // Both guards must pass
51
- * guard: composeGuards(['isLoggedIn', 'hasPermission']),
52
- * target: 'adminPanel'
53
- * }
54
- * }
55
- * });
56
- * ```
57
- *
58
- * @example
59
- * An AND composition with inline predicates
60
- * ```typescript
61
- * import { composeGuards } from "@xmachines/play-xstate";
62
- *
63
- * guard: composeGuards([
64
- * ({ context }) => context.age >= 18,
65
- * ({ context }) => context.verified
66
- * ])
67
- * ```
68
- *
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.
73
- */
74
- export declare const composeGuards: <TContext = any, TEvent = any>(guards: GuardArray<TContext, TEvent>) => ComposedGuard;
75
- /**
76
- * Composes the guards with the OR logic, through the or() helper of XState
77
- *
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.
81
- *
82
- * @typeParam TContext - The context type of the state machine
83
- * @typeParam TEvent - The event type
84
- *
85
- * @param guards - The array of the guard predicates, or of the guard names
86
- * @returns The or() guard composition of XState
87
- *
88
- * @throws {Error} When the array of the guards is empty
89
- *
90
- * @example
91
- * An OR composition with named guards
92
- * ```typescript
93
- * import { setup } from "xstate";
94
- * import { composeGuardsOr } from "@xmachines/play-xstate";
95
- *
96
- * const machine = setup({
97
- * guards: {
98
- * isOwner: ({ context }) => context.role === 'owner',
99
- * isAdmin: ({ context }) => context.role === 'admin'
100
- * }
101
- * }).createMachine({
102
- * on: {
103
- * deleteResource: {
104
- * // One guard is sufficient
105
- * guard: composeGuardsOr(['isOwner', 'isAdmin']),
106
- * actions: 'delete'
107
- * }
108
- * }
109
- * });
110
- * ```
111
- *
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.
116
- */
117
- export declare const composeGuardsOr: <TContext = any, TEvent = any>(guards: GuardArray<TContext, TEvent>) => ComposedGuard;
118
- /**
119
- * Negates a guard, through the not() helper of XState
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.
124
- *
125
- * @typeParam TContext - The context type of the state machine
126
- * @typeParam TEvent - The event type
127
- *
128
- * @param guard - The guard predicate to negate, or its name
129
- * @returns The not() guard negation of XState
130
- *
131
- * @example
132
- * A NOT composition with a named guard
133
- * ```typescript
134
- * import { setup } from "xstate";
135
- * import { negateGuard } from "@xmachines/play-xstate";
136
- *
137
- * const machine = setup({
138
- * guards: {
139
- * isGuest: ({ context }) => !context.userId
140
- * }
141
- * }).createMachine({
142
- * on: {
143
- * accessDashboard: {
144
- * // Permit the transition when the user is NOT a guest, which means an authenticated user
145
- * guard: negateGuard('isGuest'),
146
- * target: 'dashboard'
147
- * }
148
- * }
149
- * });
150
- * ```
151
- *
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.
156
- */
157
- export declare const negateGuard: <TContext = any, TEvent = any>(guard: Guard<TContext, TEvent> | string) => ComposedGuard;
158
- //# sourceMappingURL=compose.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../src/guards/compose.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAmB/E;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,GAAG,cAAc,CACzC,cAAc,EACd,WAAW,EACX,OAAO,EACP,mBAAmB,CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,eAAO,MAAM,aAAa,GAAI,QAAQ,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,EACzD,QAAQ,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,KAClC,aAYF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,eAAO,MAAM,eAAe,GAAI,QAAQ,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,EAC3D,QAAQ,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,KAClC,aAYF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,eAAO,MAAM,WAAW,GAAI,QAAQ,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,EACvD,OAAO,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,MAAM,KACrC,aAIF,CAAC"}
@@ -1,188 +0,0 @@
1
- import { and, or, not } from "xstate";
2
- import { EmptyGuardArrayError } from "../errors.js";
3
- /**
4
- * The one unsound boundary of the PRODUCTION code, and it covers the guard types of
5
- * XState only. (The test code uses `unsafeCast` from
6
- * `@xmachines/shared/test-support` instead. The two functions are counterparts, and
7
- * an audit of the casts with grep must cover both names.)
8
- *
9
- * These deprecated helpers close two known gaps in the types of XState 5.28. In the
10
- * first gap, our `Guard<TContext, TEvent>` type does not match `ComposedGuard`
11
- * structurally, because the event generic is different. In the second gap, `and()`
12
- * and `or()` need a `readonly [...tuple]`, and `GuardArray` is a mutable array. The
13
- * values at run time are identical in each case. The parameter has the type
14
- * `unknown`. Therefore one `as` cast is here, and no call site needs a double cast.
15
- * Follow the improvements of the XState types: https://github.com/statelyai/xstate/issues
16
- */
17
- const asXStateGuard = (value) => value;
18
- /**
19
- * Composes the guards with the AND logic, through the and() helper of XState
20
- *
21
- * The function joins more than one guard predicate with the AND semantics: every
22
- * guard must pass, and the composition then succeeds. It uses the built-in `and()`
23
- * helper of XState. The type inference and the serialization of the machine are
24
- * therefore correct.
25
- *
26
- * **Architectural context:** the function supports **Actor Authority (INV-01)**,
27
- * because it composes the guards of a state machine transition declaratively. A
28
- * guard enforces a rule of the business logic, and that rule decides if a
29
- * navigation or an action is valid.
30
- *
31
- * @typeParam TContext - The context type of the state machine
32
- * @typeParam TEvent - The event type
33
- *
34
- * @param guards - The array of the guard predicates, or of the guard names as strings
35
- * @returns The and() guard composition of XState
36
- *
37
- * @throws {Error} When the array of the guards is empty
38
- *
39
- * @example
40
- * An AND composition with named guards
41
- * ```typescript
42
- * import { setup } from "xstate";
43
- * import { composeGuards } from "@xmachines/play-xstate";
44
- *
45
- * const machine = setup({
46
- * guards: {
47
- * isLoggedIn: ({ context }) => !!context.userId,
48
- * hasPermission: ({ context }) => context.permissions.includes('admin'),
49
- * }
50
- * }).createMachine({
51
- * on: {
52
- * accessAdmin: {
53
- * // Both guards must pass
54
- * guard: composeGuards(['isLoggedIn', 'hasPermission']),
55
- * target: 'adminPanel'
56
- * }
57
- * }
58
- * });
59
- * ```
60
- *
61
- * @example
62
- * An AND composition with inline predicates
63
- * ```typescript
64
- * import { composeGuards } from "@xmachines/play-xstate";
65
- *
66
- * guard: composeGuards([
67
- * ({ context }) => context.age >= 18,
68
- * ({ context }) => context.verified
69
- * ])
70
- * ```
71
- *
72
- * @see {@link composeGuardsOr} for the OR composition
73
- * @see {@link negateGuard} for the NOT logic
74
- * @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
75
- * does not compose with a guard slot that `setup()` types, and the next major version removes it.
76
- */
77
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
- export const composeGuards = (guards) => {
79
- if (guards.length === 0) {
80
- throw new EmptyGuardArrayError("and");
81
- }
82
- if (guards.length === 1) {
83
- // One guard passes through. asXStateGuard gives the reason for the boundary.
84
- return asXStateGuard(guards[0]);
85
- }
86
- // Use the built-in and() of XState, for the type inference and the serialization.
87
- return and(asXStateGuard(guards));
88
- };
89
- /**
90
- * Composes the guards with the OR logic, through the or() helper of XState
91
- *
92
- * The function joins more than one guard predicate with the OR semantics: one guard
93
- * must pass at least, and the composition then succeeds. It uses the built-in `or()`
94
- * helper of XState, and the type inference is therefore correct.
95
- *
96
- * @typeParam TContext - The context type of the state machine
97
- * @typeParam TEvent - The event type
98
- *
99
- * @param guards - The array of the guard predicates, or of the guard names
100
- * @returns The or() guard composition of XState
101
- *
102
- * @throws {Error} When the array of the guards is empty
103
- *
104
- * @example
105
- * An OR composition with named guards
106
- * ```typescript
107
- * import { setup } from "xstate";
108
- * import { composeGuardsOr } from "@xmachines/play-xstate";
109
- *
110
- * const machine = setup({
111
- * guards: {
112
- * isOwner: ({ context }) => context.role === 'owner',
113
- * isAdmin: ({ context }) => context.role === 'admin'
114
- * }
115
- * }).createMachine({
116
- * on: {
117
- * deleteResource: {
118
- * // One guard is sufficient
119
- * guard: composeGuardsOr(['isOwner', 'isAdmin']),
120
- * actions: 'delete'
121
- * }
122
- * }
123
- * });
124
- * ```
125
- *
126
- * @see {@link composeGuards} for the AND composition
127
- * @see {@link negateGuard} for the NOT logic
128
- * @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
129
- * does not compose with a guard slot that `setup()` types, and the next major version removes it.
130
- */
131
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
132
- export const composeGuardsOr = (guards) => {
133
- if (guards.length === 0) {
134
- throw new EmptyGuardArrayError("or");
135
- }
136
- if (guards.length === 1) {
137
- // One guard passes through. asXStateGuard gives the reason for the boundary.
138
- return asXStateGuard(guards[0]);
139
- }
140
- // Use the built-in or() of XState, for the type inference and the serialization.
141
- return or(asXStateGuard(guards));
142
- };
143
- /**
144
- * Negates a guard, through the not() helper of XState
145
- *
146
- * The function inverts the result of a guard: the guard passes, and NOT then fails;
147
- * the guard fails, and NOT then passes. It uses the built-in `not()` helper of
148
- * XState, and the serialization is therefore correct.
149
- *
150
- * @typeParam TContext - The context type of the state machine
151
- * @typeParam TEvent - The event type
152
- *
153
- * @param guard - The guard predicate to negate, or its name
154
- * @returns The not() guard negation of XState
155
- *
156
- * @example
157
- * A NOT composition with a named guard
158
- * ```typescript
159
- * import { setup } from "xstate";
160
- * import { negateGuard } from "@xmachines/play-xstate";
161
- *
162
- * const machine = setup({
163
- * guards: {
164
- * isGuest: ({ context }) => !context.userId
165
- * }
166
- * }).createMachine({
167
- * on: {
168
- * accessDashboard: {
169
- * // Permit the transition when the user is NOT a guest, which means an authenticated user
170
- * guard: negateGuard('isGuest'),
171
- * target: 'dashboard'
172
- * }
173
- * }
174
- * });
175
- * ```
176
- *
177
- * @see {@link composeGuards} for the AND composition
178
- * @see {@link composeGuardsOr} for the OR composition
179
- * @deprecated Use the `and()`, `or()`, and `not()` combinators of XState directly. This helper
180
- * does not compose with a guard slot that `setup()` types, and the next major version removes it.
181
- */
182
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
183
- export const negateGuard = (guard) => {
184
- // XState 5.28.0: not() requires one SingleGuardArg shape, and not the Guard
185
- // type of this package. See asXStateGuard.
186
- return not(asXStateGuard(guard));
187
- };
188
- //# sourceMappingURL=compose.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compose.js","sourceRoot":"","sources":["../../src/guards/compose.ts"],"names":[],"mappings":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