@doeixd/machine 1.0.3 → 1.2.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.
Files changed (37) hide show
  1. package/README.md +48 -0
  2. package/dist/cjs/development/core.js.map +1 -1
  3. package/dist/cjs/development/delegate.js +89 -0
  4. package/dist/cjs/development/delegate.js.map +7 -0
  5. package/dist/cjs/development/index.js +383 -158
  6. package/dist/cjs/development/index.js.map +4 -4
  7. package/dist/cjs/development/minimal.js +172 -0
  8. package/dist/cjs/development/minimal.js.map +7 -0
  9. package/dist/cjs/development/react.js.map +1 -1
  10. package/dist/cjs/production/delegate.js +1 -0
  11. package/dist/cjs/production/index.js +3 -3
  12. package/dist/cjs/production/minimal.js +1 -0
  13. package/dist/esm/development/core.js.map +1 -1
  14. package/dist/esm/development/delegate.js +68 -0
  15. package/dist/esm/development/delegate.js.map +7 -0
  16. package/dist/esm/development/index.js +389 -158
  17. package/dist/esm/development/index.js.map +4 -4
  18. package/dist/esm/development/minimal.js +149 -0
  19. package/dist/esm/development/minimal.js.map +7 -0
  20. package/dist/esm/development/react.js.map +1 -1
  21. package/dist/esm/production/delegate.js +1 -0
  22. package/dist/esm/production/index.js +3 -3
  23. package/dist/esm/production/minimal.js +1 -0
  24. package/dist/types/delegate.d.ts +101 -0
  25. package/dist/types/delegate.d.ts.map +1 -0
  26. package/dist/types/index.d.ts +3 -0
  27. package/dist/types/index.d.ts.map +1 -1
  28. package/dist/types/minimal.d.ts +95 -0
  29. package/dist/types/minimal.d.ts.map +1 -0
  30. package/dist/types/types.d.ts +110 -0
  31. package/dist/types/types.d.ts.map +1 -0
  32. package/package.json +25 -1
  33. package/src/delegate.ts +267 -0
  34. package/src/index.ts +13 -0
  35. package/src/middleware.ts +1049 -1050
  36. package/src/minimal.ts +269 -0
  37. package/src/types.ts +129 -0
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/delegate.ts
21
+ var delegate_exports = {};
22
+ __export(delegate_exports, {
23
+ createDelegate: () => createDelegate,
24
+ delegate: () => delegate,
25
+ delegateAll: () => delegateAll,
26
+ renameMap: () => renameMap
27
+ });
28
+ module.exports = __toCommonJS(delegate_exports);
29
+ function delegate(ctx, key, next, options) {
30
+ const child = ctx[key];
31
+ const delegated = {};
32
+ const allTransitions = Object.keys(child).filter(
33
+ (k) => typeof child[k] === "function"
34
+ );
35
+ let transitionMap;
36
+ if (!options) {
37
+ transitionMap = Object.fromEntries(allTransitions.map((t) => [t, t]));
38
+ } else if ("pick" in options) {
39
+ transitionMap = Object.fromEntries(
40
+ options.pick.filter((t) => allTransitions.includes(t)).map((t) => [t, t])
41
+ );
42
+ } else if ("omit" in options) {
43
+ const omitSet = new Set(options.omit);
44
+ transitionMap = Object.fromEntries(
45
+ allTransitions.filter((t) => !omitSet.has(t)).map((t) => [t, t])
46
+ );
47
+ } else if ("rename" in options) {
48
+ transitionMap = Object.fromEntries(
49
+ Object.entries(options.rename).filter(
50
+ ([childName]) => allTransitions.includes(childName)
51
+ )
52
+ );
53
+ } else {
54
+ transitionMap = {};
55
+ }
56
+ for (const [childName, parentName] of Object.entries(transitionMap)) {
57
+ const childTransition = child[childName];
58
+ delegated[parentName] = (...args) => {
59
+ const nextChild = childTransition.apply(child, args);
60
+ return next({ ...ctx, [key]: nextChild });
61
+ };
62
+ }
63
+ return delegated;
64
+ }
65
+ function createDelegate(ctx, next) {
66
+ return (key, options) => delegate(ctx, key, next, options);
67
+ }
68
+ function delegateAll(ctx, keys, next, prefix = false) {
69
+ const result = {};
70
+ for (const key of keys) {
71
+ const child = ctx[key];
72
+ const transitions = Object.keys(child).filter(
73
+ (k) => typeof child[k] === "function"
74
+ );
75
+ for (const transitionName of transitions) {
76
+ const parentName = prefix ? `${String(key)}_${transitionName}` : transitionName;
77
+ const childTransition = child[transitionName];
78
+ result[parentName] = (...args) => {
79
+ const nextChild = childTransition.apply(child, args);
80
+ return next({ ...ctx, [key]: nextChild });
81
+ };
82
+ }
83
+ }
84
+ return result;
85
+ }
86
+ function renameMap() {
87
+ return (mapping) => mapping;
88
+ }
89
+ //# sourceMappingURL=delegate.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/delegate.ts"],
4
+ "sourcesContent": ["// ============================================================================\n// DELEGATION: delegate()\n// ============================================================================\n\n/**\n * Extracts the names of all function properties (transitions) from a type.\n */\ntype TransitionNamesOf<T> = {\n [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;\n}[keyof T];\n\n/**\n * Extracts the argument types of a function.\n */\ntype ArgsOf<F> = F extends (...args: infer A) => any ? A : never;\n\n/**\n * Creates delegated transitions that forward to a child and return the parent.\n * \n * @typeParam Child - Child machine type\n * @typeParam Parent - Parent machine return type\n */\ntype DelegatedTransitions<\n Child extends object,\n Parent\n> = {\n [K in TransitionNamesOf<Child>]: (\n ...args: ArgsOf<Child[K]>\n ) => Parent;\n };\n\n/**\n * Renamed delegated transitions using a mapping object.\n * \n * @typeParam Child - Child machine type\n * @typeParam Parent - Parent machine return type\n * @typeParam Mapping - Object mapping child transition names to parent names\n */\ntype RenamedDelegatedTransitions<\n Child extends object,\n Parent,\n Mapping extends Partial<Record<TransitionNamesOf<Child>, string>>\n> = {\n [K in keyof Mapping as Mapping[K] extends string ? Mapping[K] : never]: K extends TransitionNamesOf<Child>\n ? (...args: ArgsOf<Child[K]>) => Parent\n : never;\n };\n\n/**\n * Subset of delegated transitions.\n * \n * @typeParam Child - Child machine type\n * @typeParam Parent - Parent machine return type\n * @typeParam Keys - Subset of child transition names to delegate\n */\ntype PickedDelegatedTransitions<\n Child extends object,\n Parent,\n Keys extends TransitionNamesOf<Child>\n> = {\n [K in Keys]: (...args: ArgsOf<Child[K]>) => Parent;\n };\n\n/**\n * Options for delegate function.\n */\ntype DelegateOptions<Child extends object> =\n | { pick: Array<TransitionNamesOf<Child>> }\n | { omit: Array<TransitionNamesOf<Child>> }\n | { rename: Partial<Record<TransitionNamesOf<Child>, string>> };\n\n/**\n * Delegates child machine transitions to the parent level.\n * \n * When a delegated transition is called on the parent, it:\n * 1. Calls the corresponding transition on the child\n * 2. Updates the parent's context with the new child state\n * 3. Returns a new parent machine\n * \n * This enables \"flat\" composition where child transitions appear directly\n * on the parent, as opposed to `withChildren()` which namespaces them.\n * \n * @typeParam Ctx - Parent context type\n * @typeParam Key - Key where child is stored in parent context\n * @typeParam R - Parent machine return type (e.g. Machine<Ctx, T>)\n * \n * @param ctx - Current parent context (from transition factory)\n * @param key - Property key of the child machine in context\n * @param next - The `next` function from the parent's transition factory\n * @param options - Optional: pick, omit, or rename specific transitions\n * \n * @returns Object of delegated transitions to spread into parent's transitions\n */\nexport function delegate<\n Ctx extends object,\n Key extends keyof Ctx,\n R,\n Child extends Ctx[Key] & object = Ctx[Key] & object\n>(\n ctx: Ctx,\n key: Key,\n next: (c: Ctx) => R\n): DelegatedTransitions<Child, R>;\n\nexport function delegate<\n Ctx extends object,\n Key extends keyof Ctx,\n R,\n Child extends Ctx[Key] & object,\n Keys extends TransitionNamesOf<Child>\n>(\n ctx: Ctx,\n key: Key,\n next: (c: Ctx) => R,\n options: { pick: Keys[] }\n): PickedDelegatedTransitions<Child, R, Keys>;\n\nexport function delegate<\n Ctx extends object,\n Key extends keyof Ctx,\n R,\n Child extends Ctx[Key] & object,\n Keys extends TransitionNamesOf<Child>\n>(\n ctx: Ctx,\n key: Key,\n next: (c: Ctx) => R,\n options: { omit: Keys[] }\n): DelegatedTransitions<Omit<Child, Keys>, R>;\n\nexport function delegate<\n Ctx extends object,\n Key extends keyof Ctx,\n R,\n Child extends Ctx[Key] & object,\n Mapping extends Partial<Record<TransitionNamesOf<Child>, string>>\n>(\n ctx: Ctx,\n key: Key,\n next: (c: Ctx) => R,\n options: { rename: Mapping }\n): RenamedDelegatedTransitions<Child, R, Mapping>;\n\nexport function delegate<\n Ctx extends object,\n Key extends keyof Ctx,\n R\n>(\n ctx: Ctx,\n key: Key,\n next: (c: Ctx) => R,\n options?: DelegateOptions<Ctx[Key] & object>\n): Record<string, (...args: unknown[]) => R> {\n const child = ctx[key] as Record<string, unknown>;\n const delegated: Record<string, (...args: unknown[]) => R> = {};\n\n // Get all function property names from child\n const allTransitions = Object.keys(child).filter(\n (k) => typeof child[k] === 'function'\n );\n\n // Determine which transitions to include and how to name them\n let transitionMap: Record<string, string>; // childName -> parentName\n\n if (!options) {\n // Delegate all with same names\n transitionMap = Object.fromEntries(allTransitions.map((t) => [t, t]));\n } else if ('pick' in options) {\n // Only picked transitions\n transitionMap = Object.fromEntries(\n (options.pick as string[]).filter((t) => allTransitions.includes(t)).map((t) => [t, t])\n );\n } else if ('omit' in options) {\n // All except omitted\n const omitSet = new Set(options.omit as string[]);\n transitionMap = Object.fromEntries(\n allTransitions.filter((t) => !omitSet.has(t)).map((t) => [t, t])\n );\n } else if ('rename' in options) {\n // Only renamed transitions with new names\n transitionMap = Object.fromEntries(\n Object.entries(options.rename as Record<string, string>).filter(\n ([childName]) => allTransitions.includes(childName)\n )\n );\n } else {\n transitionMap = {};\n }\n\n // Create delegated transitions\n for (const [childName, parentName] of Object.entries(transitionMap)) {\n const childTransition = child[childName] as (...args: unknown[]) => unknown;\n\n delegated[parentName] = (...args: unknown[]) => {\n const nextChild = childTransition.apply(child, args);\n return next({ ...ctx, [key]: nextChild } as Ctx);\n };\n }\n\n return delegated;\n}\n\n/**\n * Type helper to get transition names from a machine or object.\n * Useful for type-safe pick/omit/rename options.\n */\nexport type TransitionsOf<M> = TransitionNamesOf<M>;\n\n// ============================================================================\n// DELEGATION UTILITIES\n// ============================================================================\n\n/**\n * Creates a delegate helper bound to a specific context and next function.\n * Useful when delegating multiple children to avoid repetition.\n */\nexport function createDelegate<Ctx extends object, R>(\n ctx: Ctx,\n next: (c: Ctx) => R\n) {\n return <Key extends keyof Ctx>(\n key: Key,\n options?: DelegateOptions<Ctx[Key] & object>\n ) => delegate(ctx, key, next, options as any);\n}\n\n/**\n * Delegates all transitions from multiple children, optionally with a prefix.\n */\nexport function delegateAll<\n Ctx extends object,\n Keys extends keyof Ctx,\n R\n>(\n ctx: Ctx,\n keys: Keys[],\n next: (c: Ctx) => R,\n prefix: boolean = false\n): Record<string, (...args: unknown[]) => R> {\n const result: Record<string, (...args: unknown[]) => R> = {};\n\n for (const key of keys) {\n const child = ctx[key] as Record<string, unknown>;\n const transitions = Object.keys(child).filter(\n (k) => typeof child[k] === 'function'\n );\n\n for (const transitionName of transitions) {\n const parentName = prefix ? `${String(key)}_${transitionName}` : transitionName;\n const childTransition = child[transitionName] as (...args: unknown[]) => unknown;\n\n result[parentName] = (...args: unknown[]) => {\n const nextChild = childTransition.apply(child, args);\n return next({ ...ctx, [key]: nextChild } as Ctx);\n };\n }\n }\n\n return result;\n}\n\n/**\n * Type-safe helper to create a rename mapping for delegate.\n */\nexport function renameMap<M extends object>() {\n return <T extends Partial<Record<TransitionNamesOf<M>, string>>>(mapping: T): T => mapping;\n}"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+IO,SAAS,SAKd,KACA,KACA,MACA,SAC2C;AAC3C,QAAM,QAAQ,IAAI,GAAG;AACrB,QAAM,YA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