@xmachines/play-xstate 2.2.0 → 4.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.
- package/README.md +158 -54
- package/dist/capabilities.d.ts +92 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +4 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/define-player.d.ts +7 -1
- package/dist/define-player.d.ts.map +1 -1
- package/dist/define-player.js +9 -60
- package/dist/define-player.js.map +1 -1
- package/dist/errors.d.ts +29 -26
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +53 -35
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -4
- package/dist/index.js.map +1 -1
- package/dist/player-actor.d.ts +74 -114
- package/dist/player-actor.d.ts.map +1 -1
- package/dist/player-actor.js +94 -223
- package/dist/player-actor.js.map +1 -1
- package/dist/routing/derive-current-route.d.ts +1 -36
- package/dist/routing/derive-current-route.d.ts.map +1 -1
- package/dist/routing/derive-current-route.js +2 -76
- package/dist/routing/derive-current-route.js.map +1 -1
- package/dist/routing/derive-initial-route.d.ts.map +1 -1
- package/dist/routing/derive-initial-route.js +11 -0
- package/dist/routing/derive-initial-route.js.map +1 -1
- package/dist/routing/derive-route.d.ts +81 -2
- package/dist/routing/derive-route.d.ts.map +1 -1
- package/dist/routing/derive-route.js +97 -3
- package/dist/routing/derive-route.js.map +1 -1
- package/dist/routing/format-play-route-transitions.d.ts +8 -2
- package/dist/routing/format-play-route-transitions.d.ts.map +1 -1
- package/dist/routing/format-play-route-transitions.js +170 -43
- package/dist/routing/format-play-route-transitions.js.map +1 -1
- package/dist/routing/index.d.ts +1 -1
- package/dist/routing/index.d.ts.map +1 -1
- package/dist/routing/types.d.ts +9 -5
- package/dist/routing/types.d.ts.map +1 -1
- package/dist/state-meta.d.ts +52 -0
- package/dist/state-meta.d.ts.map +1 -0
- package/dist/state-meta.js +77 -0
- package/dist/state-meta.js.map +1 -0
- package/dist/types.d.ts +18 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/view/derive-current-view.d.ts +1 -1
- package/dist/view/derive-current-view.d.ts.map +1 -1
- package/dist/view/derive-current-view.js +35 -17
- package/dist/view/derive-current-view.js.map +1 -1
- package/dist/with-routing.d.ts +45 -0
- package/dist/with-routing.d.ts.map +1 -0
- package/dist/with-routing.js +78 -0
- package/dist/with-routing.js.map +1 -0
- package/dist/with-view.d.ts +42 -0
- package/dist/with-view.d.ts.map +1 -0
- package/dist/with-view.js +150 -0
- package/dist/with-view.js.map +1 -0
- package/package.json +39 -18
- package/dist/guards/compose.d.ts +0 -158
- package/dist/guards/compose.d.ts.map +0 -1
- package/dist/guards/compose.js +0 -188
- package/dist/guards/compose.js.map +0 -1
- package/dist/guards/helpers.d.ts +0 -62
- package/dist/guards/helpers.d.ts.map +0 -1
- package/dist/guards/helpers.js +0 -85
- package/dist/guards/helpers.js.map +0 -1
- package/dist/guards/index.d.ts +0 -20
- package/dist/guards/index.d.ts.map +0 -1
- package/dist/guards/index.js +0 -18
- package/dist/guards/index.js.map +0 -1
- package/dist/guards/types.d.ts +0 -22
- package/dist/guards/types.d.ts.map +0 -1
- package/dist/guards/types.js +0 -2
- package/dist/guards/types.js.map +0 -1
package/dist/errors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PlayError } from "@xmachines/play";
|
|
1
|
+
import { PlayError } from "@xmachines/play/errors";
|
|
2
2
|
/**
|
|
3
3
|
* Converts a value from a throw into a string, and it trusts that value not:
|
|
4
4
|
* `String()` itself throws for an object with a null prototype, and also for a value
|
|
@@ -35,7 +35,7 @@ const safeString = (value) => {
|
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* ```typescript
|
|
38
|
-
* import { buildRouteUrl } from "@xmachines/play-xstate";
|
|
38
|
+
* import { buildRouteUrl } from "@xmachines/play-xstate/routing";
|
|
39
39
|
* import { MissingRouteParamError } from "@xmachines/play-xstate/errors";
|
|
40
40
|
*
|
|
41
41
|
* try {
|
|
@@ -61,28 +61,6 @@ export class MissingRouteParamError extends PlayError {
|
|
|
61
61
|
this.template = template;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
/**
|
|
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.
|
|
72
|
-
*
|
|
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.
|
|
75
|
-
*
|
|
76
|
-
* **Error code:** `PLAY_XSTATE_MISSING_QUERY_CONTEXT`
|
|
77
|
-
*/
|
|
78
|
-
export class MissingQueryContextError extends PlayError {
|
|
79
|
-
constructor() {
|
|
80
|
-
super("buildRouteUrl", "PLAY_XSTATE_MISSING_QUERY_CONTEXT", "buildRouteUrl() received a context without a 'query' field. " +
|
|
81
|
-
"Declare 'query: Record<string, string>' in the machine context type and " +
|
|
82
|
-
"initialise it to {} so query parameters from play.route events are not silently dropped.");
|
|
83
|
-
this.name = "MissingQueryContextError";
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
64
|
/**
|
|
87
65
|
* `formatPlayRouteTransitions()` throws this error when a state node declares a
|
|
88
66
|
* `meta.route` field but no explicit `id` field.
|
|
@@ -140,6 +118,25 @@ export class InvalidMachineError extends PlayError {
|
|
|
140
118
|
this.name = "InvalidMachineError";
|
|
141
119
|
}
|
|
142
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Describes an unknown value for the message of an error, and throws nothing itself.
|
|
123
|
+
*
|
|
124
|
+
* `JSON.stringify` alone is not enough for a value that arrives from the config of a
|
|
125
|
+
* user. It returns the VALUE `undefined` for a function, for a symbol, and for
|
|
126
|
+
* `undefined`, which puts the text "undefined" in a field that is declared `string`.
|
|
127
|
+
* It also THROWS a `TypeError` for a circular object, and an error constructor that
|
|
128
|
+
* throws hides the error that it was built to report.
|
|
129
|
+
*
|
|
130
|
+
* The fallback names the type instead, for example `[function]`.
|
|
131
|
+
*/
|
|
132
|
+
function describeValue(value) {
|
|
133
|
+
try {
|
|
134
|
+
return JSON.stringify(value) ?? `[${typeof value}]`;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return `[${typeof value}]`;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
143
140
|
/**
|
|
144
141
|
* `normalizeRoute()` throws this error when the `meta.route` value of a state is not
|
|
145
142
|
* a string and not an object with a `path` property.
|
|
@@ -150,25 +147,46 @@ export class InvalidRouteMetadataError extends PlayError {
|
|
|
150
147
|
/** The invalid route value, as a string, for the debug work. */
|
|
151
148
|
detail;
|
|
152
149
|
constructor(route, source = "deriveRoute") {
|
|
153
|
-
const detail =
|
|
150
|
+
const detail = describeValue(route);
|
|
154
151
|
super(source, "PLAY_XSTATE_INVALID_ROUTE_METADATA", `Invalid route metadata: ${detail}. Expected string or { path: string }`);
|
|
155
152
|
this.name = "InvalidRouteMetadataError";
|
|
156
153
|
this.detail = detail;
|
|
157
154
|
}
|
|
158
155
|
}
|
|
159
156
|
/**
|
|
160
|
-
* `
|
|
161
|
-
*
|
|
157
|
+
* `formatPlayRouteTransitions()` throws this error for a `meta.route` object whose
|
|
158
|
+
* `handler` field holds a value that the routing layer does not know.
|
|
159
|
+
*
|
|
160
|
+
* The field decides WHERE the generated `play.route` transition sits, and each value
|
|
161
|
+
* produces a different reachability. `"local"` in particular makes a state reachable
|
|
162
|
+
* only while its parent is active. A typo such as `"locale"` would therefore fall back
|
|
163
|
+
* to a placement that the author did not ask for, and the route would behave correctly
|
|
164
|
+
* in the test that the author wrote and wrongly everywhere else. The error refuses the
|
|
165
|
+
* value instead.
|
|
162
166
|
*
|
|
163
|
-
* **Error code:** `
|
|
167
|
+
* **Error code:** `PLAY_XSTATE_INVALID_ROUTE_HANDLER`
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* import { InvalidRouteHandlerError } from "@xmachines/play-xstate/errors";
|
|
172
|
+
*
|
|
173
|
+
* try {
|
|
174
|
+
* formatPlayRouteTransitions(config);
|
|
175
|
+
* } catch (error) {
|
|
176
|
+
* if (error instanceof InvalidRouteHandlerError) {
|
|
177
|
+
* console.error(error.handler); // the value that the config declared
|
|
178
|
+
* }
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
164
181
|
*/
|
|
165
|
-
export class
|
|
166
|
-
/** The
|
|
167
|
-
|
|
168
|
-
constructor(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.
|
|
182
|
+
export class InvalidRouteHandlerError extends PlayError {
|
|
183
|
+
/** The value that the config declared, as a string, for the debug work. */
|
|
184
|
+
handler;
|
|
185
|
+
constructor(handler, source = "formatPlayRouteTransitions") {
|
|
186
|
+
const detail = describeValue(handler);
|
|
187
|
+
super(source, "PLAY_XSTATE_INVALID_ROUTE_HANDLER", `Invalid route handler: ${detail}. Expected "root", "local", or "both"`);
|
|
188
|
+
this.name = "InvalidRouteHandlerError";
|
|
189
|
+
this.handler = detail;
|
|
172
190
|
}
|
|
173
191
|
}
|
|
174
192
|
/**
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;;;;;GAMG;AACH,MAA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,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/index.d.ts
CHANGED
|
@@ -13,9 +13,8 @@
|
|
|
13
13
|
*/
|
|
14
14
|
export { definePlayer } from "./define-player.js";
|
|
15
15
|
export { PlayerActor } from "./player-actor.js";
|
|
16
|
+
export { compose } from "./capabilities.js";
|
|
17
|
+
export type { Capability, PlayerConstructor } from "./capabilities.js";
|
|
16
18
|
export type { PlayerConfig, PlayerOptions, PlayerFactory, PlayerFactoryResumeOptions, } from "./types.js";
|
|
17
|
-
export {
|
|
18
|
-
export type { Guard, GuardArray, ComposedGuard } from "./guards/index.js";
|
|
19
|
-
export { deriveRoute, isAbsoluteRoute, buildRouteUrl, formatPlayRouteTransitions, } from "./routing/index.js";
|
|
20
|
-
export type { RouteMachineConfig, RouteStateNode, RouteContext, RouteObject, RouteMetadata, } from "./routing/index.js";
|
|
19
|
+
export { DISPOSE } from "@xmachines/play";
|
|
21
20
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACvE,YAAY,EACX,YAAY,EACZ,aAAa,EACb,aAAa,EACb,0BAA0B,GAC1B,MAAM,YAAY,CAAC;AAWpB,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -13,8 +13,18 @@
|
|
|
13
13
|
*/
|
|
14
14
|
export { definePlayer } from "./define-player.js";
|
|
15
15
|
export { PlayerActor } from "./player-actor.js";
|
|
16
|
-
// The
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
export {
|
|
16
|
+
// The composition of the capabilities. Each capability itself lives behind its own entry
|
|
17
|
+
// point — `@xmachines/play-xstate/routing` and `@xmachines/play-xstate/view` — so this
|
|
18
|
+
// module imports neither, and an application loads only what it composes.
|
|
19
|
+
export { compose } from "./capabilities.js";
|
|
20
|
+
// The routing utilities live behind `@xmachines/play-xstate/routing`, with the mixin
|
|
21
|
+
// that needs them: `deriveRoute`, `isAbsoluteRoute`, `buildRouteUrl`,
|
|
22
|
+
// `formatPlayRouteTransitions`, and the route types. Each one names
|
|
23
|
+
// `@xmachines/play-router` in its `.d.ts`, and that package is an OPTIONAL peer. This
|
|
24
|
+
// entry therefore names no capability package, exactly as the view half does.
|
|
25
|
+
// The release protocol of @xmachines/play. This package's own published `.d.ts` names
|
|
26
|
+
// exactly these, so a consumer reads them from here and needs no second manifest
|
|
27
|
+
// entry. It names `asCleanup` nowhere: a consumer of this package RECEIVES a
|
|
28
|
+
// release, and builds one only with @xmachines/play itself.
|
|
29
|
+
export { DISPOSE } from "@xmachines/play";
|
|
20
30
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,yFAAyF;AACzF,uFAAuF;AACvF,0EAA0E;AAC1E,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAS5C,qFAAqF;AACrF,sEAAsE;AACtE,oEAAoE;AACpE,sFAAsF;AACtF,8EAA8E;AAC9E,sFAAsF;AACtF,iFAAiF;AACjF,6EAA6E;AAC7E,4DAA4D;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/player-actor.d.ts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
import { Actor, type AnyStateMachine, type
|
|
2
|
-
import
|
|
1
|
+
import { Actor, type AnyStateMachine, type AnyMachineSnapshot, type InputFrom, type ActorOptions, type Observer, type Snapshot, type SnapshotFrom, type Subscription, type EventFromLogic } from "xstate";
|
|
2
|
+
import type { PlayActor } from "@xmachines/play-actor";
|
|
3
|
+
import { DISPOSE } from "@xmachines/play";
|
|
3
4
|
import { Signal } from "@xmachines/play-signals";
|
|
4
5
|
import type { PlayerOptions } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Normalizes a failure of the actor for `onError`.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*
|
|
11
|
+
* The function gives an `Error` to the handler without a change. The error of the
|
|
12
|
+
* machine therefore keeps its identity: an `instanceof` test of a consumer still
|
|
13
|
+
* works, and the path without an `onError` throws that same object again. Every
|
|
14
|
+
* other value is ours to build, and it becomes a `PlayError` with a code. That
|
|
15
|
+
* error carries the value from the throw as its `cause`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const toError: (value: unknown) => Error;
|
|
5
18
|
/**
|
|
6
19
|
* The concrete XState actor. It implements the signal protocol of the Play Architecture
|
|
7
20
|
*
|
|
8
|
-
* The class extends
|
|
9
|
-
*
|
|
21
|
+
* The class extends the `Actor` class of XState directly, and it implements
|
|
22
|
+
* {@link @xmachines/play-actor!PlayActor}. It gives you the XState v5 integration, and it keeps the
|
|
10
23
|
* compatibility with the ecosystem, such as the XState inspection and the devtools.
|
|
11
24
|
* The constructor of the base class receives the machine. Therefore a `PlayerActor`
|
|
12
25
|
* **is** the XState actor, and it is no wrapper around one: every member of the
|
|
@@ -14,8 +27,8 @@ import type { PlayerOptions } from "./types.js";
|
|
|
14
27
|
* reactive state on the TC39 Signals for the observation by the infrastructure.
|
|
15
28
|
*
|
|
16
29
|
* **Capabilities:** the class implements both the
|
|
17
|
-
* {@link @xmachines/play-
|
|
18
|
-
* {@link @xmachines/play-
|
|
30
|
+
* {@link @xmachines/play-router!index.Routable} interface and the
|
|
31
|
+
* {@link @xmachines/play-view!index.Viewable} interface. It therefore supports the
|
|
19
32
|
* routing and the view rendering.
|
|
20
33
|
*
|
|
21
34
|
* **Architectural context:** the class implements **Actor Authority (INV-01)**,
|
|
@@ -30,7 +43,7 @@ import type { PlayerOptions } from "./types.js";
|
|
|
30
43
|
* The creation of an actor, and its lifecycle
|
|
31
44
|
* ```typescript
|
|
32
45
|
* import { setup } from "xstate";
|
|
33
|
-
* import { definePlayer } from "@xmachines/play-xstate";
|
|
46
|
+
* import { definePlayer, compose, PlayerActor } from "@xmachines/play-xstate";
|
|
34
47
|
*
|
|
35
48
|
* const machine = setup({}).createMachine({
|
|
36
49
|
* initial: 'idle',
|
|
@@ -76,9 +89,9 @@ import type { PlayerOptions } from "./types.js";
|
|
|
76
89
|
*
|
|
77
90
|
* @see [Play RFC](../../docs/rfc/play.md)
|
|
78
91
|
* @see {@link definePlayer} for the creation through a factory
|
|
79
|
-
* @see {@link @xmachines/play-actor!
|
|
80
|
-
* @see {@link @xmachines/play-
|
|
81
|
-
* @see {@link @xmachines/play-
|
|
92
|
+
* @see {@link @xmachines/play-actor!PlayActor} for the signal protocol
|
|
93
|
+
* @see {@link @xmachines/play-router!index.Routable} for the routing capability
|
|
94
|
+
* @see {@link @xmachines/play-view!index.Viewable} for the view rendering capability
|
|
82
95
|
*
|
|
83
96
|
* @remarks
|
|
84
97
|
* **The routing:** this actor supports the `route: {}` config pattern of XState and
|
|
@@ -91,8 +104,21 @@ import type { PlayerOptions } from "./types.js";
|
|
|
91
104
|
* PlayRenderer is therefore correct. The class derives each view at the entry of a
|
|
92
105
|
* state and keeps it, and it computes no view on a read.
|
|
93
106
|
*/
|
|
94
|
-
export declare class PlayerActor<TMachine extends AnyStateMachine> extends
|
|
107
|
+
export declare class PlayerActor<TMachine extends AnyStateMachine> extends Actor<TMachine> implements PlayActor<ReturnType<TMachine["transition"]>, EventFromLogic<TMachine>> {
|
|
95
108
|
private playerOptions?;
|
|
109
|
+
/**
|
|
110
|
+
* The three inputs of the constructor that a CAPABILITY needs.
|
|
111
|
+
*
|
|
112
|
+
* A mixin over a generic base cannot declare a constructor: TypeScript then demands the
|
|
113
|
+
* signature `...args: any[]` (TS2545), which this workspace does not write. A field
|
|
114
|
+
* initializer of a subclass runs after `super()` instead, and it reads these. The
|
|
115
|
+
* routing capability derives `initialRoute` from all three.
|
|
116
|
+
*/
|
|
117
|
+
protected readonly playerMachine: TMachine;
|
|
118
|
+
/** @see {@link playerMachine} */
|
|
119
|
+
protected readonly playerInput: InputFrom<TMachine> | undefined;
|
|
120
|
+
/** @see {@link playerMachine} */
|
|
121
|
+
protected readonly playerRestoredSnapshot: ActorOptions<TMachine>["snapshot"] | undefined;
|
|
96
122
|
/**
|
|
97
123
|
* Tells you if this constructor returned already.
|
|
98
124
|
*
|
|
@@ -119,7 +145,7 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
119
145
|
* one, therefore resets each value of the window to `undefined`. A `declare`
|
|
120
146
|
* modifier emits nothing, and those values survive.
|
|
121
147
|
*/
|
|
122
|
-
|
|
148
|
+
protected get hooks(): PlayerOptions<TMachine>;
|
|
123
149
|
/**
|
|
124
150
|
* The record of the real lifecycle transitions, so that onStart and onStop each
|
|
125
151
|
* fire one time. The status of XState is internal, and a machine snapshot reads
|
|
@@ -136,15 +162,6 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
136
162
|
* times.
|
|
137
163
|
*/
|
|
138
164
|
private nextOnlySubscriptions?;
|
|
139
|
-
/**
|
|
140
|
-
* The last snapshot of the view pipeline. XState notifies each observer on EVERY
|
|
141
|
-
* event that it processes, and an event that it ignores delivers the identical
|
|
142
|
-
* snapshot again. deriveCurrentView is pure in the snapshot. Therefore an identical
|
|
143
|
-
* reference can change no result. The first value is undefined, and never a
|
|
144
|
-
* snapshot: the snapshot of the construction has the same reference as the snapshot
|
|
145
|
-
* that start() replays, and that value therefore stops the first view.
|
|
146
|
-
*/
|
|
147
|
-
private lastViewSnapshot;
|
|
148
165
|
state: Signal.State<ReturnType<TMachine["transition"]>>;
|
|
149
166
|
/**
|
|
150
167
|
* Tells you if the current state of the actor accepts the given event.
|
|
@@ -158,77 +175,6 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
158
175
|
* ```
|
|
159
176
|
*/
|
|
160
177
|
can(event: EventFromLogic<TMachine>): boolean;
|
|
161
|
-
/**
|
|
162
|
-
* A TC39 `Signal.Computed`. It derives the current URL path from the `meta.route`
|
|
163
|
-
* template of the active machine state and from the context of the actor.
|
|
164
|
-
*
|
|
165
|
-
* It returns `null` when the current state has no `meta.route` field, and also when
|
|
166
|
-
* it cannot resolve the complete route template. A necessary `:param` that the
|
|
167
|
-
* context does not hold is caught inside the signal, and a
|
|
168
|
-
* `MissingRouteParamError` therefore never leaves `get()`: that condition is
|
|
169
|
-
* temporary during a transition, and the signal computes the value again on the next
|
|
170
|
-
* snapshot.
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* ```typescript
|
|
174
|
-
* // It returns "/profile/alice" when context.params.userId === "alice",
|
|
175
|
-
* // and null while the param is still absent.
|
|
176
|
-
* const route = actor.currentRoute.get();
|
|
177
|
-
* ```
|
|
178
|
-
*/
|
|
179
|
-
currentRoute: Signal.Computed<string | null>;
|
|
180
|
-
/**
|
|
181
|
-
* The route of the initial state of the machine. The constructor fixes it, and it
|
|
182
|
-
* never changes, also when the code restores the actor from a snapshot.
|
|
183
|
-
*
|
|
184
|
-
* A router bridge compares it with the browser URL, and it therefore separates a
|
|
185
|
-
* deep link (a URL that is not the initial one → the router wins) from a restore
|
|
186
|
-
* (the initial URL, and the actor at a different route from the restore → the actor
|
|
187
|
-
* wins).
|
|
188
|
-
*
|
|
189
|
-
* `deriveInitialRoute` derives the value statically from the machine definition,
|
|
190
|
-
* with the pure `initialTransition` helper of XState: the chain of the initial states
|
|
191
|
-
* and their `meta.route` templates are fixed at the moment of the machine
|
|
192
|
-
* definition, and the substitution of a `:param` uses the real initial context of
|
|
193
|
-
* the machine for the `input` of this actor. The code makes no second actor, and a
|
|
194
|
-
* snapshot of a restore changes the value never: it is always the **default**
|
|
195
|
-
* initial route of the machine.
|
|
196
|
-
*/
|
|
197
|
-
readonly initialRoute: string | null;
|
|
198
|
-
/**
|
|
199
|
-
* The reactive signal of the current view spec. The signal derives the spec from
|
|
200
|
-
* the `meta.view` metadata of the active state.
|
|
201
|
-
*
|
|
202
|
-
* It emits a **new object reference** on each real change of the view on the
|
|
203
|
-
* screen: the view of a different state, or a change of a param or of the context
|
|
204
|
-
* that changes the resolved spec. A re-entry with `reenter: true` and new params
|
|
205
|
-
* also changes the spec. A snapshot that changes no view on the screen, such as an
|
|
206
|
-
* assign of the context alone, keeps the previous reference. A provider below the
|
|
207
|
-
* signal therefore mounts the UI again not on every event.
|
|
208
|
-
*
|
|
209
|
-
* The `PlaySpec` of the emission carries the context of the machine in its composed
|
|
210
|
-
* `state` field, under the read-only `/context` subtree. A spec therefore reads the
|
|
211
|
-
* context, and also each URL param, through the ordinary state grammar
|
|
212
|
-
* (`{ $state: "/context/params/section" }`). The context-projection module of
|
|
213
|
-
* `@xmachines/play-actor` holds the complete contract.
|
|
214
|
-
*
|
|
215
|
-
* The signal returns `null` when the current state has no `meta.view` metadata.
|
|
216
|
-
*
|
|
217
|
-
* Two states can declare two separate `meta.view` literals with an identical
|
|
218
|
-
* structure. A transition between those two states then emits two different
|
|
219
|
-
* references, and a provider mounts the UI again. Move the shared literal into one
|
|
220
|
-
* `typedSpec` constant, and the identity then removes the duplicate.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* const view = actor.currentView.get();
|
|
225
|
-
* if (view) {
|
|
226
|
-
* console.log(view.root); // for example "root"
|
|
227
|
-
* console.log(view.elements); // the Spec elements of @xmachines/json-render-core
|
|
228
|
-
* }
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
readonly currentView: Signal.State<PlaySpec | null>;
|
|
232
178
|
constructor(machine: TMachine, options: PlayerOptions<TMachine>, input?: InputFrom<TMachine>, restoredSnapshot?: ActorOptions<TMachine>["snapshot"]);
|
|
233
179
|
/**
|
|
234
180
|
* Starts the actor.
|
|
@@ -274,10 +220,6 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
274
220
|
* ```
|
|
275
221
|
*/
|
|
276
222
|
send(event: EventFromLogic<TMachine>): void;
|
|
277
|
-
/**
|
|
278
|
-
* Returns the current snapshot
|
|
279
|
-
*/
|
|
280
|
-
getSnapshot(): ReturnType<Actor<TMachine>["getSnapshot"]>;
|
|
281
223
|
/**
|
|
282
224
|
* Subscribes to the snapshot updates of this actor.
|
|
283
225
|
*
|
|
@@ -298,16 +240,6 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
298
240
|
* @returns The subscription, with an `unsubscribe()` method.
|
|
299
241
|
*/
|
|
300
242
|
subscribe(nextListener?: (snapshot: SnapshotFrom<TMachine>) => void, errorListener?: (error: unknown) => void, completeListener?: () => void): Subscription;
|
|
301
|
-
/**
|
|
302
|
-
* Listens for the events that this actor emits with the `emit` action.
|
|
303
|
-
*
|
|
304
|
-
* @param type - The type of the emitted event to listen for, or `"*"` for every event.
|
|
305
|
-
* @param handler - The actor calls it with each emitted event that matches.
|
|
306
|
-
* @returns The subscription, with an `unsubscribe()` method.
|
|
307
|
-
*/
|
|
308
|
-
on<TType extends EmittedFrom<TMachine>["type"] | "*">(type: TType, handler: (emitted: EmittedFrom<TMachine> & (TType extends "*" ? unknown : {
|
|
309
|
-
type: TType;
|
|
310
|
-
})) => void): Subscription;
|
|
311
243
|
/**
|
|
312
244
|
* Returns the persisted snapshot of this actor.
|
|
313
245
|
*
|
|
@@ -316,18 +248,46 @@ export declare class PlayerActor<TMachine extends AnyStateMachine> extends Abstr
|
|
|
316
248
|
*/
|
|
317
249
|
getPersistedSnapshot(options?: unknown): Snapshot<unknown>;
|
|
318
250
|
/**
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
251
|
+
* The point where a capability derives its own signals from a new snapshot.
|
|
252
|
+
*
|
|
253
|
+
* The base derives nothing: `PlayActor` asks for `state` and `send`, and this class
|
|
254
|
+
* gives exactly those. A capability is a mixin that overrides this method, calls
|
|
255
|
+
* `super.onSnapshot(snapshot)` FIRST, and then writes its own signal.
|
|
256
|
+
*
|
|
257
|
+
* That one rule fixes the order, and the order is load-bearing. The view of a state
|
|
258
|
+
* reads the route and the state that the same transition produced, and a router bridge
|
|
259
|
+
* must see a guard redirect synchronously. `compose(PlayerActor, withRouting, withView)`
|
|
260
|
+
* therefore derives the route before the view, because `withView` wraps the class that
|
|
261
|
+
* `withRouting` returned.
|
|
262
|
+
*
|
|
263
|
+
* The method runs after `state` holds the new snapshot and before the `onStateChange`
|
|
264
|
+
* hook of the options.
|
|
322
265
|
*
|
|
323
|
-
* @param snapshot - The
|
|
266
|
+
* @param snapshot - The stable snapshot of this transition.
|
|
324
267
|
*/
|
|
325
|
-
|
|
268
|
+
protected onSnapshot(snapshot: AnyMachineSnapshot): void;
|
|
326
269
|
/**
|
|
327
|
-
*
|
|
270
|
+
* Stops the actor when the scope of a `using` declaration ends.
|
|
271
|
+
*
|
|
272
|
+
* The actor is a `Disposable`, so a scope that owns one writes no teardown:
|
|
273
|
+
*
|
|
274
|
+
* ```ts
|
|
275
|
+
* {
|
|
276
|
+
* using actor = definePlayer({ machine })();
|
|
277
|
+
* actor.start();
|
|
278
|
+
* actor.send({ type: "go" });
|
|
279
|
+
* // stop() runs here, and it runs even when send() throws
|
|
280
|
+
* }
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* The release is {@link stop}, which is synchronous and idempotent. `Disposable` and
|
|
284
|
+
* not `AsyncDisposable`: a `Disposable` serves `using` AND `await using`, while an
|
|
285
|
+
* `AsyncDisposable` raises a TypeError under a plain `using`.
|
|
328
286
|
*
|
|
329
|
-
*
|
|
287
|
+
* The key is `DISPOSE` of `@xmachines/play` and not a bare `Symbol.dispose`, so that
|
|
288
|
+
* the method still answers a `using` that a bundler downlevelled for a target without
|
|
289
|
+
* the well-known symbol.
|
|
330
290
|
*/
|
|
331
|
-
|
|
291
|
+
[DISPOSE](): void;
|
|
332
292
|
}
|
|
333
293
|
//# sourceMappingURL=player-actor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"player-actor.d.ts","sourceRoot":"","sources":["../src/player-actor.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,EACL,KAAK,eAAe,
|
|
1
|
+
{"version":3,"file":"player-actor.d.ts","sourceRoot":"","sources":["../src/player-actor.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,EACL,KAAK,eAAe,EAEpB,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,MAAM,QAAQ,CAAC;AAChB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAGjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAkBhD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAWxC,CAAC;AAYF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwFG;AACH,qBAAa,WAAW,CAAC,QAAQ,SAAS,eAAe,CACxD,SAAQ,KAAK,CAAC,QAAQ,CACtB,YAAW,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;IAElF,OAAO,CAAC,aAAa,CAAC,CAA0B;IAChD;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,QAAQ,CAAC;IAC3C,iCAAiC;IACjC,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,SAAS,CAAC,QAAQ,CAAC,GAA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