@doeixd/machine 0.0.23 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +101 -65
  2. package/dist/cjs/development/core.js +19 -45
  3. package/dist/cjs/development/core.js.map +3 -3
  4. package/dist/cjs/development/index.js +51 -46
  5. package/dist/cjs/development/index.js.map +4 -4
  6. package/dist/cjs/development/react.js +19 -46
  7. package/dist/cjs/development/react.js.map +3 -3
  8. package/dist/cjs/production/core.js +1 -1
  9. package/dist/cjs/production/index.js +3 -3
  10. package/dist/cjs/production/react.js +1 -1
  11. package/dist/esm/development/core.js +19 -45
  12. package/dist/esm/development/core.js.map +3 -3
  13. package/dist/esm/development/index.js +51 -46
  14. package/dist/esm/development/index.js.map +4 -4
  15. package/dist/esm/development/react.js +19 -46
  16. package/dist/esm/development/react.js.map +3 -3
  17. package/dist/esm/production/core.js +1 -1
  18. package/dist/esm/production/index.js +3 -3
  19. package/dist/esm/production/react.js +1 -1
  20. package/dist/types/actor.d.ts +4 -4
  21. package/dist/types/actor.d.ts.map +1 -1
  22. package/dist/types/context-bound.d.ts +94 -0
  23. package/dist/types/context-bound.d.ts.map +1 -0
  24. package/dist/types/entry-react.d.ts +1 -1
  25. package/dist/types/entry-react.d.ts.map +1 -1
  26. package/dist/types/functional-combinators.d.ts +5 -5
  27. package/dist/types/generators.d.ts +2 -2
  28. package/dist/types/index.d.ts +14 -29
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/primitives.d.ts +25 -5
  31. package/dist/types/primitives.d.ts.map +1 -1
  32. package/dist/types/react.d.ts.map +1 -1
  33. package/dist/types/utils.d.ts +22 -22
  34. package/dist/types/utils.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/actor.ts +1 -1
  37. package/src/context-bound.ts +147 -0
  38. package/src/entry-react.ts +9 -2
  39. package/src/functional-combinators.ts +5 -5
  40. package/src/generators.ts +2 -2
  41. package/src/higher-order.ts +2 -2
  42. package/src/index.ts +31 -68
  43. package/src/middleware/time-travel.ts +2 -2
  44. package/src/middleware.ts +2 -2
  45. package/src/multi.ts +4 -4
  46. package/src/primitives.ts +34 -14
  47. package/src/prototype_functional.ts +2 -2
  48. package/src/react.ts +1 -2
  49. package/src/test.ts +7 -7
  50. package/src/utils.ts +31 -31
package/src/utils.ts CHANGED
@@ -231,7 +231,7 @@ export function createTransition<
231
231
  C extends object,
232
232
  TArgs extends any[]
233
233
  >(
234
- getTransitions: () => Record<string, (this: C, ...args: any[]) => any>,
234
+ getTransitions: () => Record<string, (this: Machine<C, any>, ...args: any[]) => any>,
235
235
  transformer: (ctx: C, ...args: TArgs) => C
236
236
  ): (this: { context: C }, ...args: TArgs) => Machine<C> {
237
237
  return function (this: { context: C }, ...args: TArgs): Machine<C> {
@@ -245,50 +245,50 @@ export function createTransition<
245
245
  // =============================================================================
246
246
 
247
247
  /**
248
- * Calls a transition function with an explicit `this` context.
249
- * Useful for invoking transition methods with proper context binding.
248
+ * Calls a transition function with an explicit `this` binding.
249
+ * Useful for invoking transition methods with proper machine binding.
250
250
  *
251
- * @template C - The context type that the function expects as `this`.
251
+ * @template M - The machine type that the function expects as `this`.
252
252
  * @template F - The function type with a `this` parameter.
253
253
  * @template A - The argument types for the function.
254
254
  * @param fn - The transition function to call.
255
- * @param context - The context object to bind as `this`.
255
+ * @param machine - The machine object to bind as `this`.
256
256
  * @param args - Arguments to pass to the function.
257
- * @returns The result of calling the function with the given context and arguments.
257
+ * @returns The result of calling the function with the given machine and arguments.
258
258
  *
259
259
  * @example
260
- * type MyContext = { count: number };
261
- * const increment = function(this: MyContext) { return this.count + 1; };
262
- * const result = call(increment, { count: 5 }); // Returns 6
260
+ * type MyMachine = Machine<{ count: number }>;
261
+ * const increment = function(this: MyMachine) { return createMachine({ count: this.context.count + 1 }, this); };
262
+ * const result = call(increment, machine); // Returns new machine
263
263
  *
264
264
  * // Particularly useful with machine transitions:
265
265
  * import { call } from '@doeixd/machine/utils';
266
- * const nextMachine = yield* step(call(m.increment, m.context));
266
+ * const nextMachine = yield* step(call(m.increment, m));
267
267
  */
268
- export function call<C, F extends (this: C, ...args: any[]) => any>(
268
+ export function call<M extends Machine<any>, F extends (this: M, ...args: any[]) => any>(
269
269
  fn: F,
270
- context: C,
270
+ machine: M,
271
271
  ...args: Parameters<F> extends [any, ...infer Rest] ? Rest : never
272
272
  ): ReturnType<F> {
273
- return fn.apply(context, args);
273
+ return fn.apply(machine, args);
274
274
  }
275
275
 
276
276
  /**
277
- * Binds all transition methods of a machine to its context automatically.
278
- * Returns a Proxy that intercepts method calls and binds them to `machine.context`.
279
- * This eliminates the need to use `.call(m.context, ...)` for every transition.
277
+ * Binds all transition methods of a machine to the machine itself automatically.
278
+ * Returns a Proxy that intercepts method calls and binds them to the full machine.
279
+ * This eliminates the need to use `.call(m, ...)` for every transition.
280
280
  *
281
281
  * Automatically recursively wraps returned machines, enabling seamless chaining
282
282
  * in generator-based flows.
283
283
  *
284
284
  * @template M - The machine type with a `context` property and transition methods.
285
285
  * @param machine - The machine instance to wrap.
286
- * @returns A Proxy of the machine where all callable properties (transitions) are automatically bound to the machine's context.
286
+ * @returns A Proxy of the machine where all callable properties (transitions) are automatically bound to the machine.
287
287
  *
288
288
  * @example
289
- * type CounterContext = { count: number };
289
+ * type CounterMachine = Machine<{ count: number }>;
290
290
  * const counter = bindTransitions(createMachine({ count: 0 }, {
291
- * increment(this: CounterContext) { return createCounter(this.count + 1); }
291
+ * increment(this: CounterMachine) { return createMachine({ count: this.context.count + 1 }, this); }
292
292
  * }));
293
293
  *
294
294
  * // Now you can call transitions directly without .call():
@@ -304,18 +304,18 @@ export function call<C, F extends (this: C, ...args: any[]) => any>(
304
304
  * @remarks
305
305
  * The Proxy preserves all original properties and methods. Non-callable properties
306
306
  * are accessed directly from the machine. Callable properties are wrapped to bind
307
- * them to `machine.context` before invocation. Returned machines are automatically
307
+ * them to the machine before invocation. Returned machines are automatically
308
308
  * re-wrapped to maintain binding across transition chains.
309
309
  */
310
310
  export function bindTransitions<M extends { context: any }>(machine: M): M {
311
311
  return new Proxy(machine, {
312
312
  get(target, prop) {
313
313
  const value = target[prop as keyof M];
314
-
315
- // If it's a callable property (transition method), bind it to context
314
+
315
+ // If it's a callable property (transition method), bind it to machine
316
316
  if (typeof value === 'function') {
317
317
  return function(...args: any[]) {
318
- const result = value.apply(target.context, args);
318
+ const result = value.apply(target, args);
319
319
  // Recursively wrap returned machines to maintain binding
320
320
  if (result && typeof result === 'object' && 'context' in result) {
321
321
  return bindTransitions(result);
@@ -323,7 +323,7 @@ export function bindTransitions<M extends { context: any }>(machine: M): M {
323
323
  return result;
324
324
  };
325
325
  }
326
-
326
+
327
327
  // Otherwise, return the value as-is
328
328
  return value;
329
329
  },
@@ -331,21 +331,21 @@ export function bindTransitions<M extends { context: any }>(machine: M): M {
331
331
  }
332
332
 
333
333
  /**
334
- * A strongly-typed wrapper class for binding transitions to machine context.
334
+ * A strongly-typed wrapper class for binding transitions to the machine.
335
335
  * Unlike the Proxy-based `bindTransitions`, this class preserves full type safety
336
336
  * and provides better IDE support through explicit property forwarding.
337
337
  *
338
338
  * @template M - The machine type with a `context` property and transition methods.
339
339
  *
340
340
  * @example
341
- * type CounterContext = { count: number };
341
+ * type CounterMachine = Machine<{ count: number }>;
342
342
  * const counter = createMachine({ count: 0 }, {
343
- * increment(this: CounterContext) { return createCounter(this.count + 1); }
343
+ * increment(this: CounterMachine) { return createMachine({ count: this.context.count + 1 }, this); }
344
344
  * });
345
345
  *
346
346
  * const bound = new BoundMachine(counter);
347
347
  *
348
- * // All transitions are automatically bound to context
348
+ * // All transitions are automatically bound to machine
349
349
  * const result = run(function* (m) {
350
350
  * m = yield* step(m.increment());
351
351
  * m = yield* step(m.add(5));
@@ -383,10 +383,10 @@ export class BoundMachine<M extends { context: any }> {
383
383
 
384
384
  const value = this.wrappedMachine[prop as keyof M];
385
385
 
386
- // Bind transition methods to context
386
+ // Bind transition methods to machine
387
387
  if (typeof value === 'function') {
388
388
  return (...args: any[]) => {
389
- const result = value.apply(this.wrappedMachine.context, args);
389
+ const result = value.apply(this.wrappedMachine, args);
390
390
  // Recursively wrap returned machines
391
391
  if (result && typeof result === 'object' && 'context' in result) {
392
392
  return new BoundMachine(result);
@@ -619,4 +619,4 @@ export function sequence3<
619
619
  isFinal: (machine: M1 | M2 | M3) => boolean
620
620
  ): M1 | M2 | M3 {
621
621
  return sequence([machine1, machine2, machine3], isFinal);
622
- }
622
+ }