@rxova/journey-core 0.6.0 → 0.6.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.
@@ -1,57 +1,99 @@
1
1
  import type { JourneyPersistenceOptions } from "./persistence.types";
2
2
  import type { JourneyTransition } from "./transitions.types";
3
+ /** Terminal outcomes reached when a journey completes or is explicitly terminated. */
3
4
  export type JourneyTerminal = "COMPLETE" | "TERMINATED";
5
+ /** Runtime machine status constants. */
4
6
  export declare const JOURNEY_STATUS: {
5
7
  readonly RUNNING: "running";
6
8
  readonly COMPLETE: "complete";
7
9
  readonly TERMINATED: "terminated";
8
10
  };
11
+ /** Union of possible runtime machine statuses. */
9
12
  export type JourneyStatus = (typeof JOURNEY_STATUS)[keyof typeof JOURNEY_STATUS];
13
+ /** Wildcard step identifier used by transitions that match from any step. */
10
14
  export declare const JOURNEY_WILDCARD: "*";
15
+ /** Built-in event constants that are part of core machine behavior. */
11
16
  export declare const JOURNEY_EVENT: {
12
17
  readonly GO_TO_STEP_BY_ID: "goToStepById";
13
18
  };
19
+ /** Machine event types that are always recognized by core. */
14
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. */
15
24
  export type JourneyBuiltInFrom = typeof JOURNEY_WILDCARD;
25
+ /** Default transition event names supported by machine convenience APIs. */
16
26
  export type JourneyDefaultEventType = "goToNextStep" | "goToPreviousStep" | "terminateJourney" | "completeJourney";
27
+ /** Async lifecycle phases tracked per step while guards/effects run. */
17
28
  export declare const JOURNEY_ASYNC_PHASE: {
18
29
  readonly IDLE: "idle";
19
30
  readonly EVALUATING_WHEN: "evaluating-when";
20
31
  readonly RUNNING_EFFECT: "running-effect";
21
32
  readonly ERROR: "error";
22
33
  };
34
+ /** Union of supported async lifecycle phases. */
23
35
  export type JourneyAsyncPhase = (typeof JOURNEY_ASYNC_PHASE)[keyof typeof JOURNEY_ASYNC_PHASE];
36
+ /** Async execution state for a single step. */
24
37
  export type JourneyStepAsyncState = {
25
38
  phase: JourneyAsyncPhase;
26
39
  eventType: string | null;
27
40
  transitionId: string | null;
28
41
  error: unknown | null;
29
42
  };
43
+ /** Aggregated async state for the machine, keyed by step id. */
30
44
  export type JourneyAsyncState<TStepId extends string> = {
31
45
  isLoading: boolean;
32
46
  byStep: Record<TStepId, JourneyStepAsyncState>;
33
47
  };
48
+ /** Minimal event shape used across runtime boundaries. */
34
49
  export type JourneyBaseEvent = {
35
50
  type: string;
36
51
  payload?: unknown;
37
52
  };
53
+ /** Optional event payload map by event type. */
38
54
  export type JourneyEventPayloadMap<TEventType extends string> = Partial<Record<TEventType | JourneyBuiltInEvent, unknown>>;
55
+ /** Resolves payload type for a specific event type from the provided payload map. */
39
56
  export type JourneyPayloadFor<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>, TEvent extends TEventType | JourneyBuiltInEvent> = TEvent extends keyof TPayloadMap ? TPayloadMap[TEvent] : unknown;
40
- type JourneyPayloadForDefaultEvent<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>, TDefaultEvent extends JourneyDefaultEventType> = JourneyPayloadFor<TEventType | TDefaultEvent, TPayloadMap & JourneyEventPayloadMap<TDefaultEvent>, TDefaultEvent>;
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>;
62
+ /** Built-in direct-navigation event that targets a specific step id. */
41
63
  export type JourneyGoToEvent<TStepId extends string, TPayload = unknown> = {
42
- type: (typeof JOURNEY_EVENT)["GO_TO_STEP_BY_ID"];
64
+ type: JourneyGoToStepByIdEventType;
43
65
  stepId: TStepId;
44
66
  payload?: TPayload;
45
67
  };
46
- export type JourneyEvent<TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = JourneyGoToEvent<TStepId, JourneyPayloadFor<TEventType, TPayloadMap, (typeof JOURNEY_EVENT)["GO_TO_STEP_BY_ID"]>> | {
68
+ /** Event union available to transitions and guards for the declared event type set. */
69
+ export type JourneyEvent<TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = JourneyGoToEvent<TStepId, JourneyPayloadFor<TEventType, TPayloadMap, JourneyGoToStepByIdEventType>> | {
47
70
  [TType in TEventType]: {
48
71
  type: TType;
49
72
  payload?: JourneyPayloadFor<TEventType, TPayloadMap, TType>;
50
73
  };
51
74
  }[TEventType];
52
- export type JourneyStepDefinition<TStepMeta = unknown> = {
75
+ type JourneyDefaultMachineEvent<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
76
+ [TType in JourneyDefaultEventType]: {
77
+ type: TType;
78
+ payload?: JourneyPayloadFor<JourneyMachineEventType<TEventType>, JourneyMachinePayloadMap<TEventType, TPayloadMap>, TType>;
79
+ };
80
+ }[JourneyDefaultEventType];
81
+ type JourneyCustomMachineEvent<TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
82
+ [TType in TEventType]: {
83
+ type: TType;
84
+ payload?: JourneyPayloadFor<TEventType, TPayloadMap, TType>;
85
+ };
86
+ }[TEventType];
87
+ /** Event union accepted by `JourneyMachine.send`. */
88
+ export type JourneySendEvent<TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = JourneyGoToEvent<TStepId, JourneyPayloadFor<JourneyMachineEventType<TEventType>, JourneyMachinePayloadMap<TEventType, TPayloadMap>, JourneyGoToStepByIdEventType>> | JourneyDefaultMachineEvent<TEventType, TPayloadMap> | JourneyCustomMachineEvent<TEventType, TPayloadMap>;
89
+ /**
90
+ * Step definition with optional metadata and optional typed extension fields.
91
+ * Use `TStepExtra` to explicitly model additional per-step properties.
92
+ */
93
+ export type JourneyStepDefinition<TStepMeta = unknown, TStepExtra extends object = Record<never, never>> = {
53
94
  meta?: TStepMeta;
54
- } & Record<string, unknown>;
95
+ } & TStepExtra;
96
+ /** Serializable runtime snapshot of the journey state. */
55
97
  export type JourneySnapshot<TContext, TStepId extends string, TStepMeta = unknown> = {
56
98
  currentStepId: TStepId;
57
99
  history: {
@@ -64,24 +106,28 @@ export type JourneySnapshot<TContext, TStepId extends string, TStepMeta = unknow
64
106
  status: JourneyStatus;
65
107
  async: JourneyAsyncState<TStepId>;
66
108
  };
67
- export type JourneyDefinition<TContext, TStepId extends string = string, TEventType extends string = JourneyDefaultEventType, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown> = {
109
+ /** Full machine definition used to create a journey machine instance. */
110
+ export type JourneyDefinition<TContext, TStepId extends string = string, TEventType extends string = JourneyDefaultEventType, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown, TStepExtra extends object = Record<never, never>> = {
68
111
  initial: TStepId;
69
112
  context: TContext;
70
- steps: Record<TStepId, JourneyStepDefinition<TStepMeta>>;
113
+ steps: Record<TStepId, JourneyStepDefinition<TStepMeta, TStepExtra>>;
71
114
  transitions: readonly JourneyTransition<TContext, TStepId, TEventType, TPayloadMap>[];
72
115
  };
116
+ /** Optional machine features (for example, persistence configuration). */
73
117
  export type JourneyMachineOptions<TContext, TStepId extends string, TStepMeta = unknown> = {
74
118
  persistence?: JourneyPersistenceOptions<TContext, TStepId, TStepMeta>;
75
119
  };
120
+ /** Result returned from send/navigation APIs. */
76
121
  export type JourneySendResult<TContext, TStepId extends string, TStepMeta = unknown> = {
77
122
  transitioned: boolean;
78
123
  transitionId?: string;
79
124
  snapshot: JourneySnapshot<TContext, TStepId, TStepMeta>;
80
125
  };
126
+ /** Observation events emitted by the machine lifecycle/event stream. */
81
127
  export type JourneyObservationEvent<TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown> = {
82
128
  type: "transition.start";
83
129
  from: TStepId;
84
- event: JourneyEvent<TStepId, TEventType, TPayloadMap>;
130
+ event: JourneySendEvent<TStepId, TEventType, TPayloadMap>;
85
131
  timestamp: number;
86
132
  } | {
87
133
  type: "transition.success";
@@ -132,9 +178,10 @@ export type JourneyObservationEvent<TStepId extends string, TEventType extends s
132
178
  next: TStepMeta;
133
179
  timestamp: number;
134
180
  };
181
+ /** Runtime machine API for reading snapshots, sending events, and controlling flow. */
135
182
  export type JourneyMachine<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>, TStepMeta = unknown> = {
136
183
  getSnapshot: () => JourneySnapshot<TContext, TStepId, TStepMeta>;
137
- send: (event: JourneyEvent<TStepId, TEventType, TPayloadMap>) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
184
+ send: (event: JourneySendEvent<TStepId, TEventType, TPayloadMap>) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
138
185
  goToNextStep: () => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
139
186
  terminateJourney: (payload?: JourneyPayloadForDefaultEvent<TEventType, TPayloadMap, "terminateJourney">) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
140
187
  completeJourney: (payload?: JourneyPayloadForDefaultEvent<TEventType, TPayloadMap, "completeJourney">) => Promise<JourneySendResult<TContext, TStepId, TStepMeta>>;
@@ -1,4 +1,5 @@
1
- import type { JOURNEY_EVENT, JourneyBuiltInFrom, JourneyEvent, JourneyEventPayloadMap, JourneyTerminal } from "./journey.types";
1
+ import type { JourneyBuiltInFrom, JourneyEvent, JourneyEventPayloadMap, JourneyGoToStepByIdEventType, JourneyTerminal } from "./journey.types";
2
+ /** Arguments passed to transition guards and effects. */
2
3
  export type JourneyTransitionArgs<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = {
3
4
  context: TContext;
4
5
  from: TStepId;
@@ -6,6 +7,7 @@ export type JourneyTransitionArgs<TContext, TStepId extends string, TEventType e
6
7
  index: number;
7
8
  event: JourneyEvent<TStepId, TEventType, TPayloadMap>;
8
9
  };
10
+ /** Transition destination, either another step or a terminal machine state. */
9
11
  export type JourneyTransitionTarget<TStepId extends string> = TStepId | JourneyTerminal;
10
12
  type JourneyTransitionConfig<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = {
11
13
  id?: string;
@@ -13,6 +15,7 @@ type JourneyTransitionConfig<TContext, TStepId extends string, TEventType extend
13
15
  when?: (args: JourneyTransitionArgs<TContext, TStepId, TEventType, TPayloadMap>) => boolean | Promise<boolean>;
14
16
  effect?: (args: JourneyTransitionArgs<TContext, TStepId, TEventType, TPayloadMap>) => TContext | void | Promise<TContext | void>;
15
17
  };
18
+ /** Transition declared for a standard event or terminal event. */
16
19
  export type JourneyEventTransition<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = (JourneyTransitionConfig<TContext, TStepId, TEventType, TPayloadMap> & {
17
20
  event: Exclude<TEventType, "completeJourney" | "terminateJourney">;
18
21
  to: JourneyTransitionTarget<TStepId>;
@@ -20,28 +23,39 @@ export type JourneyEventTransition<TContext, TStepId extends string, TEventType
20
23
  event: Extract<TEventType, "completeJourney" | "terminateJourney">;
21
24
  to?: never;
22
25
  });
26
+ /** Direct jump transition triggered by the built-in `goToStepById` event. */
23
27
  export type JourneyGoToStepTransition<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = JourneyTransitionConfig<TContext, TStepId, TEventType, TPayloadMap> & {
24
- event: (typeof JOURNEY_EVENT)["GO_TO_STEP_BY_ID"];
28
+ event: JourneyGoToStepByIdEventType;
25
29
  to: TStepId;
26
30
  };
31
+ /** Any transition shape accepted by `createJourneyMachine`. */
27
32
  export type JourneyTransition<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType> = Record<never, never>> = JourneyEventTransition<TContext, TStepId, TEventType, TPayloadMap> | JourneyGoToStepTransition<TContext, TStepId, TEventType, TPayloadMap>;
33
+ /** Optional transition behavior shared by direct and branch transitions. */
28
34
  export type TransitionConfig<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
29
35
  id?: string;
30
36
  effect?: (args: JourneyTransitionArgs<TContext, TStepId, TEventType, TPayloadMap>) => TContext | void | Promise<TContext | void>;
31
37
  };
38
+ /** Branch definition used by `choose(...)`. */
32
39
  export type TransitionBranch<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = TransitionConfig<TContext, TStepId, TEventType, TPayloadMap> & {
33
40
  to: JourneyTransitionTarget<TStepId>;
34
41
  when?: (args: JourneyTransitionArgs<TContext, TStepId, TEventType, TPayloadMap>) => boolean | Promise<boolean>;
35
42
  };
43
+ /** Builder returned by `tx.from(...).on(nonTerminalEvent)` and `tx.any().on(...)`. */
36
44
  export type StandardEventBuilder<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
37
45
  to: (to: JourneyTransitionTarget<TStepId>, config?: TransitionConfig<TContext, TStepId, TEventType, TPayloadMap>) => JourneyTransition<TContext, TStepId, TEventType, TPayloadMap>;
38
46
  choose: (...branches: Array<TransitionBranch<TContext, TStepId, TEventType, TPayloadMap>>) => JourneyTransition<TContext, TStepId, TEventType, TPayloadMap>[];
39
47
  };
48
+ /** Builder returned by `tx.from(...).on("completeJourney")`. */
40
49
  export type CompleteJourneyEventBuilder<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
41
50
  complete: (config?: TransitionConfig<TContext, TStepId, TEventType, TPayloadMap>) => JourneyTransition<TContext, TStepId, TEventType, TPayloadMap>;
42
51
  };
52
+ /** Builder returned by `tx.from(...).on("terminateJourney")`. */
43
53
  export type TerminateJourneyEventBuilder<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = {
44
54
  terminate: (config?: TransitionConfig<TContext, TStepId, TEventType, TPayloadMap>) => JourneyTransition<TContext, TStepId, TEventType, TPayloadMap>;
45
55
  };
56
+ /**
57
+ * Conditional transition builder returned by `tx.from(...).on(event)`:
58
+ * terminal events expose terminal-only helpers, other events expose `to/choose`.
59
+ */
46
60
  export type EventBuilder<TContext, TStepId extends string, TEventType extends string, TPayloadMap extends JourneyEventPayloadMap<TEventType>> = TEventType extends "completeJourney" ? CompleteJourneyEventBuilder<TContext, TStepId, TEventType, TPayloadMap> : TEventType extends "terminateJourney" ? TerminateJourneyEventBuilder<TContext, TStepId, TEventType, TPayloadMap> : StandardEventBuilder<TContext, TStepId, TEventType, TPayloadMap>;
47
61
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxova/journey-core",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Journey core state machine.",
5
5
  "keywords": [
6
6
  "journey",
@@ -57,7 +57,7 @@
57
57
  ],
58
58
  "scripts": {
59
59
  "build": "pnpm run clean && node ./scripts/build.mjs && tsc -p tsconfig.build.json && node ../../scripts/copy-types.mjs dist",
60
- "clean": "rm -rf dist tsconfig.build.tsbuildinfo",
60
+ "clean": "node ../../scripts/clean-paths.mjs dist tsconfig.build.tsbuildinfo",
61
61
  "coverage": "pnpm --workspace-root vitest run --coverage --coverage.include=packages/core/src/** --coverage.reporter=text-summary",
62
62
  "typecheck": "tsc --noEmit -p tsconfig.json",
63
63
  "publint": "publint",