@doeixd/machine 1.0.2 → 1.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.
Files changed (51) hide show
  1. package/README.md +48 -0
  2. package/dist/cjs/development/core.js +11 -9
  3. package/dist/cjs/development/core.js.map +3 -3
  4. package/dist/cjs/development/delegate.js +89 -0
  5. package/dist/cjs/development/delegate.js.map +7 -0
  6. package/dist/cjs/development/index.js +385 -167
  7. package/dist/cjs/development/index.js.map +4 -4
  8. package/dist/cjs/development/minimal.js +163 -0
  9. package/dist/cjs/development/minimal.js.map +7 -0
  10. package/dist/cjs/development/react.js +11 -9
  11. package/dist/cjs/development/react.js.map +3 -3
  12. package/dist/cjs/production/core.js +1 -1
  13. package/dist/cjs/production/delegate.js +1 -0
  14. package/dist/cjs/production/index.js +3 -3
  15. package/dist/cjs/production/minimal.js +1 -0
  16. package/dist/cjs/production/react.js +1 -1
  17. package/dist/esm/development/core.js +11 -9
  18. package/dist/esm/development/core.js.map +3 -3
  19. package/dist/esm/development/delegate.js +68 -0
  20. package/dist/esm/development/delegate.js.map +7 -0
  21. package/dist/esm/development/index.js +391 -167
  22. package/dist/esm/development/index.js.map +4 -4
  23. package/dist/esm/development/minimal.js +140 -0
  24. package/dist/esm/development/minimal.js.map +7 -0
  25. package/dist/esm/development/react.js +11 -9
  26. package/dist/esm/development/react.js.map +3 -3
  27. package/dist/esm/production/core.js +1 -1
  28. package/dist/esm/production/delegate.js +1 -0
  29. package/dist/esm/production/index.js +3 -3
  30. package/dist/esm/production/minimal.js +1 -0
  31. package/dist/esm/production/react.js +1 -1
  32. package/dist/types/base.d.ts +56 -0
  33. package/dist/types/base.d.ts.map +1 -0
  34. package/dist/types/delegate.d.ts +101 -0
  35. package/dist/types/delegate.d.ts.map +1 -0
  36. package/dist/types/higher-order.d.ts +2 -1
  37. package/dist/types/higher-order.d.ts.map +1 -1
  38. package/dist/types/index.d.ts +4 -49
  39. package/dist/types/index.d.ts.map +1 -1
  40. package/dist/types/minimal.d.ts +95 -0
  41. package/dist/types/minimal.d.ts.map +1 -0
  42. package/dist/types/types.d.ts +63 -0
  43. package/dist/types/types.d.ts.map +1 -0
  44. package/package.json +25 -1
  45. package/src/base.ts +62 -0
  46. package/src/delegate.ts +267 -0
  47. package/src/higher-order.ts +2 -2
  48. package/src/index.ts +15 -55
  49. package/src/middleware.ts +1049 -1050
  50. package/src/minimal.ts +269 -0
  51. package/src/types.ts +85 -0
package/README.md CHANGED
@@ -59,6 +59,7 @@ type Machine<C extends object> = {
59
59
  **Flexibility**: Unlike rigid FSM implementations, you can choose your level of immutability. Want to mutate? You can. Want pure functions? You can. Want compile-time state validation? Type-State Programming gives you that.
60
60
 
61
61
  **Read more about our core principles:** [ 📖 Core Principles Guide ](./docs/principles.md)
62
+ **Explore our utility helpers:** [ 🏷️ Tagged Helpers & Utilities ](./docs/tagged-helpers.md)
62
63
 
63
64
  ## Choosing the Right Pattern
64
65
 
@@ -2380,6 +2381,53 @@ createMultiMachine<C extends object, T extends MultiMachineBase<C>>(
2380
2381
  ): C & T
2381
2382
  ```
2382
2383
 
2384
+
2385
+ ## 📦 Submodules
2386
+
2387
+ For specialized use cases, you can import from specific submodules to reduce bundle size or use advanced inference features:
2388
+
2389
+ - **`@doeixd/machine/minimal`**: Lightweight API with "magic" type inference. [📖
2390
+ Guide](./docs/minimal.md)
2391
+ - **`@doeixd/machine/delegate`**: Utilities for composing machines via delegation. [📖
2392
+ Guide](./docs/delegate.md)
2393
+ - **`@doeixd/machine/react`**: React integration (hooks and components).
2394
+ - **`@doeixd/machine/extract`**: Build-time statechart extraction tools.
2395
+
2396
+ ## 🚀 Minimal API & Perfect Inference
2397
+
2398
+ The `@doeixd/machine/minimal` submodule is optimized for **Type-State Programming** with zero boilerplate. It features "magic" type inference, meaning you almost never have to write explicit generic types.
2399
+
2400
+ ```typescript
2401
+ import { machine } from "@doeixd/machine/minimal";
2402
+
2403
+ // Transitions (inc, add) are automatically inferred!
2404
+ const counter = machine({ count: 0 }, (ctx, next) => ({
2405
+ inc: () => next({ count: ctx.count + 1 }),
2406
+ add: (n: number) => next({ count: ctx.count + n })
2407
+ }));
2408
+
2409
+ const result = counter.inc().add(10);
2410
+ console.log(result.count); // 11
2411
+ ```
2412
+
2413
+ ## 🤝 Machine Delegation
2414
+
2415
+ Use `@doeixd/machine/delegate` to compose complex machines by delegating transitions to child machines.
2416
+
2417
+ ```typescript
2418
+ import { machine } from "@doeixd/machine/minimal";
2419
+ import { delegate } from "@doeixd/machine/delegate";
2420
+
2421
+ const authMachine = machine({ status: 'out' }, (ctx, next) => ({
2422
+ login: () => next({ status: 'in' })
2423
+ }));
2424
+
2425
+ const rootMachine = machine({ auth: authMachine }, (ctx, next) => ({
2426
+ // Automatically delegate 'login' to the 'auth' child!
2427
+ ...delegate(ctx, 'auth', next)
2428
+ }));
2429
+
2430
+
2383
2431
  ## License
2384
2432
 
2385
2433
  MIT
@@ -142,6 +142,17 @@ function snapshotOwnTransitions(source) {
142
142
  return Object.fromEntries(entries);
143
143
  }
144
144
 
145
+ // src/base.ts
146
+ var MachineBase = class {
147
+ /**
148
+ * Initializes a new machine instance with its starting context.
149
+ * @param context - The initial state of the machine.
150
+ */
151
+ constructor(context) {
152
+ this.context = context;
153
+ }
154
+ };
155
+
145
156
  // src/generators.ts
146
157
  function run(flow, initial) {
147
158
  const generator = flow(initial);
@@ -1836,15 +1847,6 @@ function runMachine(initial, onChange) {
1836
1847
  }
1837
1848
  };
1838
1849
  }
1839
- var MachineBase = class {
1840
- /**
1841
- * Initializes a new machine instance with its starting context.
1842
- * @param context - The initial state of the machine.
1843
- */
1844
- constructor(context) {
1845
- this.context = context;
1846
- }
1847
- };
1848
1850
  function next(m, update) {
1849
1851
  return setContext(m, (ctx) => update(ctx));
1850
1852
  }