@vedmalex/statemachine 1.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.cjs +3989 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +3943 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
- package/types/adapters.d.ts +30 -0
- package/types/config_validator.d.ts +76 -0
- package/types/error_handling.d.ts +124 -0
- package/types/index.d.ts +77 -0
- package/types/lite.d.ts +6 -0
- package/types/logger.d.ts +74 -0
- package/types/monitoring.d.ts +239 -0
- package/types/presets.d.ts +27 -0
- package/types/scheduler.d.ts +65 -0
- package/types/security.d.ts +145 -0
- package/types/state_machine.d.ts +227 -0
- package/types/types.d.ts +250 -0
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
export type StateName = string;
|
|
2
|
+
export type EventName = string;
|
|
3
|
+
export type EventAction<T, R = void> = (adaptee: T, ...args: unknown[]) => R;
|
|
4
|
+
export type ErrorHandler<T> = EventAction<T, void>;
|
|
5
|
+
export type MethodsOf<T> = {
|
|
6
|
+
[K in keyof T as T[K] extends (...args: any[]) => any ? K : never]: T[K];
|
|
7
|
+
};
|
|
8
|
+
export type PropertiesOf<T> = {
|
|
9
|
+
[K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: T[K];
|
|
10
|
+
};
|
|
11
|
+
export type KeysOf<T, R> = {
|
|
12
|
+
[K in keyof T]: T[K] extends R ? K : never;
|
|
13
|
+
}[keyof T];
|
|
14
|
+
export type ErrorContext = {
|
|
15
|
+
state?: StateName;
|
|
16
|
+
event?: EventName;
|
|
17
|
+
action?: string;
|
|
18
|
+
transition?: string;
|
|
19
|
+
phase?: 'guard' | 'action' | 'transition' | 'enter' | 'exit';
|
|
20
|
+
};
|
|
21
|
+
export declare class StateMachineError extends Error {
|
|
22
|
+
readonly context: ErrorContext;
|
|
23
|
+
constructor(message: string, context: ErrorContext, cause?: Error);
|
|
24
|
+
toString(): string;
|
|
25
|
+
}
|
|
26
|
+
export interface ILogger {
|
|
27
|
+
debug(message: string, context?: any): void;
|
|
28
|
+
info(message: string, context?: any): void;
|
|
29
|
+
warn(message: string, context?: any, error?: Error): void;
|
|
30
|
+
error(message: string, context?: any, error?: Error): void;
|
|
31
|
+
}
|
|
32
|
+
/** @unstable — timer scheduling injection contract; consumed by StateMachine. */
|
|
33
|
+
export interface ITimerScheduler {
|
|
34
|
+
isActive(): boolean;
|
|
35
|
+
schedule(delay: number, callback: () => void): object;
|
|
36
|
+
cancel(token: object): void;
|
|
37
|
+
}
|
|
38
|
+
/** @unstable — transition observability context (additive on IMonitor). */
|
|
39
|
+
export interface TransitionContext {
|
|
40
|
+
fromState: string;
|
|
41
|
+
toState: string;
|
|
42
|
+
eventName?: string;
|
|
43
|
+
}
|
|
44
|
+
/** @unstable — minimal aggregate observability snapshot for tests. */
|
|
45
|
+
export interface MonitorMetricsSnapshot {
|
|
46
|
+
totalTransitions: number;
|
|
47
|
+
successCount: number;
|
|
48
|
+
errorCount: number;
|
|
49
|
+
averageDuration: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @unstable — observability injection contract.
|
|
53
|
+
* EXPANDED additively in TASK-004 per TD-T4-2: third param of `recordTransition`
|
|
54
|
+
* is parameter-optional; `recordEvent?` and `getMetrics?` are interface-optional.
|
|
55
|
+
* Existing 2-arg call site at state_machine.ts:1665 remains valid.
|
|
56
|
+
*/
|
|
57
|
+
export interface IMonitor {
|
|
58
|
+
recordTransition(duration: number, success: boolean, context?: TransitionContext): void;
|
|
59
|
+
recordError(error: Error, context?: ErrorContext): void;
|
|
60
|
+
recordEvent?(eventName: string, duration: number): void;
|
|
61
|
+
getMetrics?(): MonitorMetricsSnapshot;
|
|
62
|
+
}
|
|
63
|
+
/** @unstable — error-handler injection contract; surfaces methods consumed by host integrations. */
|
|
64
|
+
export interface IErrorHandler {
|
|
65
|
+
isEnabled(): boolean;
|
|
66
|
+
enable(): void;
|
|
67
|
+
disable(): void;
|
|
68
|
+
addRecoveryStrategy(strategy: import('./error_handling').ErrorRecoveryStrategy): void;
|
|
69
|
+
removeRecoveryStrategy(strategyName: string): void;
|
|
70
|
+
getAnalytics(): import('./error_handling').ErrorAnalytics;
|
|
71
|
+
}
|
|
72
|
+
export interface StateMachineOptions {
|
|
73
|
+
logger?: ILogger;
|
|
74
|
+
monitor?: IMonitor;
|
|
75
|
+
scheduler?: ITimerScheduler;
|
|
76
|
+
errorHandler?: IErrorHandler;
|
|
77
|
+
/**
|
|
78
|
+
* Maximum time (ms) to wait for async entry/exit actions.
|
|
79
|
+
* If exceeded, the transition aborts with an error.
|
|
80
|
+
*/
|
|
81
|
+
transitionTimeout?: number;
|
|
82
|
+
/**
|
|
83
|
+
* State to transition to if an error occurs during a transition (Zombie State Prevention).
|
|
84
|
+
*/
|
|
85
|
+
errorState?: string;
|
|
86
|
+
/**
|
|
87
|
+
* If true, aborts the transition if onExit fails, keeping the state machine in the source state.
|
|
88
|
+
*/
|
|
89
|
+
abortOnExitError?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Maximum depth of the event queue before rejecting new events.
|
|
92
|
+
* Default: 1000
|
|
93
|
+
*/
|
|
94
|
+
maxQueueDepth?: number;
|
|
95
|
+
}
|
|
96
|
+
type StringKey = string & {};
|
|
97
|
+
export type SimpleStateName<S> = StringKey & keyof S;
|
|
98
|
+
export type RegionStateName<S> = {
|
|
99
|
+
[K in keyof S & string]: S[K] extends {
|
|
100
|
+
regions?: infer R;
|
|
101
|
+
} ? R extends Record<string, any> ? `${K}.${StringKey & keyof R}` : never : never;
|
|
102
|
+
}[keyof S & string];
|
|
103
|
+
export type NestedStateName<S> = {
|
|
104
|
+
[K in keyof S & string]: S[K] extends {
|
|
105
|
+
regions?: infer R;
|
|
106
|
+
} ? R extends Record<string, any> ? {
|
|
107
|
+
[RegKey in keyof R & string]: R[RegKey] extends Record<string, any> ? `${K}.${RegKey}.${StringKey & keyof R[RegKey]}` : never;
|
|
108
|
+
}[keyof R & string] : never : never;
|
|
109
|
+
}[keyof S & string];
|
|
110
|
+
export type DeepNestedStateName<S> = {
|
|
111
|
+
[K in keyof S & string]: S[K] extends {
|
|
112
|
+
regions?: infer R;
|
|
113
|
+
} ? R extends Record<string, any> ? {
|
|
114
|
+
[RegKey in keyof R & string]: R[RegKey] extends Record<string, any> ? {
|
|
115
|
+
[ChildKey in keyof R[RegKey] & string]: R[RegKey][ChildKey] extends {
|
|
116
|
+
regions?: infer CR;
|
|
117
|
+
} ? CR extends Record<string, any> ? `${K}.${RegKey}.${ChildKey}.${StringKey & keyof CR}` : never : never;
|
|
118
|
+
}[keyof R[RegKey] & string] : never;
|
|
119
|
+
}[keyof R & string] : never : never;
|
|
120
|
+
}[keyof S & string];
|
|
121
|
+
export type StatePaths<S> = SimpleStateName<S> | RegionStateName<S> | NestedStateName<S> | DeepNestedStateName<S>;
|
|
122
|
+
export type ActionOrString<T extends object, R = void> = KeysOf<T, EventAction<T, R>> | EventAction<Adapter<T>, R>;
|
|
123
|
+
export type ErrorHandlerOrString<T extends object> = KeysOf<T, ErrorHandler<T>> | ErrorHandler<T>;
|
|
124
|
+
export type RegionName = string;
|
|
125
|
+
export type RegionsConfig<T extends object> = Record<RegionName, StateMachineConfig<T>['states']>;
|
|
126
|
+
export type State<T extends object> = {
|
|
127
|
+
name: StateName;
|
|
128
|
+
display?: string;
|
|
129
|
+
comment?: string;
|
|
130
|
+
iconCls?: string;
|
|
131
|
+
onBeforeEnter?: ActionOrString<T>;
|
|
132
|
+
onEnter?: ActionOrString<T>;
|
|
133
|
+
onAfterEnter?: ActionOrString<T>;
|
|
134
|
+
onBeforeExit?: ActionOrString<T>;
|
|
135
|
+
onExit?: ActionOrString<T>;
|
|
136
|
+
onAfterExit?: ActionOrString<T>;
|
|
137
|
+
onError?: ErrorHandlerOrString<T>;
|
|
138
|
+
regions?: RegionsConfig<T>;
|
|
139
|
+
initial?: StateName;
|
|
140
|
+
history?: 'deep' | 'shallow';
|
|
141
|
+
invoke?: StateInvocation<T>[];
|
|
142
|
+
};
|
|
143
|
+
export interface StateInvocation<T extends object> {
|
|
144
|
+
/** Время задержки в миллисекундах */
|
|
145
|
+
delay: number;
|
|
146
|
+
/** Событие, которое будет вызвано после задержки */
|
|
147
|
+
event: EventName;
|
|
148
|
+
/** Условие запуска поручения */
|
|
149
|
+
cond?: (adaptee: T) => boolean;
|
|
150
|
+
/** Действие, выполняемое перед событием */
|
|
151
|
+
action?: ActionOrString<T>;
|
|
152
|
+
}
|
|
153
|
+
export type Transition<T extends object, S extends States<T>> = {
|
|
154
|
+
from: StatePaths<S>;
|
|
155
|
+
to: StatePaths<S>;
|
|
156
|
+
priority?: number;
|
|
157
|
+
guard?: ActionOrString<T, boolean>;
|
|
158
|
+
/**
|
|
159
|
+
* Action executed during the transition (after leaving `from`, before entering `to`).
|
|
160
|
+
* Receives the current owner context. Use for side-effects scoped to this edge.
|
|
161
|
+
*/
|
|
162
|
+
onTransition?: ActionOrString<T>;
|
|
163
|
+
onError?: ErrorHandlerOrString<T>;
|
|
164
|
+
};
|
|
165
|
+
export type Event<T extends object, S extends States<T>> = {
|
|
166
|
+
name: EventName;
|
|
167
|
+
display?: string;
|
|
168
|
+
comment?: string;
|
|
169
|
+
transitions: Array<Transition<T, S>>;
|
|
170
|
+
onBefore?: ActionOrString<T>;
|
|
171
|
+
onAfter?: ActionOrString<T>;
|
|
172
|
+
onSuccess?: ActionOrString<T>;
|
|
173
|
+
onError?: ErrorHandlerOrString<T>;
|
|
174
|
+
};
|
|
175
|
+
export type States<T extends object> = Record<StateName, Omit<State<T>, 'name'>>;
|
|
176
|
+
export type Events<T extends object, S extends States<T>> = Record<EventName, Omit<Event<T, S>, 'name'>>;
|
|
177
|
+
export interface Adapter<T extends object> {
|
|
178
|
+
get adaptee(): T;
|
|
179
|
+
set(property: keyof T, value: T[keyof T]): void;
|
|
180
|
+
get(property: keyof T): T[keyof T];
|
|
181
|
+
}
|
|
182
|
+
export type ExtractAdaptee<T> = T extends MemoryAdapter<infer R> ? R : T;
|
|
183
|
+
export declare function isAdapter<T extends object>(inp: unknown): inp is Adapter<T>;
|
|
184
|
+
export interface StateMachineConfig<T extends object = object> {
|
|
185
|
+
name: string;
|
|
186
|
+
description?: string;
|
|
187
|
+
stateAttribute: KeysOf<PropertiesOf<T>, string>;
|
|
188
|
+
initialState: keyof StateMachineConfig<T>['states'];
|
|
189
|
+
events: Record<EventName, Omit<Event<T, StateMachineConfig<T>['states']>, 'name'>>;
|
|
190
|
+
states: States<T>;
|
|
191
|
+
onError?: KeysOf<T, ErrorHandler<T>>;
|
|
192
|
+
}
|
|
193
|
+
export interface StatePersistenceAdapter {
|
|
194
|
+
save(state?: {
|
|
195
|
+
currentState: string;
|
|
196
|
+
history: any;
|
|
197
|
+
stateEntryTimes: any;
|
|
198
|
+
}): Promise<void>;
|
|
199
|
+
restore(): Promise<{
|
|
200
|
+
currentState: string;
|
|
201
|
+
history: any;
|
|
202
|
+
stateEntryTimes: any;
|
|
203
|
+
}>;
|
|
204
|
+
}
|
|
205
|
+
export declare class MemoryAdapter<T extends object> implements Adapter<T>, StatePersistenceAdapter {
|
|
206
|
+
adaptee: T;
|
|
207
|
+
private _savedState;
|
|
208
|
+
constructor(data: T);
|
|
209
|
+
set(property: keyof T, value: T[keyof T]): void;
|
|
210
|
+
get(property: keyof T): T[keyof T];
|
|
211
|
+
save(state?: {
|
|
212
|
+
currentState: string;
|
|
213
|
+
history: unknown;
|
|
214
|
+
stateEntryTimes: unknown;
|
|
215
|
+
}): Promise<void>;
|
|
216
|
+
restore(): Promise<{
|
|
217
|
+
currentState: string;
|
|
218
|
+
history: unknown;
|
|
219
|
+
stateEntryTimes: unknown;
|
|
220
|
+
}>;
|
|
221
|
+
}
|
|
222
|
+
export declare class LocalStorageAdapter<T extends object> implements Adapter<T>, StatePersistenceAdapter {
|
|
223
|
+
adaptee: T;
|
|
224
|
+
private key;
|
|
225
|
+
constructor(data: T, storageKey?: string);
|
|
226
|
+
set(property: keyof T, value: T[keyof T]): void;
|
|
227
|
+
get(property: keyof T): T[keyof T];
|
|
228
|
+
save(state?: any): Promise<void>;
|
|
229
|
+
restore(): Promise<any>;
|
|
230
|
+
}
|
|
231
|
+
export declare class SessionStorageAdapter<T extends object> implements Adapter<T>, StatePersistenceAdapter {
|
|
232
|
+
adaptee: T;
|
|
233
|
+
private key;
|
|
234
|
+
constructor(data: T, storageKey?: string);
|
|
235
|
+
set(property: keyof T, value: T[keyof T]): void;
|
|
236
|
+
get(property: keyof T): T[keyof T];
|
|
237
|
+
save(state?: any): Promise<void>;
|
|
238
|
+
restore(): Promise<any>;
|
|
239
|
+
}
|
|
240
|
+
export declare class ServerAdapter<T extends object> implements Adapter<T>, StatePersistenceAdapter {
|
|
241
|
+
adaptee: T;
|
|
242
|
+
static data: Record<string, unknown>;
|
|
243
|
+
private endpoint;
|
|
244
|
+
constructor(data: T, endpoint?: string);
|
|
245
|
+
set(property: keyof T, value: T[keyof T]): void;
|
|
246
|
+
get(property: keyof T): T[keyof T];
|
|
247
|
+
save(state?: any): Promise<void>;
|
|
248
|
+
restore(): Promise<any>;
|
|
249
|
+
}
|
|
250
|
+
export {};
|