@quazardous/quarkernel 1.0.12 → 2.2.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 +57 -51
- package/dist/create-machine-CYsscHPX.d.cts +275 -0
- package/dist/create-machine-CYsscHPX.d.ts +275 -0
- package/dist/fsm.cjs +1436 -0
- package/dist/fsm.cjs.map +1 -0
- package/dist/fsm.d.cts +2 -0
- package/dist/fsm.d.ts +2 -0
- package/dist/fsm.js +1432 -0
- package/dist/fsm.js.map +1 -0
- package/dist/index-BDf1xZi6.d.cts +896 -0
- package/dist/index-BPMXiW32.d.ts +896 -0
- package/dist/index.cjs +1750 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +1722 -0
- package/dist/index.js.map +1 -0
- package/dist/xstate.cjs +190 -0
- package/dist/xstate.cjs.map +1 -0
- package/dist/xstate.d.cts +152 -0
- package/dist/xstate.d.ts +152 -0
- package/dist/xstate.js +186 -0
- package/dist/xstate.js.map +1 -0
- package/package.json +62 -24
- package/LICENSE +0 -21
- package/src/index.js +0 -3
- package/src/lib/QuarKernel.js +0 -405
- package/types/QuarKernel.d.ts +0 -132
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { G as GuardFunction, A as ActionFunction, M as MachineConfig, C as CreateMachineConfig } from './create-machine-CYsscHPX.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* XState Import Helper
|
|
5
|
+
*
|
|
6
|
+
* Converts XState machine definitions to quarkernel FSM format.
|
|
7
|
+
* Supports basic XState v4/v5 machine structures.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* XState-like state node (simplified)
|
|
12
|
+
*/
|
|
13
|
+
interface XStateNode {
|
|
14
|
+
on?: Record<string, string | XStateTransition>;
|
|
15
|
+
entry?: string | string[] | XStateAction | XStateAction[];
|
|
16
|
+
exit?: string | string[] | XStateAction | XStateAction[];
|
|
17
|
+
always?: XStateTransition | XStateTransition[];
|
|
18
|
+
after?: Record<string, string | XStateTransition>;
|
|
19
|
+
meta?: Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* XState-like transition
|
|
23
|
+
*/
|
|
24
|
+
interface XStateTransition {
|
|
25
|
+
target?: string;
|
|
26
|
+
cond?: string | XStateGuard;
|
|
27
|
+
guard?: string | XStateGuard;
|
|
28
|
+
actions?: string | string[] | XStateAction | XStateAction[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* XState guard definition
|
|
32
|
+
*/
|
|
33
|
+
interface XStateGuard {
|
|
34
|
+
type: string;
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* XState action definition
|
|
39
|
+
*/
|
|
40
|
+
interface XStateAction {
|
|
41
|
+
type: string;
|
|
42
|
+
[key: string]: any;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* XState-like machine config
|
|
46
|
+
*/
|
|
47
|
+
interface XStateMachineConfig {
|
|
48
|
+
id?: string;
|
|
49
|
+
initial: string;
|
|
50
|
+
context?: Record<string, any>;
|
|
51
|
+
states: Record<string, XStateNode>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Import options
|
|
55
|
+
*/
|
|
56
|
+
interface ImportOptions<TContext = any> {
|
|
57
|
+
/** Machine prefix (required for quarkernel) */
|
|
58
|
+
prefix: string;
|
|
59
|
+
/** Guard implementations (keyed by guard name/type) */
|
|
60
|
+
guards?: Record<string, GuardFunction<TContext>>;
|
|
61
|
+
/** Action implementations (keyed by action name/type) */
|
|
62
|
+
actions?: Record<string, ActionFunction<TContext>>;
|
|
63
|
+
/** Allow force transitions */
|
|
64
|
+
allowForce?: boolean;
|
|
65
|
+
/** Track history */
|
|
66
|
+
trackHistory?: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convert XState machine config to quarkernel format
|
|
70
|
+
*
|
|
71
|
+
* @param xstateConfig - XState machine configuration
|
|
72
|
+
* @param options - Import options with implementations
|
|
73
|
+
* @returns quarkernel MachineConfig
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // XState format
|
|
78
|
+
* const xstateMachine = {
|
|
79
|
+
* id: 'order',
|
|
80
|
+
* initial: 'draft',
|
|
81
|
+
* context: { retries: 0 },
|
|
82
|
+
* states: {
|
|
83
|
+
* draft: { on: { SUBMIT: 'pending' } },
|
|
84
|
+
* pending: {
|
|
85
|
+
* on: {
|
|
86
|
+
* APPROVE: { target: 'confirmed', cond: 'canApprove' },
|
|
87
|
+
* REJECT: 'draft'
|
|
88
|
+
* }
|
|
89
|
+
* },
|
|
90
|
+
* confirmed: {}
|
|
91
|
+
* }
|
|
92
|
+
* };
|
|
93
|
+
*
|
|
94
|
+
* // Convert to quarkernel
|
|
95
|
+
* const config = fromXState(xstateMachine, {
|
|
96
|
+
* prefix: 'order',
|
|
97
|
+
* guards: {
|
|
98
|
+
* canApprove: (ctx) => ctx.retries < 3
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* const machine = useMachine(kernel, config);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function fromXState<TContext = Record<string, any>>(xstateConfig: XStateMachineConfig, options: ImportOptions<TContext>): MachineConfig<TContext>;
|
|
106
|
+
/**
|
|
107
|
+
* Export quarkernel machine config to XState-compatible format
|
|
108
|
+
* Useful for visualization tools that understand XState format
|
|
109
|
+
*
|
|
110
|
+
* @param config - quarkernel MachineConfig
|
|
111
|
+
* @returns XState-compatible config (without function implementations)
|
|
112
|
+
*/
|
|
113
|
+
declare function toXStateFormat<TContext = any>(config: MachineConfig<TContext>): XStateMachineConfig;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* XState <-> FSM Behaviors Conversion
|
|
117
|
+
*
|
|
118
|
+
* Converts between QuarKernel FSM (state-centric) and XState format.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* XState config with actions
|
|
123
|
+
*/
|
|
124
|
+
interface XStateConfigWithActions {
|
|
125
|
+
id: string;
|
|
126
|
+
initial: string;
|
|
127
|
+
context?: Record<string, unknown>;
|
|
128
|
+
states: Record<string, {
|
|
129
|
+
entry?: string | string[];
|
|
130
|
+
exit?: string | string[];
|
|
131
|
+
on?: Record<string, string | {
|
|
132
|
+
target: string;
|
|
133
|
+
actions?: string | string[];
|
|
134
|
+
}>;
|
|
135
|
+
after?: Record<number, {
|
|
136
|
+
target?: string;
|
|
137
|
+
actions?: string | string[];
|
|
138
|
+
}>;
|
|
139
|
+
}>;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Format state-centric FSM config as JavaScript code string
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const code = formatStateCentricCode(config);
|
|
147
|
+
* // Returns formatted JS code string with entry/exit/after inline in states
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function formatStateCentricCode<TContext = Record<string, unknown>>(config: CreateMachineConfig<TContext>): string;
|
|
151
|
+
|
|
152
|
+
export { type ImportOptions, type XStateConfigWithActions, type XStateMachineConfig, formatStateCentricCode, fromXState, toXStateFormat };
|
package/dist/xstate.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { G as GuardFunction, A as ActionFunction, M as MachineConfig, C as CreateMachineConfig } from './create-machine-CYsscHPX.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* XState Import Helper
|
|
5
|
+
*
|
|
6
|
+
* Converts XState machine definitions to quarkernel FSM format.
|
|
7
|
+
* Supports basic XState v4/v5 machine structures.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* XState-like state node (simplified)
|
|
12
|
+
*/
|
|
13
|
+
interface XStateNode {
|
|
14
|
+
on?: Record<string, string | XStateTransition>;
|
|
15
|
+
entry?: string | string[] | XStateAction | XStateAction[];
|
|
16
|
+
exit?: string | string[] | XStateAction | XStateAction[];
|
|
17
|
+
always?: XStateTransition | XStateTransition[];
|
|
18
|
+
after?: Record<string, string | XStateTransition>;
|
|
19
|
+
meta?: Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* XState-like transition
|
|
23
|
+
*/
|
|
24
|
+
interface XStateTransition {
|
|
25
|
+
target?: string;
|
|
26
|
+
cond?: string | XStateGuard;
|
|
27
|
+
guard?: string | XStateGuard;
|
|
28
|
+
actions?: string | string[] | XStateAction | XStateAction[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* XState guard definition
|
|
32
|
+
*/
|
|
33
|
+
interface XStateGuard {
|
|
34
|
+
type: string;
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* XState action definition
|
|
39
|
+
*/
|
|
40
|
+
interface XStateAction {
|
|
41
|
+
type: string;
|
|
42
|
+
[key: string]: any;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* XState-like machine config
|
|
46
|
+
*/
|
|
47
|
+
interface XStateMachineConfig {
|
|
48
|
+
id?: string;
|
|
49
|
+
initial: string;
|
|
50
|
+
context?: Record<string, any>;
|
|
51
|
+
states: Record<string, XStateNode>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Import options
|
|
55
|
+
*/
|
|
56
|
+
interface ImportOptions<TContext = any> {
|
|
57
|
+
/** Machine prefix (required for quarkernel) */
|
|
58
|
+
prefix: string;
|
|
59
|
+
/** Guard implementations (keyed by guard name/type) */
|
|
60
|
+
guards?: Record<string, GuardFunction<TContext>>;
|
|
61
|
+
/** Action implementations (keyed by action name/type) */
|
|
62
|
+
actions?: Record<string, ActionFunction<TContext>>;
|
|
63
|
+
/** Allow force transitions */
|
|
64
|
+
allowForce?: boolean;
|
|
65
|
+
/** Track history */
|
|
66
|
+
trackHistory?: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convert XState machine config to quarkernel format
|
|
70
|
+
*
|
|
71
|
+
* @param xstateConfig - XState machine configuration
|
|
72
|
+
* @param options - Import options with implementations
|
|
73
|
+
* @returns quarkernel MachineConfig
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // XState format
|
|
78
|
+
* const xstateMachine = {
|
|
79
|
+
* id: 'order',
|
|
80
|
+
* initial: 'draft',
|
|
81
|
+
* context: { retries: 0 },
|
|
82
|
+
* states: {
|
|
83
|
+
* draft: { on: { SUBMIT: 'pending' } },
|
|
84
|
+
* pending: {
|
|
85
|
+
* on: {
|
|
86
|
+
* APPROVE: { target: 'confirmed', cond: 'canApprove' },
|
|
87
|
+
* REJECT: 'draft'
|
|
88
|
+
* }
|
|
89
|
+
* },
|
|
90
|
+
* confirmed: {}
|
|
91
|
+
* }
|
|
92
|
+
* };
|
|
93
|
+
*
|
|
94
|
+
* // Convert to quarkernel
|
|
95
|
+
* const config = fromXState(xstateMachine, {
|
|
96
|
+
* prefix: 'order',
|
|
97
|
+
* guards: {
|
|
98
|
+
* canApprove: (ctx) => ctx.retries < 3
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* const machine = useMachine(kernel, config);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function fromXState<TContext = Record<string, any>>(xstateConfig: XStateMachineConfig, options: ImportOptions<TContext>): MachineConfig<TContext>;
|
|
106
|
+
/**
|
|
107
|
+
* Export quarkernel machine config to XState-compatible format
|
|
108
|
+
* Useful for visualization tools that understand XState format
|
|
109
|
+
*
|
|
110
|
+
* @param config - quarkernel MachineConfig
|
|
111
|
+
* @returns XState-compatible config (without function implementations)
|
|
112
|
+
*/
|
|
113
|
+
declare function toXStateFormat<TContext = any>(config: MachineConfig<TContext>): XStateMachineConfig;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* XState <-> FSM Behaviors Conversion
|
|
117
|
+
*
|
|
118
|
+
* Converts between QuarKernel FSM (state-centric) and XState format.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* XState config with actions
|
|
123
|
+
*/
|
|
124
|
+
interface XStateConfigWithActions {
|
|
125
|
+
id: string;
|
|
126
|
+
initial: string;
|
|
127
|
+
context?: Record<string, unknown>;
|
|
128
|
+
states: Record<string, {
|
|
129
|
+
entry?: string | string[];
|
|
130
|
+
exit?: string | string[];
|
|
131
|
+
on?: Record<string, string | {
|
|
132
|
+
target: string;
|
|
133
|
+
actions?: string | string[];
|
|
134
|
+
}>;
|
|
135
|
+
after?: Record<number, {
|
|
136
|
+
target?: string;
|
|
137
|
+
actions?: string | string[];
|
|
138
|
+
}>;
|
|
139
|
+
}>;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Format state-centric FSM config as JavaScript code string
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const code = formatStateCentricCode(config);
|
|
147
|
+
* // Returns formatted JS code string with entry/exit/after inline in states
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function formatStateCentricCode<TContext = Record<string, unknown>>(config: CreateMachineConfig<TContext>): string;
|
|
151
|
+
|
|
152
|
+
export { type ImportOptions, type XStateConfigWithActions, type XStateMachineConfig, formatStateCentricCode, fromXState, toXStateFormat };
|
package/dist/xstate.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// src/xstate/xstate-import.ts
|
|
2
|
+
function fromXState(xstateConfig, options) {
|
|
3
|
+
const { prefix, guards = {}, actions = {}, allowForce, trackHistory } = options;
|
|
4
|
+
const states = {};
|
|
5
|
+
for (const [stateName, xstateNode] of Object.entries(xstateConfig.states)) {
|
|
6
|
+
const stateNode = {};
|
|
7
|
+
if (xstateNode.on) {
|
|
8
|
+
stateNode.on = {};
|
|
9
|
+
for (const [event, transition] of Object.entries(xstateNode.on)) {
|
|
10
|
+
if (typeof transition === "string") {
|
|
11
|
+
stateNode.on[event] = transition;
|
|
12
|
+
} else {
|
|
13
|
+
const transitionDef = {
|
|
14
|
+
target: transition.target || stateName
|
|
15
|
+
// Self-transition if no target
|
|
16
|
+
};
|
|
17
|
+
const guardRef = transition.cond || transition.guard;
|
|
18
|
+
if (guardRef) {
|
|
19
|
+
const guardName = typeof guardRef === "string" ? guardRef : guardRef.type;
|
|
20
|
+
const guardFn = guards[guardName];
|
|
21
|
+
if (guardFn) {
|
|
22
|
+
transitionDef.guard = guardFn;
|
|
23
|
+
} else {
|
|
24
|
+
console.warn(`Guard "${guardName}" not provided in options.guards`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (transition.actions) {
|
|
28
|
+
const actionRefs = Array.isArray(transition.actions) ? transition.actions : [transition.actions];
|
|
29
|
+
const actionFns = [];
|
|
30
|
+
for (const actionRef of actionRefs) {
|
|
31
|
+
const actionName = typeof actionRef === "string" ? actionRef : actionRef.type;
|
|
32
|
+
const actionFn = actions[actionName];
|
|
33
|
+
if (actionFn) {
|
|
34
|
+
actionFns.push(actionFn);
|
|
35
|
+
} else {
|
|
36
|
+
console.warn(`Action "${actionName}" not provided in options.actions`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (actionFns.length > 0) {
|
|
40
|
+
transitionDef.actions = actionFns.length === 1 ? actionFns[0] : actionFns;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
stateNode.on[event] = transitionDef;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (xstateNode.entry) {
|
|
48
|
+
const entryRefs = Array.isArray(xstateNode.entry) ? xstateNode.entry : [xstateNode.entry];
|
|
49
|
+
const entryFns = [];
|
|
50
|
+
for (const actionRef of entryRefs) {
|
|
51
|
+
const actionName = typeof actionRef === "string" ? actionRef : actionRef.type;
|
|
52
|
+
const actionFn = actions[actionName];
|
|
53
|
+
if (actionFn) {
|
|
54
|
+
entryFns.push(actionFn);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (entryFns.length > 0) {
|
|
58
|
+
stateNode.entry = async (ctx, event, payload) => {
|
|
59
|
+
for (const fn of entryFns) {
|
|
60
|
+
await fn(ctx, event, payload);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (xstateNode.exit) {
|
|
66
|
+
const exitRefs = Array.isArray(xstateNode.exit) ? xstateNode.exit : [xstateNode.exit];
|
|
67
|
+
const exitFns = [];
|
|
68
|
+
for (const actionRef of exitRefs) {
|
|
69
|
+
const actionName = typeof actionRef === "string" ? actionRef : actionRef.type;
|
|
70
|
+
const actionFn = actions[actionName];
|
|
71
|
+
if (actionFn) {
|
|
72
|
+
exitFns.push(actionFn);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (exitFns.length > 0) {
|
|
76
|
+
stateNode.exit = async (ctx, event, payload) => {
|
|
77
|
+
for (const fn of exitFns) {
|
|
78
|
+
await fn(ctx, event, payload);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
states[stateName] = stateNode;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
prefix,
|
|
87
|
+
initial: xstateConfig.initial,
|
|
88
|
+
context: xstateConfig.context,
|
|
89
|
+
states,
|
|
90
|
+
allowForce,
|
|
91
|
+
trackHistory
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function toXStateFormat(config) {
|
|
95
|
+
const states = {};
|
|
96
|
+
for (const [stateName, stateNode] of Object.entries(config.states)) {
|
|
97
|
+
const xstateNode = {};
|
|
98
|
+
if (stateNode.on) {
|
|
99
|
+
xstateNode.on = {};
|
|
100
|
+
for (const [event, transition] of Object.entries(stateNode.on)) {
|
|
101
|
+
if (typeof transition === "string") {
|
|
102
|
+
xstateNode.on[event] = transition;
|
|
103
|
+
} else {
|
|
104
|
+
const xstateTransition = {
|
|
105
|
+
target: transition.target
|
|
106
|
+
};
|
|
107
|
+
if (transition.guard) {
|
|
108
|
+
xstateTransition.guard = { type: "guard" };
|
|
109
|
+
}
|
|
110
|
+
if (transition.actions) {
|
|
111
|
+
xstateTransition.actions = { type: "action" };
|
|
112
|
+
}
|
|
113
|
+
xstateNode.on[event] = xstateTransition;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (stateNode.entry) {
|
|
118
|
+
xstateNode.entry = { type: "entry" };
|
|
119
|
+
}
|
|
120
|
+
if (stateNode.exit) {
|
|
121
|
+
xstateNode.exit = { type: "exit" };
|
|
122
|
+
}
|
|
123
|
+
states[stateName] = xstateNode;
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
id: config.prefix,
|
|
127
|
+
initial: config.initial,
|
|
128
|
+
context: config.context,
|
|
129
|
+
states
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/xstate/xstate-behaviors.ts
|
|
134
|
+
function formatStateCentricCode(config) {
|
|
135
|
+
const { id, initial, context, states, on: globalHandlers } = config;
|
|
136
|
+
const lines = [
|
|
137
|
+
`// FSM Definition for "${id}"`,
|
|
138
|
+
`// Helpers: ctx, set(obj), send(event), log(msg)`,
|
|
139
|
+
"",
|
|
140
|
+
`export default {`,
|
|
141
|
+
` id: '${id}',`,
|
|
142
|
+
` initial: '${initial}',`
|
|
143
|
+
];
|
|
144
|
+
if (context && Object.keys(context).length > 0) {
|
|
145
|
+
lines.push(` context: ${JSON.stringify(context)},`);
|
|
146
|
+
}
|
|
147
|
+
lines.push(" states: {");
|
|
148
|
+
for (const [stateName, stateConfig] of Object.entries(states)) {
|
|
149
|
+
lines.push(` ${stateName}: {`);
|
|
150
|
+
if (stateConfig.entry) {
|
|
151
|
+
lines.push(` entry: ${stateConfig.entry.toString()},`);
|
|
152
|
+
}
|
|
153
|
+
if (stateConfig.exit) {
|
|
154
|
+
lines.push(` exit: ${stateConfig.exit.toString()},`);
|
|
155
|
+
}
|
|
156
|
+
if (stateConfig.after) {
|
|
157
|
+
lines.push(` after: { delay: ${stateConfig.after.delay}, send: '${stateConfig.after.send}' },`);
|
|
158
|
+
}
|
|
159
|
+
if (stateConfig.on && Object.keys(stateConfig.on).length > 0) {
|
|
160
|
+
lines.push(" on: {");
|
|
161
|
+
for (const [event, target] of Object.entries(stateConfig.on)) {
|
|
162
|
+
if (typeof target === "string") {
|
|
163
|
+
lines.push(` ${event}: '${target}',`);
|
|
164
|
+
} else {
|
|
165
|
+
lines.push(` ${event}: { target: '${target.target}'${target.cond ? `, cond: '${target.cond}'` : ""} },`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
lines.push(" },");
|
|
169
|
+
}
|
|
170
|
+
lines.push(" },");
|
|
171
|
+
}
|
|
172
|
+
lines.push(" },");
|
|
173
|
+
if (globalHandlers && Object.keys(globalHandlers).length > 0) {
|
|
174
|
+
lines.push(" on: {");
|
|
175
|
+
for (const [event, fn] of Object.entries(globalHandlers)) {
|
|
176
|
+
lines.push(` ${event}: ${fn.toString()},`);
|
|
177
|
+
}
|
|
178
|
+
lines.push(" },");
|
|
179
|
+
}
|
|
180
|
+
lines.push("};");
|
|
181
|
+
return lines.join("\n");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export { formatStateCentricCode, fromXState, toXStateFormat };
|
|
185
|
+
//# sourceMappingURL=xstate.js.map
|
|
186
|
+
//# sourceMappingURL=xstate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/xstate/xstate-import.ts","../src/xstate/xstate-behaviors.ts"],"names":[],"mappings":";AAkHO,SAAS,UAAA,CACd,cACA,OAAA,EACyB;AACzB,EAAA,MAAM,EAAE,MAAA,EAAQ,MAAA,GAAS,EAAC,EAAG,UAAU,EAAC,EAAG,UAAA,EAAY,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K,CAAA,IAAA,EAAO,KAAK,KAAK,EAAA,CAAG,QAAA,EAAU,CAAA,CAAA,CAAG,CAAA;AAAA,IAC9C;AACA,IAAA,KAAA,CAAM,KAAK,MAAM,CAAA;AAAA,EACnB;AAEA,EAAA,KAAA,CAAM,KAAK,IAAI,CAAA;AACf,EAAA,OAAO,KAAA,CAAM,KAAK,IAAI,CAAA;AACxB","file":"xstate.js","sourcesContent":["/**\n * XState Import Helper\n *\n * Converts XState machine definitions to quarkernel FSM format.\n * Supports basic XState v4/v5 machine structures.\n */\n\nimport type { MachineConfig, StateNode, TransitionDef, GuardFunction, ActionFunction } from '../fsm/types.js';\n\n/**\n * XState-like state node (simplified)\n */\ninterface XStateNode {\n on?: Record<string, string | XStateTransition>;\n entry?: string | string[] | XStateAction | XStateAction[];\n exit?: string | string[] | XStateAction | XStateAction[];\n always?: XStateTransition | XStateTransition[];\n after?: Record<string, string | XStateTransition>;\n meta?: Record<string, any>;\n}\n\n/**\n * XState-like transition\n */\ninterface XStateTransition {\n target?: string;\n cond?: string | XStateGuard;\n guard?: string | XStateGuard; // v5 uses guard instead of cond\n actions?: string | string[] | XStateAction | XStateAction[];\n}\n\n/**\n * XState guard definition\n */\ninterface XStateGuard {\n type: string;\n [key: string]: any;\n}\n\n/**\n * XState action definition\n */\ninterface XStateAction {\n type: string;\n [key: string]: any;\n}\n\n/**\n * XState-like machine config\n */\nexport interface XStateMachineConfig {\n id?: string;\n initial: string;\n context?: Record<string, any>;\n states: Record<string, XStateNode>;\n}\n\n/**\n * Import options\n */\nexport interface ImportOptions<TContext = any> {\n /** Machine prefix (required for quarkernel) */\n prefix: string;\n\n /** Guard implementations (keyed by guard name/type) */\n guards?: Record<string, GuardFunction<TContext>>;\n\n /** Action implementations (keyed by action name/type) */\n actions?: Record<string, ActionFunction<TContext>>;\n\n /** Allow force transitions */\n allowForce?: boolean;\n\n /** Track history */\n trackHistory?: boolean;\n}\n\n/**\n * Convert XState machine config to quarkernel format\n *\n * @param xstateConfig - XState machine configuration\n * @param options - Import options with implementations\n * @returns quarkernel MachineConfig\n *\n * @example\n * ```ts\n * // XState format\n * const xstateMachine = {\n * id: 'order',\n * initial: 'draft',\n * context: { retries: 0 },\n * states: {\n * draft: { on: { SUBMIT: 'pending' } },\n * pending: {\n * on: {\n * APPROVE: { target: 'confirmed', cond: 'canApprove' },\n * REJECT: 'draft'\n * }\n * },\n * confirmed: {}\n * }\n * };\n *\n * // Convert to quarkernel\n * const config = fromXState(xstateMachine, {\n * prefix: 'order',\n * guards: {\n * canApprove: (ctx) => ctx.retries < 3\n * }\n * });\n *\n * const machine = useMachine(kernel, config);\n * ```\n */\nexport function fromXState<TContext = Record<string, any>>(\n xstateConfig: XStateMachineConfig,\n options: ImportOptions<TContext>\n): MachineConfig<TContext> {\n const { prefix, guards = {}, actions = {}, allowForce, trackHistory } = options;\n\n const states: Record<string, StateNode<TContext>> = {};\n\n for (const [stateName, xstateNode] of Object.entries(xstateConfig.states)) {\n const stateNode: StateNode<TContext> = {};\n\n // Convert transitions\n if (xstateNode.on) {\n stateNode.on = {};\n\n for (const [event, transition] of Object.entries(xstateNode.on)) {\n if (typeof transition === 'string') {\n // Simple string target\n stateNode.on[event] = transition;\n } else {\n // Object transition with target, guard, actions\n const transitionDef: TransitionDef<TContext> = {\n target: transition.target || stateName, // Self-transition if no target\n };\n\n // Convert guard (cond in v4, guard in v5)\n const guardRef = transition.cond || transition.guard;\n if (guardRef) {\n const guardName = typeof guardRef === 'string' ? guardRef : guardRef.type;\n const guardFn = guards[guardName];\n if (guardFn) {\n transitionDef.guard = guardFn;\n } else {\n console.warn(`Guard \"${guardName}\" not provided in options.guards`);\n }\n }\n\n // Convert actions\n if (transition.actions) {\n const actionRefs = Array.isArray(transition.actions)\n ? transition.actions\n : [transition.actions];\n\n const actionFns: ActionFunction<TContext>[] = [];\n for (const actionRef of actionRefs) {\n const actionName = typeof actionRef === 'string' ? actionRef : actionRef.type;\n const actionFn = actions[actionName];\n if (actionFn) {\n actionFns.push(actionFn);\n } else {\n console.warn(`Action \"${actionName}\" not provided in options.actions`);\n }\n }\n\n if (actionFns.length > 0) {\n transitionDef.actions = actionFns.length === 1 ? actionFns[0] : actionFns;\n }\n }\n\n stateNode.on[event] = transitionDef;\n }\n }\n }\n\n // Convert entry actions\n if (xstateNode.entry) {\n const entryRefs = Array.isArray(xstateNode.entry)\n ? xstateNode.entry\n : [xstateNode.entry];\n\n const entryFns: ActionFunction<TContext>[] = [];\n for (const actionRef of entryRefs) {\n const actionName = typeof actionRef === 'string' ? actionRef : actionRef.type;\n const actionFn = actions[actionName];\n if (actionFn) {\n entryFns.push(actionFn);\n }\n }\n\n if (entryFns.length > 0) {\n stateNode.entry = async (ctx, event, payload) => {\n for (const fn of entryFns) {\n await fn(ctx, event, payload);\n }\n };\n }\n }\n\n // Convert exit actions\n if (xstateNode.exit) {\n const exitRefs = Array.isArray(xstateNode.exit)\n ? xstateNode.exit\n : [xstateNode.exit];\n\n const exitFns: ActionFunction<TContext>[] = [];\n for (const actionRef of exitRefs) {\n const actionName = typeof actionRef === 'string' ? actionRef : actionRef.type;\n const actionFn = actions[actionName];\n if (actionFn) {\n exitFns.push(actionFn);\n }\n }\n\n if (exitFns.length > 0) {\n stateNode.exit = async (ctx, event, payload) => {\n for (const fn of exitFns) {\n await fn(ctx, event, payload);\n }\n };\n }\n }\n\n states[stateName] = stateNode;\n }\n\n return {\n prefix,\n initial: xstateConfig.initial,\n context: xstateConfig.context as TContext,\n states,\n allowForce,\n trackHistory,\n };\n}\n\n/**\n * Export quarkernel machine config to XState-compatible format\n * Useful for visualization tools that understand XState format\n *\n * @param config - quarkernel MachineConfig\n * @returns XState-compatible config (without function implementations)\n */\nexport function toXStateFormat<TContext = any>(\n config: MachineConfig<TContext>\n): XStateMachineConfig {\n const states: Record<string, XStateNode> = {};\n\n for (const [stateName, stateNode] of Object.entries(config.states)) {\n const xstateNode: XStateNode = {};\n\n if (stateNode.on) {\n xstateNode.on = {};\n\n for (const [event, transition] of Object.entries(stateNode.on)) {\n if (typeof transition === 'string') {\n xstateNode.on[event] = transition;\n } else {\n const xstateTransition: XStateTransition = {\n target: transition.target,\n };\n\n // Note: guard and action functions can't be serialized\n // We just mark that they exist\n if (transition.guard) {\n xstateTransition.guard = { type: 'guard' };\n }\n if (transition.actions) {\n xstateTransition.actions = { type: 'action' };\n }\n\n xstateNode.on[event] = xstateTransition;\n }\n }\n }\n\n if (stateNode.entry) {\n xstateNode.entry = { type: 'entry' };\n }\n\n if (stateNode.exit) {\n xstateNode.exit = { type: 'exit' };\n }\n\n states[stateName] = xstateNode;\n }\n\n return {\n id: config.prefix,\n initial: config.initial,\n context: config.context as Record<string, any>,\n states,\n };\n}\n","/**\n * XState <-> FSM Behaviors Conversion\n *\n * Converts between QuarKernel FSM (state-centric) and XState format.\n */\n\nimport type { CreateMachineConfig } from '../fsm/create-machine.js';\n\n/**\n * XState config with actions\n */\nexport interface XStateConfigWithActions {\n id: string;\n initial: string;\n context?: Record<string, unknown>;\n states: Record<string, {\n entry?: string | string[];\n exit?: string | string[];\n on?: Record<string, string | { target: string; actions?: string | string[] }>;\n after?: Record<number, { target?: string; actions?: string | string[] }>;\n }>;\n}\n\n/**\n * Format state-centric FSM config as JavaScript code string\n *\n * @example\n * ```ts\n * const code = formatStateCentricCode(config);\n * // Returns formatted JS code string with entry/exit/after inline in states\n * ```\n */\nexport function formatStateCentricCode<TContext = Record<string, unknown>>(\n config: CreateMachineConfig<TContext>\n): string {\n const { id, initial, context, states, on: globalHandlers } = config;\n\n const lines: string[] = [\n `// FSM Definition for \"${id}\"`,\n `// Helpers: ctx, set(obj), send(event), log(msg)`,\n '',\n `export default {`,\n ` id: '${id}',`,\n ` initial: '${initial}',`,\n ];\n\n // Context\n if (context && Object.keys(context).length > 0) {\n lines.push(` context: ${JSON.stringify(context)},`);\n }\n\n // States\n lines.push(' states: {');\n for (const [stateName, stateConfig] of Object.entries(states)) {\n lines.push(` ${stateName}: {`);\n\n // entry\n if (stateConfig.entry) {\n lines.push(` entry: ${stateConfig.entry.toString()},`);\n }\n\n // exit\n if (stateConfig.exit) {\n lines.push(` exit: ${stateConfig.exit.toString()},`);\n }\n\n // after\n if (stateConfig.after) {\n lines.push(` after: { delay: ${stateConfig.after.delay}, send: '${stateConfig.after.send}' },`);\n }\n\n // on (transitions)\n if (stateConfig.on && Object.keys(stateConfig.on).length > 0) {\n lines.push(' on: {');\n for (const [event, target] of Object.entries(stateConfig.on)) {\n if (typeof target === 'string') {\n lines.push(` ${event}: '${target}',`);\n } else {\n lines.push(` ${event}: { target: '${target.target}'${target.cond ? `, cond: '${target.cond}'` : ''} },`);\n }\n }\n lines.push(' },');\n }\n\n lines.push(' },');\n }\n lines.push(' },');\n\n // Global event handlers\n if (globalHandlers && Object.keys(globalHandlers).length > 0) {\n lines.push(' on: {');\n for (const [event, fn] of Object.entries(globalHandlers)) {\n lines.push(` ${event}: ${fn.toString()},`);\n }\n lines.push(' },');\n }\n\n lines.push('};');\n return lines.join('\\n');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,38 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quazardous/quarkernel",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Micro Custom Events Kernel",
|
|
5
|
+
"description": "Micro Custom Events Kernel with dependency ordering, shared context, and composite events",
|
|
6
|
+
"author": "quazardous <berliozdavid@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/quazardous/quarkernel.git"
|
|
10
|
+
"url": "https://github.com/quazardous/quarkernel.git",
|
|
11
|
+
"directory": "packages/quarkernel"
|
|
9
12
|
},
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"files": [
|
|
13
|
-
"src/",
|
|
14
|
-
"types/"
|
|
15
|
-
],
|
|
16
|
-
"typings": "./types/QuarKernel.d.ts",
|
|
17
|
-
"scripts": {
|
|
18
|
-
"prepare": "npm run gen-typings",
|
|
19
|
-
"test": "mocha tests/",
|
|
20
|
-
"gen-typings": "npx -p typescript tsc src/lib/*.js --declaration --allowJs --emitDeclarationOnly --outDir types"
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/quazardous/quarkernel/issues"
|
|
21
15
|
},
|
|
16
|
+
"homepage": "https://github.com/quazardous/quarkernel#readme",
|
|
22
17
|
"keywords": [
|
|
23
18
|
"event",
|
|
19
|
+
"events",
|
|
24
20
|
"kernel",
|
|
25
|
-
"promise"
|
|
21
|
+
"promise",
|
|
22
|
+
"typescript",
|
|
23
|
+
"async",
|
|
24
|
+
"emitter",
|
|
25
|
+
"eventemitter",
|
|
26
|
+
"event-emitter",
|
|
27
|
+
"pubsub",
|
|
28
|
+
"publish-subscribe",
|
|
29
|
+
"dependency-ordering",
|
|
30
|
+
"shared-context",
|
|
31
|
+
"composite-events",
|
|
32
|
+
"wildcards",
|
|
33
|
+
"isomorphic"
|
|
26
34
|
],
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"require": "./dist/index.cjs"
|
|
40
|
+
},
|
|
41
|
+
"./fsm": {
|
|
42
|
+
"types": "./dist/fsm.d.ts",
|
|
43
|
+
"import": "./dist/fsm.js",
|
|
44
|
+
"require": "./dist/fsm.cjs"
|
|
45
|
+
},
|
|
46
|
+
"./xstate": {
|
|
47
|
+
"types": "./dist/xstate.d.ts",
|
|
48
|
+
"import": "./dist/xstate.js",
|
|
49
|
+
"require": "./dist/xstate.cjs"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"main": "./dist/index.cjs",
|
|
53
|
+
"module": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"files": [
|
|
56
|
+
"dist",
|
|
57
|
+
"README.md",
|
|
58
|
+
"LICENSE"
|
|
59
|
+
],
|
|
60
|
+
"sideEffects": false,
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=18.0.0"
|
|
34
63
|
},
|
|
35
|
-
"
|
|
36
|
-
"
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsup",
|
|
66
|
+
"build:watch": "tsup --watch",
|
|
67
|
+
"test": "vitest run",
|
|
68
|
+
"test:watch": "vitest",
|
|
69
|
+
"clean": "rm -rf dist"
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"tsup": "^8.0.0",
|
|
73
|
+
"typescript": "^5.3.0",
|
|
74
|
+
"vitest": "^1.0.0"
|
|
37
75
|
}
|
|
38
76
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 quazardous
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/src/index.js
DELETED