@rxova/journey-core 0.7.0 → 1.0.0-rc.2
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 +236 -105
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/journey-builder/index.d.cts +5 -0
- package/dist/journey-builder/index.d.ts +5 -0
- package/dist/journey-builder/types.d.cts +126 -0
- package/dist/journey-builder/types.d.ts +126 -0
- package/dist/journey-machine/async-state.d.cts +12 -0
- package/dist/journey-machine/async-state.d.ts +12 -0
- package/dist/journey-machine/computed.d.cts +2 -0
- package/dist/journey-machine/computed.d.ts +2 -0
- package/dist/journey-machine/controls.d.cts +17 -0
- package/dist/journey-machine/controls.d.ts +17 -0
- package/dist/journey-machine/helpers.d.cts +52 -0
- package/dist/journey-machine/helpers.d.ts +52 -0
- package/dist/journey-machine/index.d.cts +3 -0
- package/dist/journey-machine/index.d.ts +3 -0
- package/dist/journey-machine/navigation.d.cts +32 -0
- package/dist/journey-machine/navigation.d.ts +32 -0
- package/dist/journey-machine/plugin-controller.d.cts +15 -0
- package/dist/journey-machine/plugin-controller.d.ts +15 -0
- package/dist/journey-machine/resolve-journey-definition.d.cts +2 -0
- package/dist/journey-machine/resolve-journey-definition.d.ts +2 -0
- package/dist/journey-machine/runtime.d.cts +33 -0
- package/dist/journey-machine/runtime.d.ts +33 -0
- package/dist/journey-machine/send.d.cts +9 -0
- package/dist/journey-machine/send.d.ts +9 -0
- package/dist/plugins/analytics/index.cjs +2 -0
- package/dist/plugins/analytics/index.cjs.map +7 -0
- package/dist/plugins/analytics/index.d.cts +25 -0
- package/dist/plugins/analytics/index.d.ts +25 -0
- package/dist/plugins/analytics/index.js +2 -0
- package/dist/plugins/analytics/index.js.map +7 -0
- package/dist/plugins/autosave/index.cjs +2 -0
- package/dist/plugins/autosave/index.cjs.map +7 -0
- package/dist/plugins/autosave/index.d.cts +17 -0
- package/dist/plugins/autosave/index.d.ts +17 -0
- package/dist/plugins/autosave/index.js +2 -0
- package/dist/plugins/autosave/index.js.map +7 -0
- package/dist/plugins/diagnostics/diagnostics.d.cts +3 -0
- package/dist/plugins/diagnostics/diagnostics.d.ts +3 -0
- package/dist/plugins/diagnostics/index.cjs +2 -0
- package/dist/plugins/diagnostics/index.cjs.map +7 -0
- package/dist/plugins/diagnostics/index.d.cts +15 -0
- package/dist/plugins/diagnostics/index.d.ts +15 -0
- package/dist/plugins/diagnostics/index.js +2 -0
- package/dist/plugins/diagnostics/index.js.map +7 -0
- package/dist/plugins/execution-paths/execution-paths.d.cts +6 -0
- package/dist/plugins/execution-paths/execution-paths.d.ts +6 -0
- package/dist/plugins/execution-paths/index.cjs +2 -0
- package/dist/plugins/execution-paths/index.cjs.map +7 -0
- package/dist/plugins/execution-paths/index.d.cts +18 -0
- package/dist/plugins/execution-paths/index.d.ts +18 -0
- package/dist/plugins/execution-paths/index.js +2 -0
- package/dist/plugins/execution-paths/index.js.map +7 -0
- package/dist/plugins/persistence/controller.d.cts +17 -0
- package/dist/plugins/persistence/controller.d.ts +17 -0
- package/dist/plugins/persistence/index.cjs +2 -0
- package/dist/plugins/persistence/index.cjs.map +7 -0
- package/dist/plugins/persistence/index.d.cts +12 -0
- package/dist/plugins/persistence/index.d.ts +12 -0
- package/dist/plugins/persistence/index.js +2 -0
- package/dist/plugins/persistence/index.js.map +7 -0
- package/dist/plugins/replay/index.cjs +2 -0
- package/dist/plugins/replay/index.cjs.map +7 -0
- package/dist/plugins/replay/index.d.cts +27 -0
- package/dist/plugins/replay/index.d.ts +27 -0
- package/dist/plugins/replay/index.js +2 -0
- package/dist/plugins/replay/index.js.map +7 -0
- package/dist/types/analytics.types.d.cts +33 -0
- package/dist/types/analytics.types.d.ts +33 -0
- package/dist/types/autosave.types.d.cts +23 -0
- package/dist/types/autosave.types.d.ts +23 -0
- package/dist/types/diagnostics.types.d.cts +38 -0
- package/dist/types/diagnostics.types.d.ts +38 -0
- package/dist/types/index.d.cts +9 -0
- package/dist/types/index.d.ts +9 -4
- package/dist/types/journey.types.d.cts +191 -0
- package/dist/types/journey.types.d.ts +140 -167
- package/dist/types/machine.types.d.cts +104 -0
- package/dist/types/machine.types.d.ts +104 -0
- package/dist/types/observation.types.d.cts +83 -0
- package/dist/types/observation.types.d.ts +83 -0
- package/dist/types/persistence.types.d.cts +34 -0
- package/dist/types/persistence.types.d.ts +13 -20
- package/dist/types/replay.types.d.cts +35 -0
- package/dist/types/replay.types.d.ts +35 -0
- package/dist/types/transitions.types.d.cts +122 -0
- package/dist/types/transitions.types.d.ts +100 -57
- package/package.json +92 -2
- package/dist/machine-helpers.d.ts +0 -23
- package/dist/machine.d.ts +0 -11
- package/dist/persistence.d.ts +0 -17
- package/dist/transitions.d.ts +0 -62
- package/dist/types.d.ts +0 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { JourneyResolvedTransition, JourneyStepLifecycleCallback, JourneyTransitionsDefinition } from "./transitions.types";
|
|
2
|
+
/** Terminal outcomes reached when a journey completes or is explicitly terminated. */
|
|
3
|
+
export type JourneyTerminal = "COMPLETE" | "TERMINATED";
|
|
4
|
+
/** Union of possible runtime machine statuses. */
|
|
5
|
+
export type JourneyStatus = "idled" | "running" | "completed" | "terminated";
|
|
6
|
+
/** Mode inferred from the journey transition syntax. */
|
|
7
|
+
export type JourneyMode = "linear" | "graph" | "headless";
|
|
8
|
+
/** Wildcard step identifier, exposed as a type-only literal. */
|
|
9
|
+
export type JourneyBuiltInFrom = "*";
|
|
10
|
+
/** Default transition event names supported by machine convenience APIs. */
|
|
11
|
+
export type JourneyDefaultEventType = "goToNextStep" | "goToPreviousStep" | "terminateJourney" | "completeJourney" | "goToStepById";
|
|
12
|
+
/** JSON primitive values accepted inside runtime context. */
|
|
13
|
+
export type JourneyJsonPrimitive = string | number | boolean | null;
|
|
14
|
+
/** JSON-compatible value accepted inside runtime context. */
|
|
15
|
+
export type JourneyJsonValue = JourneyJsonPrimitive | {
|
|
16
|
+
[key: string]: JourneyJsonValue;
|
|
17
|
+
} | JourneyJsonValue[];
|
|
18
|
+
/** JSON-compatible object accepted as the machine context root. */
|
|
19
|
+
export type JourneyJsonObject = {
|
|
20
|
+
[key: string]: JourneyJsonValue;
|
|
21
|
+
};
|
|
22
|
+
/** Derives the full event type union from a user-supplied event map. */
|
|
23
|
+
export type JourneyFullEventType<TEventMap extends Record<string, unknown>> = (keyof TEventMap & string) | JourneyDefaultEventType;
|
|
24
|
+
type JourneyBuiltInSendEventType = Exclude<JourneyDefaultEventType, "goToStepById">;
|
|
25
|
+
type JourneyCustomSendEventType<TEventMap extends Record<string, unknown>> = Exclude<keyof TEventMap & string, JourneyDefaultEventType>;
|
|
26
|
+
/** Union of supported async lifecycle phases. */
|
|
27
|
+
export type JourneyAsyncPhase = "idle" | "evaluating-when" | "error";
|
|
28
|
+
/** Async execution state for a single step. */
|
|
29
|
+
export type JourneyStepAsyncState = {
|
|
30
|
+
phase: JourneyAsyncPhase;
|
|
31
|
+
eventType: string | null;
|
|
32
|
+
transitionId: string | null;
|
|
33
|
+
/** Captured error from a failed guard or lifecycle handler. `null` when no error is present. */
|
|
34
|
+
error: unknown;
|
|
35
|
+
};
|
|
36
|
+
/** Aggregated async state for the machine, keyed by step id. */
|
|
37
|
+
export type JourneyAsyncState<TStepId extends string> = {
|
|
38
|
+
isLoading: boolean;
|
|
39
|
+
byStep: Record<TStepId, JourneyStepAsyncState>;
|
|
40
|
+
};
|
|
41
|
+
/** Minimal event shape used across runtime boundaries. */
|
|
42
|
+
export type JourneyBaseEvent = {
|
|
43
|
+
type: string;
|
|
44
|
+
payload?: unknown;
|
|
45
|
+
};
|
|
46
|
+
/** Resolves payload type for a specific event type from the provided event map. */
|
|
47
|
+
export type JourneyPayloadFor<TEventMap extends Record<string, unknown>, TEvent extends string> = TEvent extends keyof TEventMap ? TEventMap[TEvent] : unknown;
|
|
48
|
+
/** Built-in direct-navigation event that targets a specific step id. */
|
|
49
|
+
export type JourneyGoToEvent<TStepId extends string, TPayload = unknown> = {
|
|
50
|
+
type: "goToStepById";
|
|
51
|
+
stepId: TStepId;
|
|
52
|
+
payload?: TPayload;
|
|
53
|
+
};
|
|
54
|
+
/** Built-in send events supported by machine convenience APIs. */
|
|
55
|
+
export type JourneyBuiltInSendEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneyGoToEvent<TStepId, JourneyPayloadFor<TEventMap, "goToStepById">> | {
|
|
56
|
+
[TType in JourneyBuiltInSendEventType]: {
|
|
57
|
+
type: TType;
|
|
58
|
+
payload?: JourneyPayloadFor<TEventMap, TType>;
|
|
59
|
+
};
|
|
60
|
+
}[JourneyBuiltInSendEventType];
|
|
61
|
+
/** Custom send events derived from a user-supplied event map. */
|
|
62
|
+
export type JourneyCustomSendEvent<TEventMap extends Record<string, unknown> = Record<never, never>> = {
|
|
63
|
+
[TType in JourneyCustomSendEventType<TEventMap>]: {
|
|
64
|
+
type: TType;
|
|
65
|
+
payload?: JourneyPayloadFor<TEventMap, TType>;
|
|
66
|
+
};
|
|
67
|
+
}[JourneyCustomSendEventType<TEventMap>];
|
|
68
|
+
/** Event union accepted by `JourneyMachine.send`. */
|
|
69
|
+
export type JourneySendEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneyBuiltInSendEvent<TStepId, TEventMap> | JourneyCustomSendEvent<TEventMap>;
|
|
70
|
+
/** Event union available to transitions and guards for the declared event type set. */
|
|
71
|
+
export type JourneyEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneySendEvent<TStepId, TEventMap>;
|
|
72
|
+
/** Step definition with optional metadata and lifecycle callbacks. */
|
|
73
|
+
export type JourneyStepDefinition<TContext extends JourneyJsonObject = JourneyJsonObject, TStepId extends string = string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = {
|
|
74
|
+
meta?: TStepMeta;
|
|
75
|
+
/** Called when the machine enters this step. */
|
|
76
|
+
onEnter?: JourneyStepLifecycleCallback<TContext, TStepId, TEventMap, THandlers>;
|
|
77
|
+
/** Called when the machine leaves this step. */
|
|
78
|
+
onLeave?: JourneyStepLifecycleCallback<TContext, TStepId, TEventMap, THandlers>;
|
|
79
|
+
};
|
|
80
|
+
/** Timeline of visited steps and current history index. */
|
|
81
|
+
export type JourneyHistory<TStepId extends string> = {
|
|
82
|
+
timeline: readonly TStepId[];
|
|
83
|
+
index: number;
|
|
84
|
+
};
|
|
85
|
+
/** Core snapshot state without async execution details. */
|
|
86
|
+
export type JourneySnapshotStateBase<TContext extends JourneyJsonObject, TStepId extends string> = {
|
|
87
|
+
currentStepId: TStepId;
|
|
88
|
+
history: JourneyHistory<TStepId>;
|
|
89
|
+
context: TContext;
|
|
90
|
+
visited: Record<TStepId, boolean>;
|
|
91
|
+
status: JourneyStatus;
|
|
92
|
+
};
|
|
93
|
+
/** Serializable runtime snapshot of the journey state. */
|
|
94
|
+
export type JourneySnapshot<TContext extends JourneyJsonObject, TStepId extends string> = JourneySnapshotStateBase<TContext, TStepId> & {
|
|
95
|
+
async: JourneyAsyncState<TStepId>;
|
|
96
|
+
};
|
|
97
|
+
/** Common read-only computed state exposed by a machine instance. */
|
|
98
|
+
export type JourneyComputedBase<TStepId extends string> = {
|
|
99
|
+
mode: JourneyMode;
|
|
100
|
+
activeStepId: TStepId;
|
|
101
|
+
activeStepIndex: number;
|
|
102
|
+
visitedStepCount: number;
|
|
103
|
+
isLoading: boolean;
|
|
104
|
+
isIdle: boolean;
|
|
105
|
+
isRunning: boolean;
|
|
106
|
+
isComplete: boolean;
|
|
107
|
+
isTerminated: boolean;
|
|
108
|
+
isInitialStep: boolean;
|
|
109
|
+
};
|
|
110
|
+
/** Wizard-style computed state available when transitions use linear array syntax. */
|
|
111
|
+
export type JourneyLinearComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
112
|
+
mode: "linear";
|
|
113
|
+
stepCount: number;
|
|
114
|
+
journeyLength: number;
|
|
115
|
+
isFirstStep: boolean;
|
|
116
|
+
isLastStep: boolean;
|
|
117
|
+
stepOrder: readonly TStepId[];
|
|
118
|
+
};
|
|
119
|
+
/** Computed state available when transitions use graph object syntax. */
|
|
120
|
+
export type JourneyGraphComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
121
|
+
mode: "graph";
|
|
122
|
+
stepCount?: undefined;
|
|
123
|
+
journeyLength?: undefined;
|
|
124
|
+
isFirstStep?: undefined;
|
|
125
|
+
isLastStep?: undefined;
|
|
126
|
+
stepOrder?: undefined;
|
|
127
|
+
};
|
|
128
|
+
/** Computed state available when transitions are omitted and navigation is headless. */
|
|
129
|
+
export type JourneyHeadlessComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
130
|
+
mode: "headless";
|
|
131
|
+
stepCount?: undefined;
|
|
132
|
+
journeyLength?: undefined;
|
|
133
|
+
isFirstStep?: undefined;
|
|
134
|
+
isLastStep?: undefined;
|
|
135
|
+
stepOrder?: undefined;
|
|
136
|
+
};
|
|
137
|
+
/** Mode-aware computed state returned by `JourneyMachine.getComputed()`. */
|
|
138
|
+
export type JourneyComputed<TStepId extends string> = JourneyLinearComputed<TStepId> | JourneyGraphComputed<TStepId> | JourneyHeadlessComputed<TStepId>;
|
|
139
|
+
/** Selector function that derives a value from a machine snapshot. */
|
|
140
|
+
export type JourneySelector<TContext extends JourneyJsonObject, TStepId extends string, TSelected = unknown> = (snapshot: JourneySnapshot<TContext, TStepId>) => TSelected;
|
|
141
|
+
/** Equality function used to compare selected values between snapshot updates. */
|
|
142
|
+
export type JourneyEqualityFn<TValue> = (previous: TValue, next: TValue) => boolean;
|
|
143
|
+
/** Shared definition fields without transition configuration. */
|
|
144
|
+
export type JourneyDefinitionBase<TContext extends JourneyJsonObject, TStepId extends string, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = {
|
|
145
|
+
/**
|
|
146
|
+
* The step the machine starts on.
|
|
147
|
+
* - **Linear transitions**: optional — defaults to the first element of the
|
|
148
|
+
* transitions array. When provided, must exist in the array and the machine
|
|
149
|
+
* starts from that step (useful for resuming mid-flow).
|
|
150
|
+
* - **Graph / headless**: required.
|
|
151
|
+
*/
|
|
152
|
+
initial?: TStepId;
|
|
153
|
+
context: TContext;
|
|
154
|
+
handlers?: THandlers;
|
|
155
|
+
steps: Record<TStepId, JourneyStepDefinition<TContext, TStepId, Record<never, never>, TStepMeta, THandlers>>;
|
|
156
|
+
};
|
|
157
|
+
/** Full machine definition used to create a journey machine instance. */
|
|
158
|
+
export type JourneyDefinition<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = Omit<JourneyDefinitionBase<TContext, TStepId, TStepMeta, THandlers>, "steps"> & {
|
|
159
|
+
steps: Record<TStepId, JourneyStepDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>>;
|
|
160
|
+
transitions?: JourneyTransitionsDefinition<TContext, TStepId, TEventMap, THandlers>;
|
|
161
|
+
};
|
|
162
|
+
export type JourneyResolvedDefinition<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = Required<Pick<JourneyDefinitionBase<TContext, TStepId, TStepMeta, THandlers>, "initial">> & Omit<JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>, "transitions"> & {
|
|
163
|
+
transitions: readonly JourneyResolvedTransition<TContext, TStepId, TEventMap, THandlers>[];
|
|
164
|
+
};
|
|
165
|
+
export type JourneyExecutionPathEventType<TEventType extends string> = TEventType | JourneyDefaultEventType;
|
|
166
|
+
/** Structural execution path returned by `getExecutionPaths()`. */
|
|
167
|
+
export type JourneyExecutionPath<TStepId extends string, TEventType extends string> = {
|
|
168
|
+
steps: TStepId[];
|
|
169
|
+
events: JourneyExecutionPathEventType<TEventType>[];
|
|
170
|
+
terminated: "final" | "depth" | "cycle" | "limit";
|
|
171
|
+
};
|
|
172
|
+
/** Result returned by structural path enumeration. */
|
|
173
|
+
export type JourneyExecutionPathsResult<TStepId extends string, TEventType extends string> = {
|
|
174
|
+
paths: JourneyExecutionPath<TStepId, TEventType>[];
|
|
175
|
+
truncated: boolean;
|
|
176
|
+
cyclesDetected: boolean;
|
|
177
|
+
};
|
|
178
|
+
/** Options for structural path enumeration from the initial step. */
|
|
179
|
+
export type JourneyExecutionPathOptions = {
|
|
180
|
+
maxDepth?: number;
|
|
181
|
+
maxPaths?: number;
|
|
182
|
+
};
|
|
183
|
+
/** Result returned from send/navigation APIs. */
|
|
184
|
+
export type JourneySendResult<TContext extends JourneyJsonObject, TStepId extends string> = {
|
|
185
|
+
transitioned: boolean;
|
|
186
|
+
transitionId?: string;
|
|
187
|
+
label?: string;
|
|
188
|
+
error?: unknown;
|
|
189
|
+
snapshot: JourneySnapshot<TContext, TStepId>;
|
|
190
|
+
};
|
|
191
|
+
export {};
|
|
@@ -1,44 +1,37 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { JourneyTransition } from "./transitions.types";
|
|
1
|
+
import type { JourneyResolvedTransition, JourneyStepLifecycleCallback, JourneyTransitionsDefinition } from "./transitions.types";
|
|
3
2
|
/** Terminal outcomes reached when a journey completes or is explicitly terminated. */
|
|
4
3
|
export type JourneyTerminal = "COMPLETE" | "TERMINATED";
|
|
5
|
-
/** Runtime machine status constants. */
|
|
6
|
-
export declare const JOURNEY_STATUS: {
|
|
7
|
-
readonly RUNNING: "running";
|
|
8
|
-
readonly COMPLETE: "complete";
|
|
9
|
-
readonly TERMINATED: "terminated";
|
|
10
|
-
};
|
|
11
4
|
/** Union of possible runtime machine statuses. */
|
|
12
|
-
export type JourneyStatus =
|
|
13
|
-
/**
|
|
14
|
-
export
|
|
15
|
-
/**
|
|
16
|
-
export
|
|
17
|
-
readonly GO_TO_STEP_BY_ID: "goToStepById";
|
|
18
|
-
};
|
|
19
|
-
/** Machine event types that are always recognized by core. */
|
|
20
|
-
export type JourneyBuiltInEvent = (typeof JOURNEY_EVENT)[keyof typeof JOURNEY_EVENT];
|
|
21
|
-
/** Event literal type for the built-in go-to-step command. */
|
|
22
|
-
export type JourneyGoToStepByIdEventType = typeof JOURNEY_EVENT.GO_TO_STEP_BY_ID;
|
|
23
|
-
/** Wildcard origin marker for transitions. */
|
|
24
|
-
export type JourneyBuiltInFrom = typeof JOURNEY_WILDCARD;
|
|
5
|
+
export type JourneyStatus = "idled" | "running" | "completed" | "terminated";
|
|
6
|
+
/** Mode inferred from the journey transition syntax. */
|
|
7
|
+
export type JourneyMode = "linear" | "graph" | "headless";
|
|
8
|
+
/** Wildcard step identifier, exposed as a type-only literal. */
|
|
9
|
+
export type JourneyBuiltInFrom = "*";
|
|
25
10
|
/** Default transition event names supported by machine convenience APIs. */
|
|
26
|
-
export type JourneyDefaultEventType = "goToNextStep" | "goToPreviousStep" | "terminateJourney" | "completeJourney";
|
|
27
|
-
/**
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
11
|
+
export type JourneyDefaultEventType = "goToNextStep" | "goToPreviousStep" | "terminateJourney" | "completeJourney" | "goToStepById";
|
|
12
|
+
/** JSON primitive values accepted inside runtime context. */
|
|
13
|
+
export type JourneyJsonPrimitive = string | number | boolean | null;
|
|
14
|
+
/** JSON-compatible value accepted inside runtime context. */
|
|
15
|
+
export type JourneyJsonValue = JourneyJsonPrimitive | {
|
|
16
|
+
[key: string]: JourneyJsonValue;
|
|
17
|
+
} | JourneyJsonValue[];
|
|
18
|
+
/** JSON-compatible object accepted as the machine context root. */
|
|
19
|
+
export type JourneyJsonObject = {
|
|
20
|
+
[key: string]: JourneyJsonValue;
|
|
33
21
|
};
|
|
22
|
+
/** Derives the full event type union from a user-supplied event map. */
|
|
23
|
+
export type JourneyFullEventType<TEventMap extends Record<string, unknown>> = (keyof TEventMap & string) | JourneyDefaultEventType;
|
|
24
|
+
type JourneyBuiltInSendEventType = Exclude<JourneyDefaultEventType, "goToStepById">;
|
|
25
|
+
type JourneyCustomSendEventType<TEventMap extends Record<string, unknown>> = Exclude<keyof TEventMap & string, JourneyDefaultEventType>;
|
|
34
26
|
/** Union of supported async lifecycle phases. */
|
|
35
|
-
export type JourneyAsyncPhase =
|
|
27
|
+
export type JourneyAsyncPhase = "idle" | "evaluating-when" | "error";
|
|
36
28
|
/** Async execution state for a single step. */
|
|
37
29
|
export type JourneyStepAsyncState = {
|
|
38
30
|
phase: JourneyAsyncPhase;
|
|
39
31
|
eventType: string | null;
|
|
40
32
|
transitionId: string | null;
|
|
41
|
-
error
|
|
33
|
+
/** Captured error from a failed guard or lifecycle handler. `null` when no error is present. */
|
|
34
|
+
error: unknown;
|
|
42
35
|
};
|
|
43
36
|
/** Aggregated async state for the machine, keyed by step id. */
|
|
44
37
|
export type JourneyAsyncState<TStepId extends string> = {
|
|
@@ -50,169 +43,149 @@ export type JourneyBaseEvent = {
|
|
|
50
43
|
type: string;
|
|
51
44
|
payload?: unknown;
|
|
52
45
|
};
|
|
53
|
-
/**
|
|
54
|
-
export type
|
|
55
|
-
/** Resolves payload type for a specific event type from the provided payload map. */
|
|
56
|
-
export type JourneyPayloadFor<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>, TEvent extends TEventType | JourneyBuiltInEvent> = TEvent extends keyof TPayloadMap ? TPayloadMap[TEvent] : unknown;
|
|
57
|
-
/** Event-type union accepted by machine `.send()`, including built-in convenience events. */
|
|
58
|
-
export type JourneyMachineEventType<TEventType extends string> = TEventType | JourneyDefaultEventType;
|
|
59
|
-
/** Payload map available to machine `.send()`, including built-in convenience events. */
|
|
60
|
-
export type JourneyMachinePayloadMap<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = TPayloadMap & JourneyEventPayloadMap<JourneyDefaultEventType>;
|
|
61
|
-
type JourneyPayloadForDefaultEvent<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>, TDefaultEvent extends JourneyDefaultEventType> = JourneyPayloadFor<JourneyMachineEventType<TEventType>, JourneyMachinePayloadMap<TEventType, TPayloadMap>, TDefaultEvent>;
|
|
46
|
+
/** Resolves payload type for a specific event type from the provided event map. */
|
|
47
|
+
export type JourneyPayloadFor<TEventMap extends Record<string, unknown>, TEvent extends string> = TEvent extends keyof TEventMap ? TEventMap[TEvent] : unknown;
|
|
62
48
|
/** Built-in direct-navigation event that targets a specific step id. */
|
|
63
49
|
export type JourneyGoToEvent<TStepId extends string, TPayload = unknown> = {
|
|
64
|
-
type:
|
|
50
|
+
type: "goToStepById";
|
|
65
51
|
stepId: TStepId;
|
|
66
52
|
payload?: TPayload;
|
|
67
53
|
};
|
|
68
|
-
/**
|
|
69
|
-
export type
|
|
70
|
-
[TType in
|
|
71
|
-
type: TType;
|
|
72
|
-
payload?: JourneyPayloadFor<TEventType, TPayloadMap, TType>;
|
|
73
|
-
};
|
|
74
|
-
}[TEventType];
|
|
75
|
-
type JourneyDefaultMachineEvent<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
|
|
76
|
-
[TType in JourneyDefaultEventType]: {
|
|
54
|
+
/** Built-in send events supported by machine convenience APIs. */
|
|
55
|
+
export type JourneyBuiltInSendEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneyGoToEvent<TStepId, JourneyPayloadFor<TEventMap, "goToStepById">> | {
|
|
56
|
+
[TType in JourneyBuiltInSendEventType]: {
|
|
77
57
|
type: TType;
|
|
78
|
-
payload?: JourneyPayloadFor<
|
|
58
|
+
payload?: JourneyPayloadFor<TEventMap, TType>;
|
|
79
59
|
};
|
|
80
|
-
}[
|
|
81
|
-
|
|
82
|
-
|
|
60
|
+
}[JourneyBuiltInSendEventType];
|
|
61
|
+
/** Custom send events derived from a user-supplied event map. */
|
|
62
|
+
export type JourneyCustomSendEvent<TEventMap extends Record<string, unknown> = Record<never, never>> = {
|
|
63
|
+
[TType in JourneyCustomSendEventType<TEventMap>]: {
|
|
83
64
|
type: TType;
|
|
84
|
-
payload?: JourneyPayloadFor<
|
|
65
|
+
payload?: JourneyPayloadFor<TEventMap, TType>;
|
|
85
66
|
};
|
|
86
|
-
}[
|
|
67
|
+
}[JourneyCustomSendEventType<TEventMap>];
|
|
87
68
|
/** Event union accepted by `JourneyMachine.send`. */
|
|
88
|
-
export type JourneySendEvent<TStepId extends string,
|
|
89
|
-
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
export type JourneyStepDefinition<TStepMeta = unknown, TStepExtra extends object = Record<never, never>> = {
|
|
69
|
+
export type JourneySendEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneyBuiltInSendEvent<TStepId, TEventMap> | JourneyCustomSendEvent<TEventMap>;
|
|
70
|
+
/** Event union available to transitions and guards for the declared event type set. */
|
|
71
|
+
export type JourneyEvent<TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>> = JourneySendEvent<TStepId, TEventMap>;
|
|
72
|
+
/** Step definition with optional metadata and lifecycle callbacks. */
|
|
73
|
+
export type JourneyStepDefinition<TContext extends JourneyJsonObject = JourneyJsonObject, TStepId extends string = string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = {
|
|
94
74
|
meta?: TStepMeta;
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
75
|
+
/** Called when the machine enters this step. */
|
|
76
|
+
onEnter?: JourneyStepLifecycleCallback<TContext, TStepId, TEventMap, THandlers>;
|
|
77
|
+
/** Called when the machine leaves this step. */
|
|
78
|
+
onLeave?: JourneyStepLifecycleCallback<TContext, TStepId, TEventMap, THandlers>;
|
|
79
|
+
};
|
|
80
|
+
/** Timeline of visited steps and current history index. */
|
|
81
|
+
export type JourneyHistory<TStepId extends string> = {
|
|
82
|
+
timeline: readonly TStepId[];
|
|
83
|
+
index: number;
|
|
84
|
+
};
|
|
85
|
+
/** Core snapshot state without async execution details. */
|
|
86
|
+
export type JourneySnapshotStateBase<TContext extends JourneyJsonObject, TStepId extends string> = {
|
|
98
87
|
currentStepId: TStepId;
|
|
99
|
-
history:
|
|
100
|
-
timeline: readonly TStepId[];
|
|
101
|
-
index: number;
|
|
102
|
-
};
|
|
88
|
+
history: JourneyHistory<TStepId>;
|
|
103
89
|
context: TContext;
|
|
104
90
|
visited: Record<TStepId, boolean>;
|
|
105
|
-
stepMeta: Record<TStepId, TStepMeta>;
|
|
106
91
|
status: JourneyStatus;
|
|
92
|
+
};
|
|
93
|
+
/** Serializable runtime snapshot of the journey state. */
|
|
94
|
+
export type JourneySnapshot<TContext extends JourneyJsonObject, TStepId extends string> = JourneySnapshotStateBase<TContext, TStepId> & {
|
|
107
95
|
async: JourneyAsyncState<TStepId>;
|
|
108
96
|
};
|
|
97
|
+
/** Common read-only computed state exposed by a machine instance. */
|
|
98
|
+
export type JourneyComputedBase<TStepId extends string> = {
|
|
99
|
+
mode: JourneyMode;
|
|
100
|
+
activeStepId: TStepId;
|
|
101
|
+
activeStepIndex: number;
|
|
102
|
+
visitedStepCount: number;
|
|
103
|
+
isLoading: boolean;
|
|
104
|
+
isIdle: boolean;
|
|
105
|
+
isRunning: boolean;
|
|
106
|
+
isComplete: boolean;
|
|
107
|
+
isTerminated: boolean;
|
|
108
|
+
isInitialStep: boolean;
|
|
109
|
+
};
|
|
110
|
+
/** Wizard-style computed state available when transitions use linear array syntax. */
|
|
111
|
+
export type JourneyLinearComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
112
|
+
mode: "linear";
|
|
113
|
+
stepCount: number;
|
|
114
|
+
journeyLength: number;
|
|
115
|
+
isFirstStep: boolean;
|
|
116
|
+
isLastStep: boolean;
|
|
117
|
+
stepOrder: readonly TStepId[];
|
|
118
|
+
};
|
|
119
|
+
/** Computed state available when transitions use graph object syntax. */
|
|
120
|
+
export type JourneyGraphComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
121
|
+
mode: "graph";
|
|
122
|
+
stepCount?: undefined;
|
|
123
|
+
journeyLength?: undefined;
|
|
124
|
+
isFirstStep?: undefined;
|
|
125
|
+
isLastStep?: undefined;
|
|
126
|
+
stepOrder?: undefined;
|
|
127
|
+
};
|
|
128
|
+
/** Computed state available when transitions are omitted and navigation is headless. */
|
|
129
|
+
export type JourneyHeadlessComputed<TStepId extends string> = JourneyComputedBase<TStepId> & {
|
|
130
|
+
mode: "headless";
|
|
131
|
+
stepCount?: undefined;
|
|
132
|
+
journeyLength?: undefined;
|
|
133
|
+
isFirstStep?: undefined;
|
|
134
|
+
isLastStep?: undefined;
|
|
135
|
+
stepOrder?: undefined;
|
|
136
|
+
};
|
|
137
|
+
/** Mode-aware computed state returned by `JourneyMachine.getComputed()`. */
|
|
138
|
+
export type JourneyComputed<TStepId extends string> = JourneyLinearComputed<TStepId> | JourneyGraphComputed<TStepId> | JourneyHeadlessComputed<TStepId>;
|
|
109
139
|
/** Selector function that derives a value from a machine snapshot. */
|
|
110
|
-
export type JourneySelector<TContext, TStepId extends string,
|
|
140
|
+
export type JourneySelector<TContext extends JourneyJsonObject, TStepId extends string, TSelected = unknown> = (snapshot: JourneySnapshot<TContext, TStepId>) => TSelected;
|
|
111
141
|
/** Equality function used to compare selected values between snapshot updates. */
|
|
112
142
|
export type JourneyEqualityFn<TValue> = (previous: TValue, next: TValue) => boolean;
|
|
113
|
-
/**
|
|
114
|
-
export type
|
|
115
|
-
|
|
143
|
+
/** Shared definition fields without transition configuration. */
|
|
144
|
+
export type JourneyDefinitionBase<TContext extends JourneyJsonObject, TStepId extends string, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = {
|
|
145
|
+
/**
|
|
146
|
+
* The step the machine starts on.
|
|
147
|
+
* - **Linear transitions**: optional — defaults to the first element of the
|
|
148
|
+
* transitions array. When provided, must exist in the array and the machine
|
|
149
|
+
* starts from that step (useful for resuming mid-flow).
|
|
150
|
+
* - **Graph / headless**: required.
|
|
151
|
+
*/
|
|
152
|
+
initial?: TStepId;
|
|
116
153
|
context: TContext;
|
|
117
|
-
|
|
118
|
-
|
|
154
|
+
handlers?: THandlers;
|
|
155
|
+
steps: Record<TStepId, JourneyStepDefinition<TContext, TStepId, Record<never, never>, TStepMeta, THandlers>>;
|
|
156
|
+
};
|
|
157
|
+
/** Full machine definition used to create a journey machine instance. */
|
|
158
|
+
export type JourneyDefinition<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = Omit<JourneyDefinitionBase<TContext, TStepId, TStepMeta, THandlers>, "steps"> & {
|
|
159
|
+
steps: Record<TStepId, JourneyStepDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>>;
|
|
160
|
+
transitions?: JourneyTransitionsDefinition<TContext, TStepId, TEventMap, THandlers>;
|
|
161
|
+
};
|
|
162
|
+
export type JourneyResolvedDefinition<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>> = Required<Pick<JourneyDefinitionBase<TContext, TStepId, TStepMeta, THandlers>, "initial">> & Omit<JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>, "transitions"> & {
|
|
163
|
+
transitions: readonly JourneyResolvedTransition<TContext, TStepId, TEventMap, THandlers>[];
|
|
119
164
|
};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
165
|
+
export type JourneyExecutionPathEventType<TEventType extends string> = TEventType | JourneyDefaultEventType;
|
|
166
|
+
/** Structural execution path returned by `getExecutionPaths()`. */
|
|
167
|
+
export type JourneyExecutionPath<TStepId extends string, TEventType extends string> = {
|
|
168
|
+
steps: TStepId[];
|
|
169
|
+
events: JourneyExecutionPathEventType<TEventType>[];
|
|
170
|
+
terminated: "final" | "depth" | "cycle" | "limit";
|
|
171
|
+
};
|
|
172
|
+
/** Result returned by structural path enumeration. */
|
|
173
|
+
export type JourneyExecutionPathsResult<TStepId extends string, TEventType extends string> = {
|
|
174
|
+
paths: JourneyExecutionPath<TStepId, TEventType>[];
|
|
175
|
+
truncated: boolean;
|
|
176
|
+
cyclesDetected: boolean;
|
|
177
|
+
};
|
|
178
|
+
/** Options for structural path enumeration from the initial step. */
|
|
179
|
+
export type JourneyExecutionPathOptions = {
|
|
180
|
+
maxDepth?: number;
|
|
181
|
+
maxPaths?: number;
|
|
124
182
|
};
|
|
125
183
|
/** Result returned from send/navigation APIs. */
|
|
126
|
-
export type JourneySendResult<TContext, TStepId extends string
|
|
184
|
+
export type JourneySendResult<TContext extends JourneyJsonObject, TStepId extends string> = {
|
|
127
185
|
transitioned: boolean;
|
|
128
186
|
transitionId?: string;
|
|
187
|
+
label?: string;
|
|
129
188
|
error?: unknown;
|
|
130
|
-
snapshot: JourneySnapshot<TContext, TStepId
|
|
131
|
-
};
|
|
132
|
-
/** Observation events emitted by the machine lifecycle/event stream. */
|
|
133
|
-
export type JourneyObservationEvent<TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown> = {
|
|
134
|
-
type: "journey.start";
|
|
135
|
-
stepId: TStepId;
|
|
136
|
-
timestamp: number;
|
|
137
|
-
} | {
|
|
138
|
-
type: "transition.start";
|
|
139
|
-
from: TStepId;
|
|
140
|
-
event: JourneySendEvent<TStepId, TEventType, TPayloadMap>;
|
|
141
|
-
timestamp: number;
|
|
142
|
-
} | {
|
|
143
|
-
type: "transition.success";
|
|
144
|
-
from: TStepId;
|
|
145
|
-
to: TStepId | JourneyTerminal;
|
|
146
|
-
eventType: string;
|
|
147
|
-
transitionId: string | null;
|
|
148
|
-
timestamp: number;
|
|
149
|
-
} | {
|
|
150
|
-
type: "transition.error";
|
|
151
|
-
from: TStepId;
|
|
152
|
-
eventType: string;
|
|
153
|
-
transitionId: string | null;
|
|
154
|
-
error: unknown;
|
|
155
|
-
timestamp: number;
|
|
156
|
-
} | {
|
|
157
|
-
type: "step.exit";
|
|
158
|
-
stepId: TStepId;
|
|
159
|
-
timestamp: number;
|
|
160
|
-
} | {
|
|
161
|
-
type: "step.enter";
|
|
162
|
-
stepId: TStepId;
|
|
163
|
-
timestamp: number;
|
|
164
|
-
} | {
|
|
165
|
-
type: "journey.complete";
|
|
166
|
-
stepId: TStepId;
|
|
167
|
-
timestamp: number;
|
|
168
|
-
} | {
|
|
169
|
-
type: "journey.close";
|
|
170
|
-
stepId: TStepId;
|
|
171
|
-
timestamp: number;
|
|
172
|
-
} | {
|
|
173
|
-
type: "navigation.previous";
|
|
174
|
-
from: TStepId;
|
|
175
|
-
to: TStepId;
|
|
176
|
-
requestedSteps: number;
|
|
177
|
-
appliedSteps: number;
|
|
178
|
-
timestamp: number;
|
|
179
|
-
} | {
|
|
180
|
-
type: "navigation.lastVisited";
|
|
181
|
-
from: TStepId;
|
|
182
|
-
to: TStepId;
|
|
183
|
-
timestamp: number;
|
|
184
|
-
} | {
|
|
185
|
-
type: "metadata.updated";
|
|
186
|
-
stepId: TStepId;
|
|
187
|
-
previous: TStepMeta;
|
|
188
|
-
next: TStepMeta;
|
|
189
|
-
timestamp: number;
|
|
190
|
-
};
|
|
191
|
-
/** Runtime machine API for reading snapshots, sending events, and controlling flow. */
|
|
192
|
-
export type JourneyMachine<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown> = {
|
|
193
|
-
getSnapshot: () => JourneySnapshot<TContext, TStepId, TStepMeta>;
|
|
194
|
-
send: (event: JourneySendEvent<TStepId, TEventType, TPayloadMap>) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
195
|
-
goToNextStep: () => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
196
|
-
terminateJourney: (payload?: JourneyPayloadForDefaultEvent<TEventType, TPayloadMap, "terminateJourney">) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
197
|
-
completeJourney: (payload?: JourneyPayloadForDefaultEvent<TEventType, TPayloadMap, "completeJourney">) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
198
|
-
goToPreviousStep: (steps?: number) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
199
|
-
goToLastVisitedStep: () => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
|
|
200
|
-
updateContext: (updater: (context: TContext) => TContext) => JourneySnapshot<TContext, TStepId, TStepMeta>;
|
|
201
|
-
updateStepMetadata: (stepId: TStepId, updater: (metadata: TStepMeta) => TStepMeta) => JourneySnapshot<TContext, TStepId, TStepMeta>;
|
|
202
|
-
clearStepError: (stepId?: TStepId) => JourneySnapshot<TContext, TStepId, TStepMeta>;
|
|
203
|
-
resetMachine: () => JourneySnapshot<TContext, TStepId, TStepMeta>;
|
|
204
|
-
dispose: () => void;
|
|
205
|
-
subscribe: (listener: () => void) => () => void;
|
|
206
|
-
subscribeSelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TStepMeta, TSelected>, listener: (next: TSelected, previous: TSelected) => void, equalityFn?: JourneyEqualityFn<TSelected>) => () => void;
|
|
207
|
-
subscribeEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventType, TPayloadMap, TStepMeta>) => void) => () => void;
|
|
208
|
-
subscribeStart: (listener: (event: Extract<JourneyObservationEvent<TStepId, TEventType, TPayloadMap, TStepMeta>, {
|
|
209
|
-
type: "journey.start";
|
|
210
|
-
}>) => void) => () => void;
|
|
211
|
-
subscribeComplete: (listener: (event: Extract<JourneyObservationEvent<TStepId, TEventType, TPayloadMap, TStepMeta>, {
|
|
212
|
-
type: "journey.complete";
|
|
213
|
-
}>) => void) => () => void;
|
|
214
|
-
subscribeTerminate: (listener: (event: Extract<JourneyObservationEvent<TStepId, TEventType, TPayloadMap, TStepMeta>, {
|
|
215
|
-
type: "journey.close";
|
|
216
|
-
}>) => void) => () => void;
|
|
189
|
+
snapshot: JourneySnapshot<TContext, TStepId>;
|
|
217
190
|
};
|
|
218
191
|
export {};
|