controlled-machine 0.1.0
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 +572 -0
- package/dist/index.cjs +160 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +143 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +103 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +100 -0
- package/dist/react.d.ts +100 -0
- package/dist/react.js +103 -0
- package/dist/react.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * Controlled Machine\n *\n * A controlled state machine where state lives outside the machine.\n *\n * - input: External data passed in\n * - computed: Derived values from input\n * - context: input + computed (full context available in handlers)\n * - on: Event → conditional actions\n * - effects: Watch-based side effects\n * - always: Auto-evaluated rules on context change\n */\n\n// ============================================\n// Types\n// ============================================\n\nexport type Rule<\n TContext,\n TPayload = undefined,\n TActions extends string = string,\n> = {\n when?: (context: TContext, payload: TPayload) => boolean\n do: TActions | TActions[]\n}\n\nexport type Handler<\n TContext,\n TPayload = undefined,\n TActions extends string = string,\n> = TActions | TActions[] | Rule<TContext, TPayload, TActions>[]\n\n// Effect helpers - utilities available in effect callbacks\nexport type EffectHelpers<TEvents extends EventsConfig> = {\n send: Send<TEvents>\n}\n\nexport type Cleanup = () => void\n\nexport type Effect<\n TContext,\n TEvents extends EventsConfig,\n TWatched = unknown,\n> = {\n watch: (context: TContext) => TWatched\n enter?: (\n context: TContext,\n helpers: EffectHelpers<TEvents>,\n ) => void | Cleanup | Promise<void>\n exit?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup\n change?: (\n context: TContext,\n prev: TWatched | undefined,\n curr: TWatched,\n helpers: EffectHelpers<TEvents>,\n ) => void | Cleanup\n}\n\n/** Helper for inferring prev/curr types from watch return type */\nexport function effect<TContext, TEvents extends EventsConfig, TWatched>(\n config: Effect<TContext, TEvents, TWatched>,\n): Effect<TContext, TEvents, TWatched> {\n return config\n}\n\nexport type EventsConfig = Record<string, unknown>\nexport type ComputedConfig = Record<string, unknown>\n\n// ============================================\n// Object-based Generic Types\n// ============================================\n\n/**\n * Object-based generic types - specify only needed types in any order\n *\n * @example\n * createMachine<{\n * input: MyInput\n * events: MyEvents\n * actions: 'foo' | 'bar'\n * }>({...})\n */\nexport type MachineTypes = {\n input?: unknown\n events?: EventsConfig\n computed?: ComputedConfig\n actions?: string\n state?: string\n}\n\nexport type Input<T extends MachineTypes> = T['input']\nexport type Events<T extends MachineTypes> = T['events'] extends EventsConfig\n ? T['events']\n : Record<string, undefined>\nexport type Computed<T extends MachineTypes> = T['computed'] extends ComputedConfig\n ? T['computed']\n : Record<string, never>\nexport type Actions<T extends MachineTypes> = T['actions'] extends string\n ? T['actions']\n : string\nexport type State<T extends MachineTypes> = T['state'] extends string\n ? T['state']\n : string\n\n// Context = Input + Computed (full context available in handlers)\nexport type Context<T extends MachineTypes> = Input<T> & Computed<T>\n\n// State-based handler configuration\nexport type StateConfig<\n TContext,\n TEvents extends EventsConfig,\n TActions extends string = string,\n> = {\n on?: { [K in keyof TEvents]?: Handler<TContext, TEvents[K], TActions> }\n}\n\nexport type StatesConfig<\n TState extends string,\n TContext,\n TEvents extends EventsConfig,\n TActions extends string = string,\n> = {\n [K in TState]?: StateConfig<TContext, TEvents, TActions>\n}\n\nexport type Machine<T extends MachineTypes> = {\n computed?: {\n [K in keyof Computed<T>]: (input: Input<T>) => Computed<T>[K]\n }\n on?: {\n [K in keyof Events<T>]?: Handler<Context<T>, Events<T>[K], Actions<T>>\n }\n states?: StatesConfig<State<T>, Context<T>, Events<T>, Actions<T>>\n always?: Rule<Context<T>, undefined, Actions<T>>[]\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n effects?: Effect<Context<T>, Events<T>, any>[]\n actions?: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [K in Actions<T>]: (context: Context<T>, payload?: any) => void\n }\n}\n\nexport type Send<TEvents extends EventsConfig> = <K extends keyof TEvents>(\n event: K,\n ...args: TEvents[K] extends undefined ? [] : [payload: TEvents[K]]\n) => void\n\n// createMachine return type\nexport type MachineInstance<T extends MachineTypes> = Machine<T> & {\n send: <K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => void\n evaluate: (input: Input<T>) => void\n getComputed: (input: Input<T>) => Computed<T>\n cleanup: () => void\n}\n\n// ============================================\n// Core Logic (Pure)\n// ============================================\n\nexport function executeActions<TContext, TPayload>(\n actionNames: string | string[],\n actions: Record<string, (context: TContext, payload?: TPayload) => void>,\n context: TContext,\n payload: TPayload,\n): void {\n if (typeof actionNames === 'string') {\n actions[actionNames]?.(context, payload)\n } else {\n for (const name of actionNames) {\n actions[name]?.(context, payload)\n }\n }\n}\n\nexport function isRuleArray<TContext, TPayload, TActions extends string>(\n handler: Handler<TContext, TPayload, TActions>,\n): handler is Rule<TContext, TPayload, TActions>[] {\n return (\n Array.isArray(handler) &&\n handler.length > 0 &&\n typeof handler[0] === 'object' &&\n 'do' in handler[0]\n )\n}\n\nexport function executeHandler<TContext, TPayload>(\n handler: Handler<TContext, TPayload>,\n actions: Record<string, (context: TContext, payload?: TPayload) => void>,\n context: TContext,\n payload: TPayload,\n): void {\n // Single action or action array\n if (typeof handler === 'string' || (Array.isArray(handler) && !isRuleArray(handler))) {\n executeActions(handler as string | string[], actions, context, payload)\n return\n }\n\n // Rule array\n for (const rule of handler as Rule<TContext, TPayload>[]) {\n if (!rule.when || rule.when(context, payload)) {\n executeActions(rule.do, actions, context, payload)\n break\n }\n }\n}\n\nexport function computeValues<TContext, TComputed extends ComputedConfig>(\n context: TContext,\n computed?: { [K in keyof TComputed]: (context: TContext) => TComputed[K] },\n): TContext & TComputed {\n if (!computed) return context as TContext & TComputed\n\n const values = {} as TComputed\n for (const key in computed) {\n values[key] = computed[key](context)\n }\n return { ...context, ...values }\n}\n\n/**\n * Shallow comparison function - for composite watch support\n *\n * Arrays: length + === comparison for each element\n * Others: === comparison\n */\nexport function shallowEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true\n\n // Array comparison\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n return a.every((v, i) => v === b[i])\n }\n\n return false\n}\n\n// Effect store for tracking state\nexport type EffectStore = {\n watchedValues: Map<number, unknown>\n enterCleanups: Map<number, () => void>\n changeCleanups: Map<number, () => void>\n exitCleanups: Map<number, () => void>\n}\n\nexport function createEffectStore(): EffectStore {\n return {\n watchedValues: new Map(),\n enterCleanups: new Map(),\n changeCleanups: new Map(),\n exitCleanups: new Map(),\n }\n}\n\n/**\n * Common effects processing logic\n */\nexport function processEffects<TContext, TEvents extends EventsConfig>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n effects: Effect<TContext, TEvents, any>[] | undefined,\n context: TContext,\n effectHelpers: EffectHelpers<TEvents>,\n store: EffectStore,\n): void {\n if (!effects) return\n\n effects.forEach((effect, i) => {\n const prev = store.watchedValues.get(i)\n const curr = effect.watch(context)\n\n if (!shallowEqual(prev, curr)) {\n // cleanup previous enter\n const enterCleanup = store.enterCleanups.get(i)\n if (enterCleanup) {\n enterCleanup()\n store.enterCleanups.delete(i)\n }\n\n // cleanup previous change\n const changeCleanup = store.changeCleanups.get(i)\n if (changeCleanup) {\n changeCleanup()\n store.changeCleanups.delete(i)\n }\n\n // change callback (can return cleanup)\n const changeResult = effect.change?.(context, prev, curr, effectHelpers)\n if (typeof changeResult === 'function') {\n store.changeCleanups.set(i, changeResult)\n }\n\n // enter (falsy → truthy)\n if (!prev && curr) {\n // cleanup previous exit\n const exitCleanup = store.exitCleanups.get(i)\n if (exitCleanup) {\n exitCleanup()\n store.exitCleanups.delete(i)\n }\n\n const enterResult = effect.enter?.(context, effectHelpers)\n if (typeof enterResult === 'function') {\n store.enterCleanups.set(i, enterResult)\n }\n }\n\n // exit (truthy → falsy)\n if (prev && !curr) {\n const exitResult = effect.exit?.(context, effectHelpers)\n if (typeof exitResult === 'function') {\n store.exitCleanups.set(i, exitResult)\n }\n }\n\n store.watchedValues.set(i, curr)\n }\n })\n}\n\n/**\n * Clear effect store\n */\nexport function clearEffectStore(store: EffectStore): void {\n store.enterCleanups.forEach((fn) => fn())\n store.enterCleanups.clear()\n store.changeCleanups.forEach((fn) => fn())\n store.changeCleanups.clear()\n store.exitCleanups.forEach((fn) => fn())\n store.exitCleanups.clear()\n store.watchedValues.clear()\n}\n\n// ============================================\n// Vanilla (non-React) + Type inference helper\n// ============================================\n\nexport function createMachine<T extends MachineTypes>(\n config: Machine<T>,\n): MachineInstance<T> {\n const effectStore = createEffectStore()\n\n const send = (<K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n const context = computeValues(input, config.computed)\n const payload = args[0] as Events<T>[K]\n\n // 1. State-specific handler first\n const state = (context as { state?: State<T> }).state\n if (state && config.states?.[state]?.on?.[event]) {\n const stateHandler = config.states[state].on[event]\n executeHandler(stateHandler, config.actions ?? {}, context, payload)\n }\n\n // 2. Global handler\n const globalHandler = config.on?.[event]\n if (globalHandler) {\n executeHandler(globalHandler, config.actions ?? {}, context, payload)\n }\n }) as <K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => void\n\n // vanilla send wrapper (with input binding)\n const createEffectHelpersWithInput = (input: Input<T>): EffectHelpers<Events<T>> => ({\n send: (<K extends keyof Events<T>>(\n event: K,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n send(event, input, ...args)\n }) as Send<Events<T>>,\n })\n\n const evaluate = (input: Input<T>) => {\n const context = computeValues(input, config.computed)\n const effectHelpers = createEffectHelpersWithInput(input)\n\n // always\n if (config.always && config.actions) {\n const actionsMap = config.actions as Record<\n string,\n (context: Context<T>) => void\n >\n for (const rule of config.always) {\n if (!rule.when || rule.when(context, undefined)) {\n executeActions(rule.do, actionsMap, context, undefined)\n break\n }\n }\n }\n\n // effects\n processEffects(config.effects, context, effectHelpers, effectStore)\n }\n\n const getComputed = (input: Input<T>): Computed<T> => {\n const context = computeValues(input, config.computed)\n if (!config.computed) return {} as Computed<T>\n const result = {} as Computed<T>\n for (const key in config.computed) {\n result[key] = context[key]\n }\n return result\n }\n\n const cleanup = () => clearEffectStore(effectStore)\n\n return Object.assign(config, { send, evaluate, getComputed, cleanup })\n}\n"],"names":["effect"],"mappings":";;AA2DO,SAAS,OACd,QACqC;AACrC,SAAO;AACT;AAoGO,SAAS,eACd,aACA,SACA,SACA,SACM;AACN,MAAI,OAAO,gBAAgB,UAAU;AACnC,YAAQ,WAAW,IAAI,SAAS,OAAO;AAAA,EACzC,OAAO;AACL,eAAW,QAAQ,aAAa;AAC9B,cAAQ,IAAI,IAAI,SAAS,OAAO;AAAA,IAClC;AAAA,EACF;AACF;AAEO,SAAS,YACd,SACiD;AACjD,SACE,MAAM,QAAQ,OAAO,KACrB,QAAQ,SAAS,KACjB,OAAO,QAAQ,CAAC,MAAM,YACtB,QAAQ,QAAQ,CAAC;AAErB;AAEO,SAAS,eACd,SACA,SACA,SACA,SACM;AAEN,MAAI,OAAO,YAAY,YAAa,MAAM,QAAQ,OAAO,KAAK,CAAC,YAAY,OAAO,GAAI;AACpF,mBAAe,SAA8B,SAAS,SAAS,OAAO;AACtE;AAAA,EACF;AAGA,aAAW,QAAQ,SAAuC;AACxD,QAAI,CAAC,KAAK,QAAQ,KAAK,KAAK,SAAS,OAAO,GAAG;AAC7C,qBAAe,KAAK,IAAI,SAAS,SAAS,OAAO;AACjD;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,cACd,SACA,UACsB;AACtB,MAAI,CAAC,SAAU,QAAO;AAEtB,QAAM,SAAS,CAAA;AACf,aAAW,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,IACnF,OAAO,CACL,UACG,SACA;AACH,WAAK,OAAO,OAAO,GAAG,IAAI;AAAA,IAC5B;AAAA,EAAA;AAGF,QAAM,WAAW,CAAC,UAAoB;AACpC,UAAM,UAAU,cAAc,OAAO,OAAO,QAAQ;AACpD,UAAM,gBAAgB,6BAA6B,KAAK;AAGxD,QAAI,OAAO,UAAU,OAAO,SAAS;AACnC,YAAM,aAAa,OAAO;AAI1B,iBAAW,QAAQ,OAAO,QAAQ;AAChC,YAAI,CAAC,KAAK,QAAQ,KAAK,KAAK,SAAS,MAAS,GAAG;AAC/C,yBAAe,KAAK,IAAI,YAAY,SAAS,MAAS;AACtD;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAGA,mBAAe,OAAO,SAAS,SAAS,eAAe,WAAW;AAAA,EACpE;AAEA,QAAM,cAAc,CAAC,UAAiC;AACpD,UAAM,UAAU,cAAc,OAAO,OAAO,QAAQ;AACpD,QAAI,CAAC,OAAO,SAAU,QAAO,CAAA;AAC7B,UAAM,SAAS,CAAA;AACf,eAAW,OAAO,OAAO,UAAU;AACjC,aAAO,GAAG,IAAI,QAAQ,GAAG;AAAA,IAC3B;AACA,WAAO;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,iBAAiB,WAAW;AAElD,SAAO,OAAO,OAAO,QAAQ,EAAE,MAAM,UAAU,aAAa,SAAS;AACvE;;;;;;;;;;;"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export declare type Actions<T extends MachineTypes> = T['actions'] extends string ? T['actions'] : string;
|
|
2
|
+
|
|
3
|
+
export declare type Cleanup = () => void;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Clear effect store
|
|
7
|
+
*/
|
|
8
|
+
export declare function clearEffectStore(store: EffectStore): void;
|
|
9
|
+
|
|
10
|
+
export declare type Computed<T extends MachineTypes> = T['computed'] extends ComputedConfig ? T['computed'] : Record<string, never>;
|
|
11
|
+
|
|
12
|
+
export declare type ComputedConfig = Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
export declare function computeValues<TContext, TComputed extends ComputedConfig>(context: TContext, computed?: {
|
|
15
|
+
[K in keyof TComputed]: (context: TContext) => TComputed[K];
|
|
16
|
+
}): TContext & TComputed;
|
|
17
|
+
|
|
18
|
+
export declare type Context<T extends MachineTypes> = Input<T> & Computed<T>;
|
|
19
|
+
|
|
20
|
+
export declare function createEffectStore(): EffectStore;
|
|
21
|
+
|
|
22
|
+
export declare function createMachine<T extends MachineTypes>(config: Machine<T>): MachineInstance<T>;
|
|
23
|
+
|
|
24
|
+
export declare type Effect<TContext, TEvents extends EventsConfig, TWatched = unknown> = {
|
|
25
|
+
watch: (context: TContext) => TWatched;
|
|
26
|
+
enter?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup | Promise<void>;
|
|
27
|
+
exit?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup;
|
|
28
|
+
change?: (context: TContext, prev: TWatched | undefined, curr: TWatched, helpers: EffectHelpers<TEvents>) => void | Cleanup;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Helper for inferring prev/curr types from watch return type */
|
|
32
|
+
export declare function effect<TContext, TEvents extends EventsConfig, TWatched>(config: Effect<TContext, TEvents, TWatched>): Effect<TContext, TEvents, TWatched>;
|
|
33
|
+
|
|
34
|
+
export declare type EffectHelpers<TEvents extends EventsConfig> = {
|
|
35
|
+
send: Send<TEvents>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export declare type EffectStore = {
|
|
39
|
+
watchedValues: Map<number, unknown>;
|
|
40
|
+
enterCleanups: Map<number, () => void>;
|
|
41
|
+
changeCleanups: Map<number, () => void>;
|
|
42
|
+
exitCleanups: Map<number, () => void>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export declare type Events<T extends MachineTypes> = T['events'] extends EventsConfig ? T['events'] : Record<string, undefined>;
|
|
46
|
+
|
|
47
|
+
export declare type EventsConfig = Record<string, unknown>;
|
|
48
|
+
|
|
49
|
+
export declare function executeActions<TContext, TPayload>(actionNames: string | string[], actions: Record<string, (context: TContext, payload?: TPayload) => void>, context: TContext, payload: TPayload): void;
|
|
50
|
+
|
|
51
|
+
export declare function executeHandler<TContext, TPayload>(handler: Handler<TContext, TPayload>, actions: Record<string, (context: TContext, payload?: TPayload) => void>, context: TContext, payload: TPayload): void;
|
|
52
|
+
|
|
53
|
+
export declare type Handler<TContext, TPayload = undefined, TActions extends string = string> = TActions | TActions[] | Rule<TContext, TPayload, TActions>[];
|
|
54
|
+
|
|
55
|
+
export declare type Input<T extends MachineTypes> = T['input'];
|
|
56
|
+
|
|
57
|
+
export declare function isRuleArray<TContext, TPayload, TActions extends string>(handler: Handler<TContext, TPayload, TActions>): handler is Rule<TContext, TPayload, TActions>[];
|
|
58
|
+
|
|
59
|
+
export declare type Machine<T extends MachineTypes> = {
|
|
60
|
+
computed?: {
|
|
61
|
+
[K in keyof Computed<T>]: (input: Input<T>) => Computed<T>[K];
|
|
62
|
+
};
|
|
63
|
+
on?: {
|
|
64
|
+
[K in keyof Events<T>]?: Handler<Context<T>, Events<T>[K], Actions<T>>;
|
|
65
|
+
};
|
|
66
|
+
states?: StatesConfig<State<T>, Context<T>, Events<T>, Actions<T>>;
|
|
67
|
+
always?: Rule<Context<T>, undefined, Actions<T>>[];
|
|
68
|
+
effects?: Effect<Context<T>, Events<T>, any>[];
|
|
69
|
+
actions?: {
|
|
70
|
+
[K in Actions<T>]: (context: Context<T>, payload?: any) => void;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export declare type MachineInstance<T extends MachineTypes> = Machine<T> & {
|
|
75
|
+
send: <K extends keyof Events<T>>(event: K, input: Input<T>, ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]) => void;
|
|
76
|
+
evaluate: (input: Input<T>) => void;
|
|
77
|
+
getComputed: (input: Input<T>) => Computed<T>;
|
|
78
|
+
cleanup: () => void;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Object-based generic types - specify only needed types in any order
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* createMachine<{
|
|
86
|
+
* input: MyInput
|
|
87
|
+
* events: MyEvents
|
|
88
|
+
* actions: 'foo' | 'bar'
|
|
89
|
+
* }>({...})
|
|
90
|
+
*/
|
|
91
|
+
export declare type MachineTypes = {
|
|
92
|
+
input?: unknown;
|
|
93
|
+
events?: EventsConfig;
|
|
94
|
+
computed?: ComputedConfig;
|
|
95
|
+
actions?: string;
|
|
96
|
+
state?: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Common effects processing logic
|
|
101
|
+
*/
|
|
102
|
+
export declare function processEffects<TContext, TEvents extends EventsConfig>(effects: Effect<TContext, TEvents, any>[] | undefined, context: TContext, effectHelpers: EffectHelpers<TEvents>, store: EffectStore): void;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Controlled Machine
|
|
106
|
+
*
|
|
107
|
+
* A controlled state machine where state lives outside the machine.
|
|
108
|
+
*
|
|
109
|
+
* - input: External data passed in
|
|
110
|
+
* - computed: Derived values from input
|
|
111
|
+
* - context: input + computed (full context available in handlers)
|
|
112
|
+
* - on: Event → conditional actions
|
|
113
|
+
* - effects: Watch-based side effects
|
|
114
|
+
* - always: Auto-evaluated rules on context change
|
|
115
|
+
*/
|
|
116
|
+
export declare type Rule<TContext, TPayload = undefined, TActions extends string = string> = {
|
|
117
|
+
when?: (context: TContext, payload: TPayload) => boolean;
|
|
118
|
+
do: TActions | TActions[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export declare type Send<TEvents extends EventsConfig> = <K extends keyof TEvents>(event: K, ...args: TEvents[K] extends undefined ? [] : [payload: TEvents[K]]) => void;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Shallow comparison function - for composite watch support
|
|
125
|
+
*
|
|
126
|
+
* Arrays: length + === comparison for each element
|
|
127
|
+
* Others: === comparison
|
|
128
|
+
*/
|
|
129
|
+
export declare function shallowEqual(a: unknown, b: unknown): boolean;
|
|
130
|
+
|
|
131
|
+
export declare type State<T extends MachineTypes> = T['state'] extends string ? T['state'] : string;
|
|
132
|
+
|
|
133
|
+
export declare type StateConfig<TContext, TEvents extends EventsConfig, TActions extends string = string> = {
|
|
134
|
+
on?: {
|
|
135
|
+
[K in keyof TEvents]?: Handler<TContext, TEvents[K], TActions>;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export declare type StatesConfig<TState extends string, TContext, TEvents extends EventsConfig, TActions extends string = string> = {
|
|
140
|
+
[K in TState]?: StateConfig<TContext, TEvents, TActions>;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export { }
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export declare type Actions<T extends MachineTypes> = T['actions'] extends string ? T['actions'] : string;
|
|
2
|
+
|
|
3
|
+
export declare type Cleanup = () => void;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Clear effect store
|
|
7
|
+
*/
|
|
8
|
+
export declare function clearEffectStore(store: EffectStore): void;
|
|
9
|
+
|
|
10
|
+
export declare type Computed<T extends MachineTypes> = T['computed'] extends ComputedConfig ? T['computed'] : Record<string, never>;
|
|
11
|
+
|
|
12
|
+
export declare type ComputedConfig = Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
export declare function computeValues<TContext, TComputed extends ComputedConfig>(context: TContext, computed?: {
|
|
15
|
+
[K in keyof TComputed]: (context: TContext) => TComputed[K];
|
|
16
|
+
}): TContext & TComputed;
|
|
17
|
+
|
|
18
|
+
export declare type Context<T extends MachineTypes> = Input<T> & Computed<T>;
|
|
19
|
+
|
|
20
|
+
export declare function createEffectStore(): EffectStore;
|
|
21
|
+
|
|
22
|
+
export declare function createMachine<T extends MachineTypes>(config: Machine<T>): MachineInstance<T>;
|
|
23
|
+
|
|
24
|
+
export declare type Effect<TContext, TEvents extends EventsConfig, TWatched = unknown> = {
|
|
25
|
+
watch: (context: TContext) => TWatched;
|
|
26
|
+
enter?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup | Promise<void>;
|
|
27
|
+
exit?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup;
|
|
28
|
+
change?: (context: TContext, prev: TWatched | undefined, curr: TWatched, helpers: EffectHelpers<TEvents>) => void | Cleanup;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Helper for inferring prev/curr types from watch return type */
|
|
32
|
+
export declare function effect<TContext, TEvents extends EventsConfig, TWatched>(config: Effect<TContext, TEvents, TWatched>): Effect<TContext, TEvents, TWatched>;
|
|
33
|
+
|
|
34
|
+
export declare type EffectHelpers<TEvents extends EventsConfig> = {
|
|
35
|
+
send: Send<TEvents>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export declare type EffectStore = {
|
|
39
|
+
watchedValues: Map<number, unknown>;
|
|
40
|
+
enterCleanups: Map<number, () => void>;
|
|
41
|
+
changeCleanups: Map<number, () => void>;
|
|
42
|
+
exitCleanups: Map<number, () => void>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export declare type Events<T extends MachineTypes> = T['events'] extends EventsConfig ? T['events'] : Record<string, undefined>;
|
|
46
|
+
|
|
47
|
+
export declare type EventsConfig = Record<string, unknown>;
|
|
48
|
+
|
|
49
|
+
export declare function executeActions<TContext, TPayload>(actionNames: string | string[], actions: Record<string, (context: TContext, payload?: TPayload) => void>, context: TContext, payload: TPayload): void;
|
|
50
|
+
|
|
51
|
+
export declare function executeHandler<TContext, TPayload>(handler: Handler<TContext, TPayload>, actions: Record<string, (context: TContext, payload?: TPayload) => void>, context: TContext, payload: TPayload): void;
|
|
52
|
+
|
|
53
|
+
export declare type Handler<TContext, TPayload = undefined, TActions extends string = string> = TActions | TActions[] | Rule<TContext, TPayload, TActions>[];
|
|
54
|
+
|
|
55
|
+
export declare type Input<T extends MachineTypes> = T['input'];
|
|
56
|
+
|
|
57
|
+
export declare function isRuleArray<TContext, TPayload, TActions extends string>(handler: Handler<TContext, TPayload, TActions>): handler is Rule<TContext, TPayload, TActions>[];
|
|
58
|
+
|
|
59
|
+
export declare type Machine<T extends MachineTypes> = {
|
|
60
|
+
computed?: {
|
|
61
|
+
[K in keyof Computed<T>]: (input: Input<T>) => Computed<T>[K];
|
|
62
|
+
};
|
|
63
|
+
on?: {
|
|
64
|
+
[K in keyof Events<T>]?: Handler<Context<T>, Events<T>[K], Actions<T>>;
|
|
65
|
+
};
|
|
66
|
+
states?: StatesConfig<State<T>, Context<T>, Events<T>, Actions<T>>;
|
|
67
|
+
always?: Rule<Context<T>, undefined, Actions<T>>[];
|
|
68
|
+
effects?: Effect<Context<T>, Events<T>, any>[];
|
|
69
|
+
actions?: {
|
|
70
|
+
[K in Actions<T>]: (context: Context<T>, payload?: any) => void;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export declare type MachineInstance<T extends MachineTypes> = Machine<T> & {
|
|
75
|
+
send: <K extends keyof Events<T>>(event: K, input: Input<T>, ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]) => void;
|
|
76
|
+
evaluate: (input: Input<T>) => void;
|
|
77
|
+
getComputed: (input: Input<T>) => Computed<T>;
|
|
78
|
+
cleanup: () => void;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Object-based generic types - specify only needed types in any order
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* createMachine<{
|
|
86
|
+
* input: MyInput
|
|
87
|
+
* events: MyEvents
|
|
88
|
+
* actions: 'foo' | 'bar'
|
|
89
|
+
* }>({...})
|
|
90
|
+
*/
|
|
91
|
+
export declare type MachineTypes = {
|
|
92
|
+
input?: unknown;
|
|
93
|
+
events?: EventsConfig;
|
|
94
|
+
computed?: ComputedConfig;
|
|
95
|
+
actions?: string;
|
|
96
|
+
state?: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Common effects processing logic
|
|
101
|
+
*/
|
|
102
|
+
export declare function processEffects<TContext, TEvents extends EventsConfig>(effects: Effect<TContext, TEvents, any>[] | undefined, context: TContext, effectHelpers: EffectHelpers<TEvents>, store: EffectStore): void;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Controlled Machine
|
|
106
|
+
*
|
|
107
|
+
* A controlled state machine where state lives outside the machine.
|
|
108
|
+
*
|
|
109
|
+
* - input: External data passed in
|
|
110
|
+
* - computed: Derived values from input
|
|
111
|
+
* - context: input + computed (full context available in handlers)
|
|
112
|
+
* - on: Event → conditional actions
|
|
113
|
+
* - effects: Watch-based side effects
|
|
114
|
+
* - always: Auto-evaluated rules on context change
|
|
115
|
+
*/
|
|
116
|
+
export declare type Rule<TContext, TPayload = undefined, TActions extends string = string> = {
|
|
117
|
+
when?: (context: TContext, payload: TPayload) => boolean;
|
|
118
|
+
do: TActions | TActions[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export declare type Send<TEvents extends EventsConfig> = <K extends keyof TEvents>(event: K, ...args: TEvents[K] extends undefined ? [] : [payload: TEvents[K]]) => void;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Shallow comparison function - for composite watch support
|
|
125
|
+
*
|
|
126
|
+
* Arrays: length + === comparison for each element
|
|
127
|
+
* Others: === comparison
|
|
128
|
+
*/
|
|
129
|
+
export declare function shallowEqual(a: unknown, b: unknown): boolean;
|
|
130
|
+
|
|
131
|
+
export declare type State<T extends MachineTypes> = T['state'] extends string ? T['state'] : string;
|
|
132
|
+
|
|
133
|
+
export declare type StateConfig<TContext, TEvents extends EventsConfig, TActions extends string = string> = {
|
|
134
|
+
on?: {
|
|
135
|
+
[K in keyof TEvents]?: Handler<TContext, TEvents[K], TActions>;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export declare type StatesConfig<TState extends string, TContext, TEvents extends EventsConfig, TActions extends string = string> = {
|
|
140
|
+
[K in TState]?: StateConfig<TContext, TEvents, TActions>;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
function effect(config) {
|
|
2
|
+
return config;
|
|
3
|
+
}
|
|
4
|
+
function executeActions(actionNames, actions, context, payload) {
|
|
5
|
+
if (typeof actionNames === "string") {
|
|
6
|
+
actions[actionNames]?.(context, payload);
|
|
7
|
+
} else {
|
|
8
|
+
for (const name of actionNames) {
|
|
9
|
+
actions[name]?.(context, payload);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function isRuleArray(handler) {
|
|
14
|
+
return Array.isArray(handler) && handler.length > 0 && typeof handler[0] === "object" && "do" in handler[0];
|
|
15
|
+
}
|
|
16
|
+
function executeHandler(handler, actions, context, payload) {
|
|
17
|
+
if (typeof handler === "string" || Array.isArray(handler) && !isRuleArray(handler)) {
|
|
18
|
+
executeActions(handler, actions, context, payload);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
for (const rule of handler) {
|
|
22
|
+
if (!rule.when || rule.when(context, payload)) {
|
|
23
|
+
executeActions(rule.do, actions, context, payload);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function computeValues(context, computed) {
|
|
29
|
+
if (!computed) return context;
|
|
30
|
+
const values = {};
|
|
31
|
+
for (const key in computed) {
|
|
32
|
+
values[key] = computed[key](context);
|
|
33
|
+
}
|
|
34
|
+
return { ...context, ...values };
|
|
35
|
+
}
|
|
36
|
+
function shallowEqual(a, b) {
|
|
37
|
+
if (a === b) return true;
|
|
38
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
39
|
+
if (a.length !== b.length) return false;
|
|
40
|
+
return a.every((v, i) => v === b[i]);
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
function createEffectStore() {
|
|
45
|
+
return {
|
|
46
|
+
watchedValues: /* @__PURE__ */ new Map(),
|
|
47
|
+
enterCleanups: /* @__PURE__ */ new Map(),
|
|
48
|
+
changeCleanups: /* @__PURE__ */ new Map(),
|
|
49
|
+
exitCleanups: /* @__PURE__ */ new Map()
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function processEffects(effects, context, effectHelpers, store) {
|
|
53
|
+
if (!effects) return;
|
|
54
|
+
effects.forEach((effect2, i) => {
|
|
55
|
+
const prev = store.watchedValues.get(i);
|
|
56
|
+
const curr = effect2.watch(context);
|
|
57
|
+
if (!shallowEqual(prev, curr)) {
|
|
58
|
+
const enterCleanup = store.enterCleanups.get(i);
|
|
59
|
+
if (enterCleanup) {
|
|
60
|
+
enterCleanup();
|
|
61
|
+
store.enterCleanups.delete(i);
|
|
62
|
+
}
|
|
63
|
+
const changeCleanup = store.changeCleanups.get(i);
|
|
64
|
+
if (changeCleanup) {
|
|
65
|
+
changeCleanup();
|
|
66
|
+
store.changeCleanups.delete(i);
|
|
67
|
+
}
|
|
68
|
+
const changeResult = effect2.change?.(context, prev, curr, effectHelpers);
|
|
69
|
+
if (typeof changeResult === "function") {
|
|
70
|
+
store.changeCleanups.set(i, changeResult);
|
|
71
|
+
}
|
|
72
|
+
if (!prev && curr) {
|
|
73
|
+
const exitCleanup = store.exitCleanups.get(i);
|
|
74
|
+
if (exitCleanup) {
|
|
75
|
+
exitCleanup();
|
|
76
|
+
store.exitCleanups.delete(i);
|
|
77
|
+
}
|
|
78
|
+
const enterResult = effect2.enter?.(context, effectHelpers);
|
|
79
|
+
if (typeof enterResult === "function") {
|
|
80
|
+
store.enterCleanups.set(i, enterResult);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (prev && !curr) {
|
|
84
|
+
const exitResult = effect2.exit?.(context, effectHelpers);
|
|
85
|
+
if (typeof exitResult === "function") {
|
|
86
|
+
store.exitCleanups.set(i, exitResult);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
store.watchedValues.set(i, curr);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function clearEffectStore(store) {
|
|
94
|
+
store.enterCleanups.forEach((fn) => fn());
|
|
95
|
+
store.enterCleanups.clear();
|
|
96
|
+
store.changeCleanups.forEach((fn) => fn());
|
|
97
|
+
store.changeCleanups.clear();
|
|
98
|
+
store.exitCleanups.forEach((fn) => fn());
|
|
99
|
+
store.exitCleanups.clear();
|
|
100
|
+
store.watchedValues.clear();
|
|
101
|
+
}
|
|
102
|
+
function createMachine(config) {
|
|
103
|
+
const effectStore = createEffectStore();
|
|
104
|
+
const send = ((event, input, ...args) => {
|
|
105
|
+
const context = computeValues(input, config.computed);
|
|
106
|
+
const payload = args[0];
|
|
107
|
+
const state = context.state;
|
|
108
|
+
if (state && config.states?.[state]?.on?.[event]) {
|
|
109
|
+
const stateHandler = config.states[state].on[event];
|
|
110
|
+
executeHandler(stateHandler, config.actions ?? {}, context, payload);
|
|
111
|
+
}
|
|
112
|
+
const globalHandler = config.on?.[event];
|
|
113
|
+
if (globalHandler) {
|
|
114
|
+
executeHandler(globalHandler, config.actions ?? {}, context, payload);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
const createEffectHelpersWithInput = (input) => ({
|
|
118
|
+
send: ((event, ...args) => {
|
|
119
|
+
send(event, input, ...args);
|
|
120
|
+
})
|
|
121
|
+
});
|
|
122
|
+
const evaluate = (input) => {
|
|
123
|
+
const context = computeValues(input, config.computed);
|
|
124
|
+
const effectHelpers = createEffectHelpersWithInput(input);
|
|
125
|
+
if (config.always && config.actions) {
|
|
126
|
+
const actionsMap = config.actions;
|
|
127
|
+
for (const rule of config.always) {
|
|
128
|
+
if (!rule.when || rule.when(context, void 0)) {
|
|
129
|
+
executeActions(rule.do, actionsMap, context, void 0);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
processEffects(config.effects, context, effectHelpers, effectStore);
|
|
135
|
+
};
|
|
136
|
+
const getComputed = (input) => {
|
|
137
|
+
const context = computeValues(input, config.computed);
|
|
138
|
+
if (!config.computed) return {};
|
|
139
|
+
const result = {};
|
|
140
|
+
for (const key in config.computed) {
|
|
141
|
+
result[key] = context[key];
|
|
142
|
+
}
|
|
143
|
+
return result;
|
|
144
|
+
};
|
|
145
|
+
const cleanup = () => clearEffectStore(effectStore);
|
|
146
|
+
return Object.assign(config, { send, evaluate, getComputed, cleanup });
|
|
147
|
+
}
|
|
148
|
+
export {
|
|
149
|
+
clearEffectStore,
|
|
150
|
+
computeValues,
|
|
151
|
+
createEffectStore,
|
|
152
|
+
createMachine,
|
|
153
|
+
effect,
|
|
154
|
+
executeActions,
|
|
155
|
+
executeHandler,
|
|
156
|
+
isRuleArray,
|
|
157
|
+
processEffects,
|
|
158
|
+
shallowEqual
|
|
159
|
+
};
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Controlled Machine\n *\n * A controlled state machine where state lives outside the machine.\n *\n * - input: External data passed in\n * - computed: Derived values from input\n * - context: input + computed (full context available in handlers)\n * - on: Event → conditional actions\n * - effects: Watch-based side effects\n * - always: Auto-evaluated rules on context change\n */\n\n// ============================================\n// Types\n// ============================================\n\nexport type Rule<\n TContext,\n TPayload = undefined,\n TActions extends string = string,\n> = {\n when?: (context: TContext, payload: TPayload) => boolean\n do: TActions | TActions[]\n}\n\nexport type Handler<\n TContext,\n TPayload = undefined,\n TActions extends string = string,\n> = TActions | TActions[] | Rule<TContext, TPayload, TActions>[]\n\n// Effect helpers - utilities available in effect callbacks\nexport type EffectHelpers<TEvents extends EventsConfig> = {\n send: Send<TEvents>\n}\n\nexport type Cleanup = () => void\n\nexport type Effect<\n TContext,\n TEvents extends EventsConfig,\n TWatched = unknown,\n> = {\n watch: (context: TContext) => TWatched\n enter?: (\n context: TContext,\n helpers: EffectHelpers<TEvents>,\n ) => void | Cleanup | Promise<void>\n exit?: (context: TContext, helpers: EffectHelpers<TEvents>) => void | Cleanup\n change?: (\n context: TContext,\n prev: TWatched | undefined,\n curr: TWatched,\n helpers: EffectHelpers<TEvents>,\n ) => void | Cleanup\n}\n\n/** Helper for inferring prev/curr types from watch return type */\nexport function effect<TContext, TEvents extends EventsConfig, TWatched>(\n config: Effect<TContext, TEvents, TWatched>,\n): Effect<TContext, TEvents, TWatched> {\n return config\n}\n\nexport type EventsConfig = Record<string, unknown>\nexport type ComputedConfig = Record<string, unknown>\n\n// ============================================\n// Object-based Generic Types\n// ============================================\n\n/**\n * Object-based generic types - specify only needed types in any order\n *\n * @example\n * createMachine<{\n * input: MyInput\n * events: MyEvents\n * actions: 'foo' | 'bar'\n * }>({...})\n */\nexport type MachineTypes = {\n input?: unknown\n events?: EventsConfig\n computed?: ComputedConfig\n actions?: string\n state?: string\n}\n\nexport type Input<T extends MachineTypes> = T['input']\nexport type Events<T extends MachineTypes> = T['events'] extends EventsConfig\n ? T['events']\n : Record<string, undefined>\nexport type Computed<T extends MachineTypes> = T['computed'] extends ComputedConfig\n ? T['computed']\n : Record<string, never>\nexport type Actions<T extends MachineTypes> = T['actions'] extends string\n ? T['actions']\n : string\nexport type State<T extends MachineTypes> = T['state'] extends string\n ? T['state']\n : string\n\n// Context = Input + Computed (full context available in handlers)\nexport type Context<T extends MachineTypes> = Input<T> & Computed<T>\n\n// State-based handler configuration\nexport type StateConfig<\n TContext,\n TEvents extends EventsConfig,\n TActions extends string = string,\n> = {\n on?: { [K in keyof TEvents]?: Handler<TContext, TEvents[K], TActions> }\n}\n\nexport type StatesConfig<\n TState extends string,\n TContext,\n TEvents extends EventsConfig,\n TActions extends string = string,\n> = {\n [K in TState]?: StateConfig<TContext, TEvents, TActions>\n}\n\nexport type Machine<T extends MachineTypes> = {\n computed?: {\n [K in keyof Computed<T>]: (input: Input<T>) => Computed<T>[K]\n }\n on?: {\n [K in keyof Events<T>]?: Handler<Context<T>, Events<T>[K], Actions<T>>\n }\n states?: StatesConfig<State<T>, Context<T>, Events<T>, Actions<T>>\n always?: Rule<Context<T>, undefined, Actions<T>>[]\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n effects?: Effect<Context<T>, Events<T>, any>[]\n actions?: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [K in Actions<T>]: (context: Context<T>, payload?: any) => void\n }\n}\n\nexport type Send<TEvents extends EventsConfig> = <K extends keyof TEvents>(\n event: K,\n ...args: TEvents[K] extends undefined ? [] : [payload: TEvents[K]]\n) => void\n\n// createMachine return type\nexport type MachineInstance<T extends MachineTypes> = Machine<T> & {\n send: <K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => void\n evaluate: (input: Input<T>) => void\n getComputed: (input: Input<T>) => Computed<T>\n cleanup: () => void\n}\n\n// ============================================\n// Core Logic (Pure)\n// ============================================\n\nexport function executeActions<TContext, TPayload>(\n actionNames: string | string[],\n actions: Record<string, (context: TContext, payload?: TPayload) => void>,\n context: TContext,\n payload: TPayload,\n): void {\n if (typeof actionNames === 'string') {\n actions[actionNames]?.(context, payload)\n } else {\n for (const name of actionNames) {\n actions[name]?.(context, payload)\n }\n }\n}\n\nexport function isRuleArray<TContext, TPayload, TActions extends string>(\n handler: Handler<TContext, TPayload, TActions>,\n): handler is Rule<TContext, TPayload, TActions>[] {\n return (\n Array.isArray(handler) &&\n handler.length > 0 &&\n typeof handler[0] === 'object' &&\n 'do' in handler[0]\n )\n}\n\nexport function executeHandler<TContext, TPayload>(\n handler: Handler<TContext, TPayload>,\n actions: Record<string, (context: TContext, payload?: TPayload) => void>,\n context: TContext,\n payload: TPayload,\n): void {\n // Single action or action array\n if (typeof handler === 'string' || (Array.isArray(handler) && !isRuleArray(handler))) {\n executeActions(handler as string | string[], actions, context, payload)\n return\n }\n\n // Rule array\n for (const rule of handler as Rule<TContext, TPayload>[]) {\n if (!rule.when || rule.when(context, payload)) {\n executeActions(rule.do, actions, context, payload)\n break\n }\n }\n}\n\nexport function computeValues<TContext, TComputed extends ComputedConfig>(\n context: TContext,\n computed?: { [K in keyof TComputed]: (context: TContext) => TComputed[K] },\n): TContext & TComputed {\n if (!computed) return context as TContext & TComputed\n\n const values = {} as TComputed\n for (const key in computed) {\n values[key] = computed[key](context)\n }\n return { ...context, ...values }\n}\n\n/**\n * Shallow comparison function - for composite watch support\n *\n * Arrays: length + === comparison for each element\n * Others: === comparison\n */\nexport function shallowEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true\n\n // Array comparison\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n return a.every((v, i) => v === b[i])\n }\n\n return false\n}\n\n// Effect store for tracking state\nexport type EffectStore = {\n watchedValues: Map<number, unknown>\n enterCleanups: Map<number, () => void>\n changeCleanups: Map<number, () => void>\n exitCleanups: Map<number, () => void>\n}\n\nexport function createEffectStore(): EffectStore {\n return {\n watchedValues: new Map(),\n enterCleanups: new Map(),\n changeCleanups: new Map(),\n exitCleanups: new Map(),\n }\n}\n\n/**\n * Common effects processing logic\n */\nexport function processEffects<TContext, TEvents extends EventsConfig>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n effects: Effect<TContext, TEvents, any>[] | undefined,\n context: TContext,\n effectHelpers: EffectHelpers<TEvents>,\n store: EffectStore,\n): void {\n if (!effects) return\n\n effects.forEach((effect, i) => {\n const prev = store.watchedValues.get(i)\n const curr = effect.watch(context)\n\n if (!shallowEqual(prev, curr)) {\n // cleanup previous enter\n const enterCleanup = store.enterCleanups.get(i)\n if (enterCleanup) {\n enterCleanup()\n store.enterCleanups.delete(i)\n }\n\n // cleanup previous change\n const changeCleanup = store.changeCleanups.get(i)\n if (changeCleanup) {\n changeCleanup()\n store.changeCleanups.delete(i)\n }\n\n // change callback (can return cleanup)\n const changeResult = effect.change?.(context, prev, curr, effectHelpers)\n if (typeof changeResult === 'function') {\n store.changeCleanups.set(i, changeResult)\n }\n\n // enter (falsy → truthy)\n if (!prev && curr) {\n // cleanup previous exit\n const exitCleanup = store.exitCleanups.get(i)\n if (exitCleanup) {\n exitCleanup()\n store.exitCleanups.delete(i)\n }\n\n const enterResult = effect.enter?.(context, effectHelpers)\n if (typeof enterResult === 'function') {\n store.enterCleanups.set(i, enterResult)\n }\n }\n\n // exit (truthy → falsy)\n if (prev && !curr) {\n const exitResult = effect.exit?.(context, effectHelpers)\n if (typeof exitResult === 'function') {\n store.exitCleanups.set(i, exitResult)\n }\n }\n\n store.watchedValues.set(i, curr)\n }\n })\n}\n\n/**\n * Clear effect store\n */\nexport function clearEffectStore(store: EffectStore): void {\n store.enterCleanups.forEach((fn) => fn())\n store.enterCleanups.clear()\n store.changeCleanups.forEach((fn) => fn())\n store.changeCleanups.clear()\n store.exitCleanups.forEach((fn) => fn())\n store.exitCleanups.clear()\n store.watchedValues.clear()\n}\n\n// ============================================\n// Vanilla (non-React) + Type inference helper\n// ============================================\n\nexport function createMachine<T extends MachineTypes>(\n config: Machine<T>,\n): MachineInstance<T> {\n const effectStore = createEffectStore()\n\n const send = (<K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n const context = computeValues(input, config.computed)\n const payload = args[0] as Events<T>[K]\n\n // 1. State-specific handler first\n const state = (context as { state?: State<T> }).state\n if (state && config.states?.[state]?.on?.[event]) {\n const stateHandler = config.states[state].on[event]\n executeHandler(stateHandler, config.actions ?? {}, context, payload)\n }\n\n // 2. Global handler\n const globalHandler = config.on?.[event]\n if (globalHandler) {\n executeHandler(globalHandler, config.actions ?? {}, context, payload)\n }\n }) as <K extends keyof Events<T>>(\n event: K,\n input: Input<T>,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => void\n\n // vanilla send wrapper (with input binding)\n const createEffectHelpersWithInput = (input: Input<T>): EffectHelpers<Events<T>> => ({\n send: (<K extends keyof Events<T>>(\n event: K,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n send(event, input, ...args)\n }) as Send<Events<T>>,\n })\n\n const evaluate = (input: Input<T>) => {\n const context = computeValues(input, config.computed)\n const effectHelpers = createEffectHelpersWithInput(input)\n\n // always\n if (config.always && config.actions) {\n const actionsMap = config.actions as Record<\n string,\n (context: Context<T>) => void\n >\n for (const rule of config.always) {\n if (!rule.when || rule.when(context, undefined)) {\n executeActions(rule.do, actionsMap, context, undefined)\n break\n }\n }\n }\n\n // effects\n processEffects(config.effects, context, effectHelpers, effectStore)\n }\n\n const getComputed = (input: Input<T>): Computed<T> => {\n const context = computeValues(input, config.computed)\n if (!config.computed) return {} as Computed<T>\n const result = {} as Computed<T>\n for (const key in config.computed) {\n result[key] = context[key]\n }\n return result\n }\n\n const cleanup = () => clearEffectStore(effectStore)\n\n return Object.assign(config, { send, evaluate, getComputed, cleanup })\n}\n"],"names":["effect"],"mappings":"AA2DO,SAAS,OACd,QACqC;AACrC,SAAO;AACT;AAoGO,SAAS,eACd,aACA,SACA,SACA,SACM;AACN,MAAI,OAAO,gBAAgB,UAAU;AACnC,YAAQ,WAAW,IAAI,SAAS,OAAO;AAAA,EACzC,OAAO;AACL,eAAW,QAAQ,aAAa;AAC9B,cAAQ,IAAI,IAAI,SAAS,OAAO;AAAA,IAClC;AAAA,EACF;AACF;AAEO,SAAS,YACd,SACiD;AACjD,SACE,MAAM,QAAQ,OAAO,KACrB,QAAQ,SAAS,KACjB,OAAO,QAAQ,CAAC,MAAM,YACtB,QAAQ,QAAQ,CAAC;AAErB;AAEO,SAAS,eACd,SACA,SACA,SACA,SACM;AAEN,MAAI,OAAO,YAAY,YAAa,MAAM,QAAQ,OAAO,KAAK,CAAC,YAAY,OAAO,GAAI;AACpF,mBAAe,SAA8B,SAAS,SAAS,OAAO;AACtE;AAAA,EACF;AAGA,aAAW,QAAQ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;AAClD,qBAAe,cAAc,OAAO,WAAW,CAAA,GAAI,SAAS,OAAO;AAAA,IACrE;AAGA,UAAM,gBAAgB,OAAO,KAAK,KAAK;AACvC,QAAI,eAAe;AACjB,qBAAe,eAAe,OAAO,WAAW,CAAA,GAAI,SAAS,OAAO;AAAA,IACtE;AAAA,EACF;AAOA,QAAM,+BAA+B,CAAC,WAA+C;AAAA,IACnF,OAAO,CACL,UACG,SACA;AACH,WAAK,OAAO,OAAO,GAAG,IAAI;AAAA,IAC5B;AAAA,EAAA;AAGF,QAAM,WAAW,CAAC,UAAoB;AACpC,UAAM,UAAU,cAAc,OAAO,OAAO,QAAQ;AACpD,UAAM,gBAAgB,6BAA6B,KAAK;AAGxD,QAAI,OAAO,UAAU,OAAO,SAAS;AACnC,YAAM,aAAa,OAAO;AAI1B,iBAAW,QAAQ,OAAO,QAAQ;AAChC,YAAI,CAAC,KAAK,QAAQ,KAAK,KAAK,SAAS,MAAS,GAAG;AAC/C,yBAAe,KAAK,IAAI,YAAY,SAAS,MAAS;AACtD;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAGA,mBAAe,OAAO,SAAS,SAAS,eAAe,WAAW;AAAA,EACpE;AAEA,QAAM,cAAc,CAAC,UAAiC;AACpD,UAAM,UAAU,cAAc,OAAO,OAAO,QAAQ;AACpD,QAAI,CAAC,OAAO,SAAU,QAAO,CAAA;AAC7B,UAAM,SAAS,CAAA;AACf,eAAW,OAAO,OAAO,UAAU;AACjC,aAAO,GAAG,IAAI,QAAQ,GAAG;AAAA,IAC3B;AACA,WAAO;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,iBAAiB,WAAW;AAElD,SAAO,OAAO,OAAO,QAAQ,EAAE,MAAM,UAAU,aAAa,SAAS;AACvE;"}
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const index = require("./index.cjs");
|
|
5
|
+
function useMachine(machine, input) {
|
|
6
|
+
const inputRef = react.useRef(input);
|
|
7
|
+
const machineRef = react.useRef(machine);
|
|
8
|
+
const isMountedRef = react.useRef(true);
|
|
9
|
+
inputRef.current = input;
|
|
10
|
+
machineRef.current = machine;
|
|
11
|
+
const { computed: computedDef } = machine;
|
|
12
|
+
const context = react.useMemo(
|
|
13
|
+
() => index.computeValues(input, computedDef),
|
|
14
|
+
[input, computedDef]
|
|
15
|
+
);
|
|
16
|
+
const computed = react.useMemo(() => {
|
|
17
|
+
if (!computedDef) return {};
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const key in computedDef) {
|
|
20
|
+
result[key] = context[key];
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}, [context, computedDef]);
|
|
24
|
+
const contextRef = react.useRef(context);
|
|
25
|
+
contextRef.current = context;
|
|
26
|
+
const prevContextRef = react.useRef(context);
|
|
27
|
+
const effectStoreRef = react.useRef({
|
|
28
|
+
watchedValues: /* @__PURE__ */ new Map(),
|
|
29
|
+
enterCleanups: /* @__PURE__ */ new Map(),
|
|
30
|
+
changeCleanups: /* @__PURE__ */ new Map(),
|
|
31
|
+
exitCleanups: /* @__PURE__ */ new Map()
|
|
32
|
+
});
|
|
33
|
+
const { always, actions } = machine;
|
|
34
|
+
if (prevContextRef.current !== context && always && actions) {
|
|
35
|
+
const actionsMap = actions;
|
|
36
|
+
for (const rule of always) {
|
|
37
|
+
if (!rule.when || rule.when(context, void 0)) {
|
|
38
|
+
index.executeActions(rule.do, actionsMap, context, void 0);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
prevContextRef.current = context;
|
|
44
|
+
const send = react.useCallback(
|
|
45
|
+
(event, ...args) => {
|
|
46
|
+
const currentMachine = machineRef.current;
|
|
47
|
+
const currentInput = inputRef.current;
|
|
48
|
+
const currentContext = index.computeValues(
|
|
49
|
+
currentInput,
|
|
50
|
+
currentMachine.computed
|
|
51
|
+
);
|
|
52
|
+
const payload = args[0];
|
|
53
|
+
const state2 = currentContext.state;
|
|
54
|
+
if (state2 && currentMachine.states?.[state2]?.on?.[event]) {
|
|
55
|
+
const stateHandler = currentMachine.states[state2].on[event];
|
|
56
|
+
index.executeHandler(
|
|
57
|
+
stateHandler,
|
|
58
|
+
currentMachine.actions ?? {},
|
|
59
|
+
currentContext,
|
|
60
|
+
payload
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
const globalHandler = currentMachine.on?.[event];
|
|
64
|
+
if (globalHandler) {
|
|
65
|
+
index.executeHandler(
|
|
66
|
+
globalHandler,
|
|
67
|
+
currentMachine.actions ?? {},
|
|
68
|
+
currentContext,
|
|
69
|
+
payload
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
[]
|
|
74
|
+
// no dependencies - uses refs
|
|
75
|
+
);
|
|
76
|
+
const safeSend = react.useCallback(
|
|
77
|
+
(event, ...args) => {
|
|
78
|
+
if (!isMountedRef.current) return;
|
|
79
|
+
send(event, ...args);
|
|
80
|
+
},
|
|
81
|
+
[send]
|
|
82
|
+
);
|
|
83
|
+
const effectHelpers = react.useMemo(
|
|
84
|
+
() => ({ send: safeSend }),
|
|
85
|
+
[safeSend]
|
|
86
|
+
);
|
|
87
|
+
const { effects } = machine;
|
|
88
|
+
react.useEffect(() => {
|
|
89
|
+
index.processEffects(effects, context, effectHelpers, effectStoreRef.current);
|
|
90
|
+
}, [context, effects, effectHelpers]);
|
|
91
|
+
react.useEffect(() => {
|
|
92
|
+
isMountedRef.current = true;
|
|
93
|
+
const store = effectStoreRef.current;
|
|
94
|
+
return () => {
|
|
95
|
+
isMountedRef.current = false;
|
|
96
|
+
index.clearEffectStore(store);
|
|
97
|
+
};
|
|
98
|
+
}, []);
|
|
99
|
+
const state = context.state ?? "";
|
|
100
|
+
return { send, computed, state };
|
|
101
|
+
}
|
|
102
|
+
exports.useMachine = useMachine;
|
|
103
|
+
//# sourceMappingURL=react.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.cjs","sources":["../src/react.ts"],"sourcesContent":["/**\n * Controlled Machine - React Integration\n *\n * React hook for using controlled-machine in React components.\n */\n\nimport { useCallback, useRef, useEffect, useMemo } from 'react'\nimport {\n type MachineTypes,\n type Machine,\n type Events,\n type Computed,\n type State,\n type Send,\n type EffectHelpers,\n type EffectStore,\n type Context,\n computeValues,\n executeActions,\n executeHandler,\n processEffects,\n clearEffectStore,\n} from './index'\n\n// ============================================\n// React Hook\n// ============================================\n\nexport function useMachine<T extends MachineTypes>(\n machine: Machine<T>,\n input: T['input'],\n): { send: Send<Events<T>>; computed: Computed<T>; state: State<T> } {\n // refs for stable callbacks\n const inputRef = useRef(input)\n const machineRef = useRef(machine)\n const isMountedRef = useRef(true)\n\n inputRef.current = input\n machineRef.current = machine\n\n // compute values\n const { computed: computedDef } = machine\n const context = useMemo(\n () => computeValues(input, computedDef),\n [input, computedDef],\n )\n\n // extract computed only\n const computed = useMemo(() => {\n if (!computedDef) return {} as Computed<T>\n const result = {} as Computed<T>\n for (const key in computedDef) {\n result[key] = context[key]\n }\n return result\n }, [context, computedDef])\n\n const contextRef = useRef(context)\n contextRef.current = context\n\n const prevContextRef = useRef<Context<T>>(context)\n const effectStoreRef = useRef<EffectStore>({\n watchedValues: new Map(),\n enterCleanups: new Map(),\n changeCleanups: new Map(),\n exitCleanups: new Map(),\n })\n\n // always: auto-evaluate when context changes (synchronous, during render)\n const { always, actions } = machine\n if (prevContextRef.current !== context && always && actions) {\n const actionsMap = actions as Record<string, (context: Context<T>) => void>\n for (const rule of always) {\n if (!rule.when || rule.when(context, undefined)) {\n executeActions(rule.do, actionsMap, context, undefined)\n break\n }\n }\n }\n prevContextRef.current = context\n\n // send: stable function (no deps, uses refs)\n const send: Send<Events<T>> = useCallback(\n <K extends keyof Events<T>>(\n event: K,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n const currentMachine = machineRef.current\n const currentInput = inputRef.current\n const currentContext = computeValues(\n currentInput,\n currentMachine.computed,\n )\n const payload = args[0] as Events<T>[K]\n\n // 1. State-specific handler first\n const state = (currentContext as { state?: State<T> }).state\n if (state && currentMachine.states?.[state]?.on?.[event]) {\n const stateHandler = currentMachine.states[state].on![event]!\n executeHandler(\n stateHandler,\n currentMachine.actions ?? {},\n currentContext,\n payload,\n )\n }\n\n // 2. Global handler\n const globalHandler = currentMachine.on?.[event]\n if (globalHandler) {\n executeHandler(\n globalHandler,\n currentMachine.actions ?? {},\n currentContext,\n payload,\n )\n }\n },\n [], // no dependencies - uses refs\n )\n\n // safeSend: won't be called after unmount\n const safeSend: Send<Events<T>> = useCallback(\n <K extends keyof Events<T>>(\n event: K,\n ...args: Events<T>[K] extends undefined ? [] : [payload: Events<T>[K]]\n ) => {\n if (!isMountedRef.current) return\n send(event, ...args)\n },\n [send],\n )\n\n // effect helpers\n const effectHelpers: EffectHelpers<Events<T>> = useMemo(\n () => ({ send: safeSend }),\n [safeSend],\n )\n\n // effects: detect watch value changes\n const { effects } = machine\n useEffect(() => {\n processEffects(effects, context, effectHelpers, effectStoreRef.current)\n }, [context, effects, effectHelpers])\n\n // mount/unmount management\n useEffect(() => {\n isMountedRef.current = true\n const store = effectStoreRef.current\n return () => {\n isMountedRef.current = false\n clearEffectStore(store)\n }\n }, [])\n\n // state from context (default to empty string if not provided)\n const state = (context as { state?: State<T> }).state ?? ('' as State<T>)\n\n return { send, computed, state }\n}\n\n// Re-export types for convenience\nexport type { Send } from './index'\n"],"names":["useRef","useMemo","computeValues","executeActions","useCallback","state","executeHandler","useEffect","processEffects","clearEffectStore"],"mappings":";;;;AA4BO,SAAS,WACd,SACA,OACmE;AAEnE,QAAM,WAAWA,MAAAA,OAAO,KAAK;AAC7B,QAAM,aAAaA,MAAAA,OAAO,OAAO;AACjC,QAAM,eAAeA,MAAAA,OAAO,IAAI;AAEhC,WAAS,UAAU;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