ihsm 0.0.26 → 0.1.21
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 +105 -113
- package/lib/cjs/index.d.ts +8 -1394
- package/lib/cjs/index.js +65 -764
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/internal/console-instrumentation.d.ts +34 -0
- package/lib/cjs/internal/console-instrumentation.js +71 -0
- package/lib/cjs/internal/console-instrumentation.js.map +1 -0
- package/lib/cjs/internal/identity.d.ts +16 -0
- package/lib/cjs/internal/identity.js +170 -0
- package/lib/cjs/internal/identity.js.map +1 -0
- package/lib/cjs/internal/instrumentation.d.ts +47 -0
- package/lib/cjs/internal/instrumentation.js +201 -0
- package/lib/cjs/internal/instrumentation.js.map +1 -0
- package/lib/cjs/internal/runtime.d.ts +376 -0
- package/lib/cjs/internal/runtime.js +2530 -0
- package/lib/cjs/internal/runtime.js.map +1 -0
- package/lib/cjs/internal/types.d.ts +515 -0
- package/lib/cjs/internal/types.js +9 -0
- package/lib/cjs/internal/types.js.map +1 -0
- package/lib/cjs/test-only.d.ts +5 -0
- package/lib/cjs/test-only.js +21 -0
- package/lib/cjs/test-only.js.map +1 -0
- package/lib/cjs/testing.d.ts +120 -88
- package/lib/cjs/testing.js +126 -38
- package/lib/cjs/testing.js.map +1 -1
- package/lib/cjs/transition-routines.d.ts +3 -0
- package/lib/cjs/transition-routines.js +11 -0
- package/lib/cjs/transition-routines.js.map +1 -0
- package/lib/cjs/types.d.ts +5 -0
- package/lib/cjs/{internal/defs.private.js → types.js} +1 -1
- package/lib/cjs/types.js.map +1 -0
- package/lib/esm/index.d.ts +8 -1394
- package/lib/esm/index.js +5 -742
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/internal/console-instrumentation.d.ts +34 -0
- package/lib/esm/internal/console-instrumentation.js +68 -0
- package/lib/esm/internal/console-instrumentation.js.map +1 -0
- package/lib/esm/internal/identity.d.ts +16 -0
- package/lib/esm/internal/identity.js +159 -0
- package/lib/esm/internal/identity.js.map +1 -0
- package/lib/esm/internal/instrumentation.d.ts +47 -0
- package/lib/esm/internal/instrumentation.js +178 -0
- package/lib/esm/internal/instrumentation.js.map +1 -0
- package/lib/esm/internal/runtime.d.ts +376 -0
- package/lib/esm/internal/runtime.js +2462 -0
- package/lib/esm/internal/runtime.js.map +1 -0
- package/lib/esm/internal/types.d.ts +515 -0
- package/lib/esm/internal/types.js +6 -0
- package/lib/esm/internal/types.js.map +1 -0
- package/lib/esm/test-only.d.ts +5 -0
- package/lib/esm/test-only.js +15 -0
- package/lib/esm/test-only.js.map +1 -0
- package/lib/esm/testing.d.ts +120 -88
- package/lib/esm/testing.js +125 -38
- package/lib/esm/testing.js.map +1 -1
- package/lib/esm/transition-routines.d.ts +3 -0
- package/lib/esm/transition-routines.js +3 -0
- package/lib/esm/transition-routines.js.map +1 -0
- package/lib/esm/types.d.ts +5 -0
- package/lib/esm/types.js +2 -0
- package/lib/esm/types.js.map +1 -0
- package/package.json +22 -4
- package/lib/cjs/internal/defs.private.d.ts +0 -41
- package/lib/cjs/internal/defs.private.js.map +0 -1
- package/lib/cjs/internal/dispatch.debug.d.ts +0 -4
- package/lib/cjs/internal/dispatch.debug.js +0 -332
- package/lib/cjs/internal/dispatch.debug.js.map +0 -1
- package/lib/cjs/internal/dispatch.production.d.ts +0 -6
- package/lib/cjs/internal/dispatch.production.js +0 -241
- package/lib/cjs/internal/dispatch.production.js.map +0 -1
- package/lib/cjs/internal/dispatch.trace.d.ts +0 -4
- package/lib/cjs/internal/dispatch.trace.js +0 -418
- package/lib/cjs/internal/dispatch.trace.js.map +0 -1
- package/lib/cjs/internal/hsm.d.ts +0 -60
- package/lib/cjs/internal/hsm.js +0 -215
- package/lib/cjs/internal/hsm.js.map +0 -1
- package/lib/cjs/internal/lookup.d.ts +0 -15
- package/lib/cjs/internal/lookup.js +0 -32
- package/lib/cjs/internal/lookup.js.map +0 -1
- package/lib/cjs/internal/utils.d.ts +0 -26
- package/lib/cjs/internal/utils.js +0 -63
- package/lib/cjs/internal/utils.js.map +0 -1
- package/lib/esm/internal/defs.private.d.ts +0 -41
- package/lib/esm/internal/defs.private.js +0 -2
- package/lib/esm/internal/defs.private.js.map +0 -1
- package/lib/esm/internal/dispatch.debug.d.ts +0 -4
- package/lib/esm/internal/dispatch.debug.js +0 -328
- package/lib/esm/internal/dispatch.debug.js.map +0 -1
- package/lib/esm/internal/dispatch.production.d.ts +0 -6
- package/lib/esm/internal/dispatch.production.js +0 -237
- package/lib/esm/internal/dispatch.production.js.map +0 -1
- package/lib/esm/internal/dispatch.trace.d.ts +0 -4
- package/lib/esm/internal/dispatch.trace.js +0 -414
- package/lib/esm/internal/dispatch.trace.js.map +0 -1
- package/lib/esm/internal/hsm.d.ts +0 -60
- package/lib/esm/internal/hsm.js +0 -211
- package/lib/esm/internal/hsm.js.map +0 -1
- package/lib/esm/internal/lookup.d.ts +0 -15
- package/lib/esm/internal/lookup.js +0 -29
- package/lib/esm/internal/lookup.js.map +0 -1
- package/lib/esm/internal/utils.d.ts +0 -26
- package/lib/esm/internal/utils.js +0 -52
- package/lib/esm/internal/utils.js.map +0 -1
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal Pure types for the ihsm runtime — no runtime values in this module.
|
|
3
|
+
*/
|
|
4
|
+
import type { Machine, Port, RuntimeError, TopState as RuntimeTopState, TraceLevel, UnhandledEventError } from './runtime.js';
|
|
5
|
+
export type Any = Record<string, any>;
|
|
6
|
+
/** Optional trailing argument on generated service client methods. */
|
|
7
|
+
export type ServiceCallOptions = {
|
|
8
|
+
readonly timeoutMs?: number;
|
|
9
|
+
};
|
|
10
|
+
export interface ActorConfig {
|
|
11
|
+
context?: object;
|
|
12
|
+
services?: object;
|
|
13
|
+
notifications?: object;
|
|
14
|
+
internalServices?: object;
|
|
15
|
+
internalNotifications?: object;
|
|
16
|
+
port?: object;
|
|
17
|
+
}
|
|
18
|
+
export type ActorContextOf<C extends ActorConfig> = C extends {
|
|
19
|
+
context: infer Context;
|
|
20
|
+
} ? Context : Any;
|
|
21
|
+
export type ActorServicesOf<C extends ActorConfig> = C extends {
|
|
22
|
+
services: infer S extends object;
|
|
23
|
+
} ? S : {};
|
|
24
|
+
export type ActorNotificationsOf<C extends ActorConfig> = C extends {
|
|
25
|
+
notifications: infer N extends object;
|
|
26
|
+
} ? N : {};
|
|
27
|
+
export type ActorInternalServicesOf<C extends ActorConfig> = C extends {
|
|
28
|
+
internalServices: infer S extends object;
|
|
29
|
+
} ? S : {};
|
|
30
|
+
export type ActorInternalNotificationsOf<C extends ActorConfig> = C extends {
|
|
31
|
+
internalNotifications: infer N extends object;
|
|
32
|
+
} ? N : {};
|
|
33
|
+
export type DomainPortOf<C extends ActorConfig> = C extends {
|
|
34
|
+
port: infer P extends object;
|
|
35
|
+
} ? P : {};
|
|
36
|
+
export type ActorPortOf<C extends ActorConfig> = C extends {
|
|
37
|
+
port: infer P extends object;
|
|
38
|
+
} ? P & PortServices<C> : IPort<C>;
|
|
39
|
+
export type ActorMethodKeysOf<C extends ActorConfig> = keyof ActorServicesOf<C> | keyof ActorNotificationsOf<C> | keyof ActorInternalServicesOf<C> | keyof ActorInternalNotificationsOf<C>;
|
|
40
|
+
export type ProtocolBucket = 'services' | 'notifications' | 'internalServices' | 'internalNotifications';
|
|
41
|
+
export type StateClassOf<C extends ActorConfig = ActorConfig> = new (...args: any[]) => RuntimeTopState<C>;
|
|
42
|
+
export type TopStateArg<C extends ActorConfig = ActorConfig> = StateClassOf<C>;
|
|
43
|
+
export type ActorConfigOf<T> = T extends new (...args: any[]) => RuntimeTopState<infer C extends ActorConfig> ? C : T extends TopStateArg<infer C> ? C : T extends abstract new (...args: never[]) => RuntimeTopState<infer C extends ActorConfig> ? C : T extends new (...args: any[]) => infer Inst ? Inst extends {
|
|
44
|
+
readonly hsm: HandlerHsm<infer C>;
|
|
45
|
+
} ? C : Inst extends {
|
|
46
|
+
readonly ctx: infer Context;
|
|
47
|
+
} ? [Context] extends [ActorContextOf<infer C>] ? C : ActorConfig : ActorConfig : T extends {
|
|
48
|
+
readonly ctx: infer Context;
|
|
49
|
+
} ? [Context] extends [ActorContextOf<infer C>] ? C : ActorConfig : ActorConfig;
|
|
50
|
+
/** Top state constructor whose {@link ActorConfigOf} passes {@link DisjointActorConfig}. */
|
|
51
|
+
export type ValidatedTopStateArg<T extends TopStateArg<ActorConfig>> = DisjointActorConfig<ActorConfigOf<T>> extends true ? T : never;
|
|
52
|
+
export type MachineContext<T> = ActorContextOf<ActorConfigOf<T>>;
|
|
53
|
+
export type MachinePublic<T> = ActorServicesOf<ActorConfigOf<T>> & ActorNotificationsOf<ActorConfigOf<T>>;
|
|
54
|
+
export type MachineInternal<T> = ActorInternalServicesOf<ActorConfigOf<T>> & ActorInternalNotificationsOf<ActorConfigOf<T>>;
|
|
55
|
+
export type MachinePort<T> = ActorPortOf<ActorConfigOf<T>>;
|
|
56
|
+
export type ReservedName = 'ctx' | 'hsm' | 'notify' | 'notifyNow' | 'onEntry' | 'onExit' | 'onError' | 'onUnhandled';
|
|
57
|
+
export type IsReservedName<K extends PropertyKey> = K extends ReservedName ? true : false;
|
|
58
|
+
export type FilterReservedKeys<O extends object> = {
|
|
59
|
+
[K in keyof O as IsReservedName<K> extends true ? never : K]: O[K];
|
|
60
|
+
};
|
|
61
|
+
export type ServiceHandler<Reply> = Reply | Promise<Reply>;
|
|
62
|
+
export type NotificationHandler = void | Promise<void>;
|
|
63
|
+
export type AssertAsyncService<M> = M extends (...args: never[]) => infer R ? (R extends Promise<unknown> ? M : ['ihsm: service members must return Promise<Reply>', M]) : M;
|
|
64
|
+
export type ServiceArgs<S extends object, K extends keyof S> = S[K] extends (...args: infer A) => Promise<unknown> ? A : never;
|
|
65
|
+
export type ServiceReply<S extends object, K extends keyof S> = S[K] extends (...args: never[]) => Promise<infer R> ? Awaited<R> : never;
|
|
66
|
+
export type NotificationArgs<N extends object, K extends keyof N> = N[K] extends (...args: infer A) => void | Promise<void> ? A : never;
|
|
67
|
+
type BucketKeys<B> = {} extends B ? never : keyof B;
|
|
68
|
+
type ProtocolFieldKeys<C extends ActorConfig> = BucketKeys<ActorServicesOf<C>> | BucketKeys<ActorNotificationsOf<C>> | BucketKeys<ActorInternalServicesOf<C>> | BucketKeys<ActorInternalNotificationsOf<C>>;
|
|
69
|
+
export type OverlappingProtocolKeys<C extends ActorConfig> = Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorNotificationsOf<C>>, PropertyKey> extends never ? Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorInternalServicesOf<C>>, PropertyKey> extends never ? Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey> extends never ? Extract<BucketKeys<ActorNotificationsOf<C>> & BucketKeys<ActorInternalServicesOf<C>>, PropertyKey> extends never ? Extract<BucketKeys<ActorNotificationsOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey> extends never ? Extract<BucketKeys<ActorInternalServicesOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey> extends never ? Extract<ProtocolFieldKeys<C>, ReservedName> extends never ? true : ['ihsm: protocol keys must not use reserved names', Extract<ProtocolFieldKeys<C>, ReservedName>] : ['ihsm: internalServices and internalNotifications share keys', Extract<BucketKeys<ActorInternalServicesOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey>] : ['ihsm: notifications and internalNotifications share keys', Extract<BucketKeys<ActorNotificationsOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey>] : ['ihsm: notifications and internalServices share keys', Extract<BucketKeys<ActorNotificationsOf<C>> & BucketKeys<ActorInternalServicesOf<C>>, PropertyKey>] : ['ihsm: services and internalNotifications share keys', Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorInternalNotificationsOf<C>>, PropertyKey>] : ['ihsm: services and internalServices share keys', Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorInternalServicesOf<C>>, PropertyKey>] : ['ihsm: services and notifications share keys', Extract<BucketKeys<ActorServicesOf<C>> & BucketKeys<ActorNotificationsOf<C>>, PropertyKey>];
|
|
70
|
+
export type DisjointActorConfig<C extends ActorConfig> = OverlappingProtocolKeys<C>;
|
|
71
|
+
export type ValidatedActorConfig<C extends ActorConfig> = DisjointActorConfig<C> extends true ? C : never;
|
|
72
|
+
export interface DispatchErrorCallback<C extends ActorConfig = ActorConfig> {
|
|
73
|
+
(hsm: Properties<C>, err: Error): void;
|
|
74
|
+
}
|
|
75
|
+
export interface TraceWriter {
|
|
76
|
+
write<C extends ActorConfig>(hsm: Properties<C>, msg: any): void;
|
|
77
|
+
}
|
|
78
|
+
export interface Properties<C extends ActorConfig = ActorConfig> {
|
|
79
|
+
currentState: StateClass<C>;
|
|
80
|
+
readonly currentStateName: string;
|
|
81
|
+
readonly topState: StateClass<C>;
|
|
82
|
+
readonly topStateName: string;
|
|
83
|
+
readonly ctxTypeName: string;
|
|
84
|
+
readonly traceHeader: string;
|
|
85
|
+
readonly eventName: string;
|
|
86
|
+
readonly eventPayload: unknown[];
|
|
87
|
+
traceLevel: TraceLevel;
|
|
88
|
+
traceWriter: TraceWriter;
|
|
89
|
+
dispatchErrorCallback: DispatchErrorCallback<C>;
|
|
90
|
+
readonly actorUuid: string;
|
|
91
|
+
readonly actorName: string;
|
|
92
|
+
readonly actorPath: string;
|
|
93
|
+
}
|
|
94
|
+
export type StateClass<C extends ActorConfig = ActorConfig> = StateClassOf<C>;
|
|
95
|
+
export interface Disposable {
|
|
96
|
+
dispose(): void;
|
|
97
|
+
}
|
|
98
|
+
export interface ResultWithSubscription<Result> {
|
|
99
|
+
readonly value: Result;
|
|
100
|
+
readonly subscription: Disposable;
|
|
101
|
+
}
|
|
102
|
+
export interface TracedMessage {
|
|
103
|
+
readonly event: string;
|
|
104
|
+
readonly payload: readonly unknown[];
|
|
105
|
+
}
|
|
106
|
+
export type EventObserver = (message: TracedMessage) => void;
|
|
107
|
+
/** Parent-machine link on {@link ParentActor} and optional `parent` on actor shells. */
|
|
108
|
+
export declare const kParentLink: unique symbol;
|
|
109
|
+
/** Active {@link Machine} on handler instances — set in {@link Machine} constructor. */
|
|
110
|
+
export declare const kHandlerMachine: unique symbol;
|
|
111
|
+
export type ParentActor<T extends TopStateArg = TopStateArg> = {
|
|
112
|
+
readonly top: T;
|
|
113
|
+
readonly [kParentLink]?: Machine<ActorConfigOf<T>>;
|
|
114
|
+
};
|
|
115
|
+
/** Optional parent link on actors created via {@link makeChildActor}. */
|
|
116
|
+
export type ActorParentField<ParentT extends TopStateArg = TopStateArg> = {
|
|
117
|
+
readonly parent?: ParentActor<ParentT>;
|
|
118
|
+
};
|
|
119
|
+
export interface RandomService {
|
|
120
|
+
/** Pseudorandom number in `[0, 1)`. */
|
|
121
|
+
random(): number;
|
|
122
|
+
/** Cryptographic-quality random in `[0, 1)` when the platform provides it. */
|
|
123
|
+
cryptoRandom(): number;
|
|
124
|
+
/** Generate a UUID v4 string. */
|
|
125
|
+
randomUUID(): string;
|
|
126
|
+
/** Fill `array` with cryptographically strong random bytes. */
|
|
127
|
+
getRandomValues<T extends ArrayBufferView>(array: T): T;
|
|
128
|
+
}
|
|
129
|
+
export interface TimerService {
|
|
130
|
+
/** Schedule a one-shot callback after `millis` milliseconds. */
|
|
131
|
+
setTimeout(callback: () => void, millis?: number): number;
|
|
132
|
+
/** Cancel a timer previously returned by {@link TimerService.setTimeout}. */
|
|
133
|
+
clearTimeout(id: number | undefined): void;
|
|
134
|
+
/** Schedule a repeating callback every `millis` milliseconds. */
|
|
135
|
+
setInterval(callback: () => void, millis?: number): number;
|
|
136
|
+
/** Cancel an interval previously returned by {@link TimerService.setInterval}. */
|
|
137
|
+
clearInterval(id: number | undefined): void;
|
|
138
|
+
}
|
|
139
|
+
export type ServiceClient<S extends object> = {
|
|
140
|
+
[K in keyof S]: S[K] extends (...args: infer A) => Promise<infer R> ? (...args: [...A, ServiceCallOptions?]) => Promise<R> : never;
|
|
141
|
+
};
|
|
142
|
+
export type NotificationClient<N extends object> = {
|
|
143
|
+
[K in keyof N]: N[K] extends (...args: infer A) => void | Promise<void> ? (...args: A) => void : never;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Faceted call surface — `actor.call.<service>(...)` returns `Promise<Reply>`.
|
|
147
|
+
* Delivery mode is chosen by the facet at the call site, not inferred.
|
|
148
|
+
*/
|
|
149
|
+
export type CallFacet<S extends object> = ServiceClient<S>;
|
|
150
|
+
/** Faceted notification surface — `actor.notify.<event>(...)` / `actor.notifyNow.<event>(...)`. */
|
|
151
|
+
export type NotifyFacet<N extends object> = NotificationClient<N>;
|
|
152
|
+
export type ActorHsm<_C extends ActorConfig = ActorConfig> = {
|
|
153
|
+
sync(): Promise<void>;
|
|
154
|
+
currentStateName: string;
|
|
155
|
+
topStateName: string;
|
|
156
|
+
traceLevel: TraceLevel;
|
|
157
|
+
traceWriter: TraceWriter;
|
|
158
|
+
traceHeader: string;
|
|
159
|
+
readonly id: string;
|
|
160
|
+
readonly actorUuid: string;
|
|
161
|
+
readonly actorName: string;
|
|
162
|
+
readonly actorPath: string;
|
|
163
|
+
};
|
|
164
|
+
export type TestActorHsm<C extends ActorConfig = ActorConfig> = ActorHsm<C> & {
|
|
165
|
+
currentState: StateClassOf<C>;
|
|
166
|
+
topState: StateClassOf<C>;
|
|
167
|
+
};
|
|
168
|
+
export type OwnerActorHsm<C extends ActorConfig = ActorConfig> = TestActorHsm<C> & {
|
|
169
|
+
restore(state: StateClassOf<C>, ctx: ActorContextOf<C>): void;
|
|
170
|
+
dispatchErrorCallback: DispatchErrorCallback<C>;
|
|
171
|
+
};
|
|
172
|
+
export type ExternalActor<C extends ActorConfig = ActorConfig> = ActorParentField & {
|
|
173
|
+
readonly id: string;
|
|
174
|
+
readonly notify: NotifyFacet<ActorNotificationsOf<C>>;
|
|
175
|
+
readonly notifyNow: NotifyFacet<ActorNotificationsOf<C>>;
|
|
176
|
+
readonly call: CallFacet<ActorServicesOf<C>>;
|
|
177
|
+
readonly hsm: ActorHsm<C>;
|
|
178
|
+
};
|
|
179
|
+
export type ExternalHsm<C extends ActorConfig = ActorConfig> = ExternalActor<C>['hsm'];
|
|
180
|
+
export type InboundActor<C extends ActorConfig = ActorConfig> = ActorParentField & {
|
|
181
|
+
readonly id: string;
|
|
182
|
+
readonly notify: NotifyFacet<ActorNotificationsOf<C> & ActorInternalNotificationsOf<C>>;
|
|
183
|
+
readonly notifyNow: NotifyFacet<ActorNotificationsOf<C> & ActorInternalNotificationsOf<C>>;
|
|
184
|
+
readonly call: CallFacet<ActorServicesOf<C>>;
|
|
185
|
+
readonly hsm: TestActorHsm<C>;
|
|
186
|
+
};
|
|
187
|
+
export type InboundHsm<C extends ActorConfig = ActorConfig> = InboundActor<C>['hsm'];
|
|
188
|
+
export type ChildActor<C extends ActorConfig = ActorConfig> = Omit<InboundActor<C>, 'call' | 'hsm'> & {
|
|
189
|
+
readonly call: CallFacet<ActorServicesOf<C> & ActorInternalServicesOf<C>>;
|
|
190
|
+
readonly hsm: OwnerActorHsm<C>;
|
|
191
|
+
};
|
|
192
|
+
export type ChildHsm<C extends ActorConfig = ActorConfig> = ChildActor<C>['hsm'];
|
|
193
|
+
export type SelfNotifications<C extends ActorConfig = ActorConfig> = NotificationClient<ActorNotificationsOf<C>> & NotificationClient<ActorInternalNotificationsOf<C>>;
|
|
194
|
+
/** Timer, random, and deferred self-notifications — always on {@link ActorPortOf}. */
|
|
195
|
+
export type PortServices<C extends ActorConfig = ActorConfig> = TimerService & RandomService & {
|
|
196
|
+
defer(ms: number): SelfNotifications<C>;
|
|
197
|
+
};
|
|
198
|
+
/** Runtime port — timers, randomness, deferred notifications, and {@link IPort.actor}. */
|
|
199
|
+
export interface IPort<C extends ActorConfig = ActorConfig> extends TimerService, RandomService {
|
|
200
|
+
actor: InboundActor<C> | ChildActor<C>;
|
|
201
|
+
defer(ms: number): SelfNotifications<C>;
|
|
202
|
+
}
|
|
203
|
+
/** Port instance passed to a factory before {@link IPort.actor} is bound. */
|
|
204
|
+
export type UntypedPortInput = Port<TopStateArg>;
|
|
205
|
+
export type MachinePortInput<C extends ActorConfig = ActorConfig> = IPort<C> | UntypedPortInput;
|
|
206
|
+
export type HandlerHsm<C extends ActorConfig = ActorConfig> = {
|
|
207
|
+
ctx: ActorContextOf<C>;
|
|
208
|
+
transition(next: StateClassOf<C>): void;
|
|
209
|
+
port: ActorPortOf<C>;
|
|
210
|
+
unhandled(): never;
|
|
211
|
+
eventName: string;
|
|
212
|
+
eventPayload: unknown[];
|
|
213
|
+
currentState: StateClassOf<C>;
|
|
214
|
+
currentStateName: string;
|
|
215
|
+
topState: StateClassOf<C>;
|
|
216
|
+
topStateName: string;
|
|
217
|
+
traceHeader: string;
|
|
218
|
+
/** Structured live domain stack (`ihsm.domain.path`) — derived from the trace header (§4.10.3). */
|
|
219
|
+
readonly traceFrames: readonly TraceFrame[];
|
|
220
|
+
/** Severity-typed handler logger — emits trace-correlated OTEL logs (§4.10.1). */
|
|
221
|
+
readonly log: ActorLogger;
|
|
222
|
+
traceLevel: TraceLevel;
|
|
223
|
+
traceWriter: TraceWriter;
|
|
224
|
+
dispatchErrorCallback: DispatchErrorCallback<C>;
|
|
225
|
+
readonly id: string;
|
|
226
|
+
readonly actorUuid: string;
|
|
227
|
+
readonly actorName: string;
|
|
228
|
+
readonly actorPath: string;
|
|
229
|
+
};
|
|
230
|
+
/** Structural root state instance — merged with the runtime {@link TopState} class. */
|
|
231
|
+
export interface TopState<C extends ActorConfig = ActorConfig> {
|
|
232
|
+
readonly ctx: ActorContextOf<C>;
|
|
233
|
+
readonly hsm: HandlerHsm<C>;
|
|
234
|
+
/** Self-directed default-queue notifications — `this.notify.x()`. */
|
|
235
|
+
readonly notify: SelfNotifications<C>;
|
|
236
|
+
/** Self-directed priority-queue notifications — `this.notifyNow.x()`. */
|
|
237
|
+
readonly notifyNow: SelfNotifications<C>;
|
|
238
|
+
}
|
|
239
|
+
export type TestHsm<C extends ActorConfig = ActorConfig> = ChildHsm<C> & {
|
|
240
|
+
port: ActorPortOf<C>;
|
|
241
|
+
subscribe(observer: (message: {
|
|
242
|
+
event: string;
|
|
243
|
+
payload: unknown[];
|
|
244
|
+
}) => void): {
|
|
245
|
+
dispose(): void;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
export type EmbodimentKind = 'root' | 'inbound' | 'child' | 'test';
|
|
249
|
+
export interface StateEvents<C extends ActorConfig = ActorConfig> {
|
|
250
|
+
onExit(): Promise<void> | void;
|
|
251
|
+
onEntry(): Promise<void> | void;
|
|
252
|
+
onError(error: RuntimeError<C>): Promise<void> | void;
|
|
253
|
+
onUnhandled(error: UnhandledEventError<C>): Promise<void> | void;
|
|
254
|
+
}
|
|
255
|
+
/** Machine snapshot passed to error constructors at failure time. */
|
|
256
|
+
export type ErrorHost<C extends ActorConfig = ActorConfig> = {
|
|
257
|
+
readonly ctx: ActorContextOf<C>;
|
|
258
|
+
readonly topStateName: string;
|
|
259
|
+
readonly currentStateName: string;
|
|
260
|
+
readonly eventName: string;
|
|
261
|
+
readonly eventPayload: unknown[];
|
|
262
|
+
};
|
|
263
|
+
/** @internal Per-actor runtime instance bag (context, handler facade, port). */
|
|
264
|
+
export interface Instance<C extends ActorConfig> {
|
|
265
|
+
ctx: ActorContextOf<C>;
|
|
266
|
+
hsm: HsmWithTracing<C>;
|
|
267
|
+
portRef?: unknown;
|
|
268
|
+
}
|
|
269
|
+
/** @internal Compiled transition between two states. */
|
|
270
|
+
export interface Transition<C extends ActorConfig> {
|
|
271
|
+
execute(hsm: HsmWithTracing<C>, srcState: StateClassOf<C>, dstState: StateClassOf<C>): Promise<void>;
|
|
272
|
+
}
|
|
273
|
+
export type DoneCallback = () => void;
|
|
274
|
+
/** @internal Run-to-completion work unit queued on the machine. */
|
|
275
|
+
export type Task = (done: DoneCallback) => void;
|
|
276
|
+
export interface ActorIdentity {
|
|
277
|
+
readonly uuid: string;
|
|
278
|
+
readonly name: string;
|
|
279
|
+
readonly path: string;
|
|
280
|
+
readonly kind: EmbodimentKind;
|
|
281
|
+
readonly parentUuid?: string;
|
|
282
|
+
}
|
|
283
|
+
export type TriggerKind = 'external' | 'call' | 'self' | 'actor' | 'timer' | 'init';
|
|
284
|
+
export type DispatchPhase = 'lookup' | 'handler' | 'transition' | 'onEntry' | 'onExit' | 'unhandled' | 'initialize';
|
|
285
|
+
export interface MacrostepBegin {
|
|
286
|
+
readonly id: string;
|
|
287
|
+
readonly actor: ActorIdentity;
|
|
288
|
+
readonly trigger: string;
|
|
289
|
+
readonly triggerKind: TriggerKind;
|
|
290
|
+
readonly startState: string;
|
|
291
|
+
readonly cause?: CauseRef;
|
|
292
|
+
/** For `timer` triggers — the scheduled delay, surfaced on the `ihsm.link.kind=timer` link (§4.7.5). */
|
|
293
|
+
readonly delayMs?: number;
|
|
294
|
+
}
|
|
295
|
+
export interface MacrostepEnd {
|
|
296
|
+
readonly id: string;
|
|
297
|
+
readonly endState: string;
|
|
298
|
+
readonly steps: number;
|
|
299
|
+
readonly transitioned: boolean;
|
|
300
|
+
readonly outcome: 'ok' | 'error';
|
|
301
|
+
}
|
|
302
|
+
export interface MicrostepBegin {
|
|
303
|
+
readonly macrostepId: string;
|
|
304
|
+
readonly seq: number;
|
|
305
|
+
readonly event: string;
|
|
306
|
+
readonly bucket: ProtocolBucket;
|
|
307
|
+
readonly queue: NotificationQueue;
|
|
308
|
+
readonly fromState: string;
|
|
309
|
+
readonly handlerState?: string;
|
|
310
|
+
readonly cause?: CauseRef;
|
|
311
|
+
}
|
|
312
|
+
export interface MicrostepEnd {
|
|
313
|
+
readonly macrostepId: string;
|
|
314
|
+
readonly seq: number;
|
|
315
|
+
readonly toState: string;
|
|
316
|
+
readonly transitioned: boolean;
|
|
317
|
+
readonly async: boolean;
|
|
318
|
+
readonly outcome: 'ok' | 'error';
|
|
319
|
+
}
|
|
320
|
+
export interface CauseRef {
|
|
321
|
+
readonly actorUuid: string;
|
|
322
|
+
readonly macrostepId?: string;
|
|
323
|
+
readonly stepSeq?: number;
|
|
324
|
+
readonly kind: 'cause' | 'timer' | 'message' | 'spawn' | 'wire';
|
|
325
|
+
carrier?: Record<string, string>;
|
|
326
|
+
}
|
|
327
|
+
export interface SpawnInfo {
|
|
328
|
+
readonly parent: CauseRef;
|
|
329
|
+
readonly child: ActorIdentity;
|
|
330
|
+
}
|
|
331
|
+
export interface PortCallBegin {
|
|
332
|
+
readonly callId: number;
|
|
333
|
+
readonly method: string;
|
|
334
|
+
readonly cause?: CauseRef;
|
|
335
|
+
}
|
|
336
|
+
export interface PortCallEnd {
|
|
337
|
+
readonly callId: number;
|
|
338
|
+
readonly method: string;
|
|
339
|
+
readonly outcome: 'ok' | 'error';
|
|
340
|
+
readonly error?: Error;
|
|
341
|
+
}
|
|
342
|
+
export interface OutboundCallBegin {
|
|
343
|
+
readonly callId: number;
|
|
344
|
+
readonly service: string;
|
|
345
|
+
readonly targetUuid?: string;
|
|
346
|
+
readonly cause?: CauseRef;
|
|
347
|
+
}
|
|
348
|
+
export interface OutboundCallEnd {
|
|
349
|
+
readonly callId: number;
|
|
350
|
+
readonly service: string;
|
|
351
|
+
readonly outcome: 'ok' | 'error';
|
|
352
|
+
readonly error?: Error;
|
|
353
|
+
}
|
|
354
|
+
export interface EnqueueInfo {
|
|
355
|
+
readonly event: string;
|
|
356
|
+
readonly queue: NotificationQueue;
|
|
357
|
+
readonly delayMs?: number;
|
|
358
|
+
readonly targetUuid?: string;
|
|
359
|
+
cause: CauseRef;
|
|
360
|
+
}
|
|
361
|
+
export interface DispatchError {
|
|
362
|
+
readonly phase: DispatchPhase;
|
|
363
|
+
readonly errorClass: string;
|
|
364
|
+
readonly error: Error;
|
|
365
|
+
readonly recovered: boolean;
|
|
366
|
+
}
|
|
367
|
+
export interface TraceFrame {
|
|
368
|
+
readonly name: string;
|
|
369
|
+
readonly kind: 'event' | 'handler' | 'transition' | 'onEntry' | 'onExit' | 'port' | 'service' | 'initialize';
|
|
370
|
+
}
|
|
371
|
+
export type LogAttributes = Record<string, string | number | boolean>;
|
|
372
|
+
export interface LogRecord {
|
|
373
|
+
readonly severity: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
374
|
+
readonly body: string;
|
|
375
|
+
readonly attributes?: LogAttributes;
|
|
376
|
+
readonly frames: readonly TraceFrame[];
|
|
377
|
+
readonly error?: Error;
|
|
378
|
+
readonly source: 'user' | 'runtime';
|
|
379
|
+
}
|
|
380
|
+
export interface ActorLogger {
|
|
381
|
+
trace(message: string, attributes?: LogAttributes): void;
|
|
382
|
+
debug(message: string, attributes?: LogAttributes): void;
|
|
383
|
+
info(message: string, attributes?: LogAttributes): void;
|
|
384
|
+
warn(message: string, attributes?: LogAttributes): void;
|
|
385
|
+
error(message: string | Error, attributes?: LogAttributes): void;
|
|
386
|
+
fatal(message: string | Error, attributes?: LogAttributes): void;
|
|
387
|
+
}
|
|
388
|
+
export interface Instrumentation<_C extends ActorConfig = ActorConfig> {
|
|
389
|
+
onActorCreated?(id: ActorIdentity): void;
|
|
390
|
+
onActorSpawned?(info: SpawnInfo): void;
|
|
391
|
+
onActorDisposed?(id: ActorIdentity): void;
|
|
392
|
+
onMacrostepBegin?(info: MacrostepBegin): void;
|
|
393
|
+
onMacrostepEnd?(info: MacrostepEnd): void;
|
|
394
|
+
onMicrostepBegin?(info: MicrostepBegin): void;
|
|
395
|
+
onMicrostepEnd?(info: MicrostepEnd): void;
|
|
396
|
+
onPortCallBegin?(info: PortCallBegin): void;
|
|
397
|
+
onPortCallEnd?(info: PortCallEnd): void;
|
|
398
|
+
onOutboundCallBegin?(info: OutboundCallBegin): void;
|
|
399
|
+
onOutboundCallEnd?(info: OutboundCallEnd): void;
|
|
400
|
+
onEnqueue?(info: EnqueueInfo): void;
|
|
401
|
+
onError?(info: DispatchError): void;
|
|
402
|
+
onLog?(record: LogRecord): void;
|
|
403
|
+
transition?: TransitionTracer;
|
|
404
|
+
}
|
|
405
|
+
export interface ActorOptions<C extends ActorConfig = ActorConfig> {
|
|
406
|
+
initialize?: boolean;
|
|
407
|
+
traceLevel?: TraceLevel;
|
|
408
|
+
traceWriter?: TraceWriter;
|
|
409
|
+
dispatchErrorCallback?: DispatchErrorCallback<C>;
|
|
410
|
+
transitions?: TransitionResolver<C>;
|
|
411
|
+
}
|
|
412
|
+
/** Machine host passed to {@link executeTransitionRoutine}. */
|
|
413
|
+
export interface TransitionHost<C extends ActorConfig = ActorConfig> {
|
|
414
|
+
readonly ctx: ActorContextOf<C>;
|
|
415
|
+
readonly currentStateName: string;
|
|
416
|
+
readonly topState: StateClassOf<C>;
|
|
417
|
+
readonly topStateName: string;
|
|
418
|
+
readonly ctxTypeName: string;
|
|
419
|
+
readonly traceHeader: string;
|
|
420
|
+
readonly eventName: string;
|
|
421
|
+
readonly eventPayload: unknown[];
|
|
422
|
+
currentState: StateClassOf<C>;
|
|
423
|
+
}
|
|
424
|
+
/** @internal Runtime host passed to transition execution and dispatch tasks. */
|
|
425
|
+
export interface HsmWithTracing<C extends ActorConfig = ActorConfig> extends TransitionHost<C> {
|
|
426
|
+
readonly actorUuid: string;
|
|
427
|
+
readonly actorName: string;
|
|
428
|
+
readonly actorPath: string;
|
|
429
|
+
traceLevel: TraceLevel;
|
|
430
|
+
traceWriter: TraceWriter;
|
|
431
|
+
dispatchErrorCallback: DispatchErrorCallback<C>;
|
|
432
|
+
reportDispatchError(err: Error): void;
|
|
433
|
+
sync(): Promise<void>;
|
|
434
|
+
restore(state: StateClassOf<C>, ctx: ActorContextOf<C>): void;
|
|
435
|
+
transition(nextState: StateClassOf<C>): void;
|
|
436
|
+
unhandled(): never;
|
|
437
|
+
_instance: Instance<C>;
|
|
438
|
+
_transitionState?: StateClassOf<C>;
|
|
439
|
+
_currentEventName?: string;
|
|
440
|
+
_currentEventPayload?: unknown[];
|
|
441
|
+
_tracePush(domain: string, msg: string): void;
|
|
442
|
+
_tracePopDone(msg: string): void;
|
|
443
|
+
_tracePopError(msg: string): void;
|
|
444
|
+
_traceWrite(msg: any): void;
|
|
445
|
+
pushTask(t: Task): void;
|
|
446
|
+
unshiftHiPriorityTask(t: Task): void;
|
|
447
|
+
pushHiPriorityTask(t: Task): void;
|
|
448
|
+
}
|
|
449
|
+
export interface ProtocolSlot {
|
|
450
|
+
readonly bucket: ProtocolBucket;
|
|
451
|
+
readonly name: string;
|
|
452
|
+
}
|
|
453
|
+
export interface ProtocolIndex {
|
|
454
|
+
readonly slots: ReadonlyMap<string, ProtocolSlot>;
|
|
455
|
+
get(name: string): ProtocolSlot | undefined;
|
|
456
|
+
entries(kind: EmbodimentKind): Iterable<[string, ProtocolSlot]>;
|
|
457
|
+
}
|
|
458
|
+
/** @internal Machine surface used by generated actor handles and dispatch. */
|
|
459
|
+
export interface DispatchableMachine {
|
|
460
|
+
dispatchService(name: string, args: unknown[]): Promise<unknown>;
|
|
461
|
+
dispatchNotification(name: string, args: unknown[], queue: NotificationQueue): void;
|
|
462
|
+
unshiftHiPriorityTask(t: (done: () => void) => void): void;
|
|
463
|
+
readonly ctx: unknown;
|
|
464
|
+
readonly actorUuid: string;
|
|
465
|
+
actorHsmFor(kind: EmbodimentKind): unknown;
|
|
466
|
+
/**
|
|
467
|
+
* Timer service used to arm service-call timeouts. The actor's bound port when it provides a
|
|
468
|
+
* timer service (so a virtual clock governs `timeoutMs` deterministically), otherwise
|
|
469
|
+
* `undefined` (the runtime falls back to the host timer).
|
|
470
|
+
*/
|
|
471
|
+
readonly callTimer?: TimerService;
|
|
472
|
+
}
|
|
473
|
+
export type NotificationQueue = 'default' | 'priority' | 'timer';
|
|
474
|
+
export interface TransitionResolver<C extends ActorConfig = ActorConfig> {
|
|
475
|
+
resolve(src: StateClassOf<C>, dest: StateClassOf<C>): Transition<C>;
|
|
476
|
+
}
|
|
477
|
+
export interface PlannedTransition<C extends ActorConfig = ActorConfig> {
|
|
478
|
+
readonly exit: readonly StateClassOf<C>[];
|
|
479
|
+
readonly entry: readonly StateClassOf<C>[];
|
|
480
|
+
readonly finalState?: StateClassOf<C>;
|
|
481
|
+
}
|
|
482
|
+
export interface TransitionRoutinePlan<C extends ActorConfig = ActorConfig> extends PlannedTransition<C> {
|
|
483
|
+
readonly from: StateClassOf<C>;
|
|
484
|
+
readonly to: StateClassOf<C>;
|
|
485
|
+
}
|
|
486
|
+
export interface TransitionTracer {
|
|
487
|
+
traceTransitionStart(fromStateName: string, toStateName: string): void;
|
|
488
|
+
traceInitializeStart?(stateName: string): void;
|
|
489
|
+
traceInitializeDone?(finalStateName: string): void;
|
|
490
|
+
/** Optional: fired just before a real (non-default) `onExit`/`onEntry` runs — lets a structural tracer bracket the hook as a child span. */
|
|
491
|
+
traceHookStart?(stateName: string, hook: 'onExit' | 'onEntry'): void;
|
|
492
|
+
traceHookDone(stateName: string, hook: 'onExit' | 'onEntry'): void;
|
|
493
|
+
traceHookSkipped(stateName: string, hook: 'onExit' | 'onEntry'): void;
|
|
494
|
+
traceHookError(stateName: string, hook: 'onExit' | 'onEntry', cause: unknown): void;
|
|
495
|
+
traceTransitionDone(finalStateName: string): void;
|
|
496
|
+
}
|
|
497
|
+
export type TransitionRoutineStyle = 'production' | 'debug' | 'verbose';
|
|
498
|
+
export interface TransitionRoutineExecuteOptions<C extends ActorConfig = ActorConfig> {
|
|
499
|
+
readonly style?: TransitionRoutineStyle;
|
|
500
|
+
readonly tracer?: TransitionTracer;
|
|
501
|
+
readonly setCurrentState?: (state: StateClassOf<C>) => void;
|
|
502
|
+
/**
|
|
503
|
+
* Emit hook (`onExit`/`onEntry`) tracer callbacks regardless of `style`. Set when the tracer is a
|
|
504
|
+
* structural instrumentation seam (not the console tracer), whose entry/exit spans are not
|
|
505
|
+
* `TraceLevel`-gated (spec §4.8). Default `false` — console tracer keeps its verbose-only gating.
|
|
506
|
+
*/
|
|
507
|
+
readonly hookEvents?: boolean;
|
|
508
|
+
}
|
|
509
|
+
export type TransitionTraceHost = {
|
|
510
|
+
_tracePush(domain: string, msg: string): void;
|
|
511
|
+
_traceWrite(msg: string): void;
|
|
512
|
+
_tracePopDone(msg: string): void;
|
|
513
|
+
_tracePopError(msg: string): void;
|
|
514
|
+
};
|
|
515
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Parent-machine link on {@link ParentActor} and optional `parent` on actor shells. */
|
|
2
|
+
export const kParentLink = Symbol('ihsm.parentLink');
|
|
3
|
+
/** Active {@link Machine} on handler instances — set in {@link Machine} constructor. */
|
|
4
|
+
export const kHandlerMachine = Symbol('ihsm.handlerMachine');
|
|
5
|
+
//#endregion
|
|
6
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/internal/types.ts"],"names":[],"mappings":"AAiJA,wFAAwF;AACxF,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAErD,wFAAwF;AACxF,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AA6d7D,YAAY"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ProtocolIndex } from './internal/types.js';
|
|
2
|
+
export declare function cacheProtocolIndex(topState: object, index: ProtocolIndex): ProtocolIndex;
|
|
3
|
+
export declare function protocolIndexFor(topState: object): ProtocolIndex | undefined;
|
|
4
|
+
export declare function disableDispatchStorage(): void;
|
|
5
|
+
export declare function resetDispatchStorage(): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @internal Test hooks — not part of the public API. */
|
|
2
|
+
import { cacheProtocolIndex as writeProtocolIndexCache, dispatchContext, protocolIndexFor as readProtocolIndexFor } from './internal/runtime.js';
|
|
3
|
+
export function cacheProtocolIndex(topState, index) {
|
|
4
|
+
return writeProtocolIndexCache(topState, index);
|
|
5
|
+
}
|
|
6
|
+
export function protocolIndexFor(topState) {
|
|
7
|
+
return readProtocolIndexFor(topState);
|
|
8
|
+
}
|
|
9
|
+
export function disableDispatchStorage() {
|
|
10
|
+
dispatchContext.markUnavailable();
|
|
11
|
+
}
|
|
12
|
+
export function resetDispatchStorage() {
|
|
13
|
+
dispatchContext.resetInit();
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=test-only.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-only.js","sourceRoot":"","sources":["../../src/test-only.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,OAAO,EAAE,kBAAkB,IAAI,uBAAuB,EAAE,eAAe,EAAE,gBAAgB,IAAI,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG9I,MAAM,UAAU,kBAAkB,CAAC,QAAgB,EAAE,KAAoB;IACxE,OAAO,uBAAuB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AACjD,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,QAAgB;IAChD,OAAO,oBAAoB,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,sBAAsB;IACrC,eAAe,CAAC,eAAe,EAAE,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,oBAAoB;IACnC,eAAe,CAAC,SAAS,EAAE,CAAC;AAC7B,CAAC"}
|