@doeixd/machine 0.0.17 → 0.0.18

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/src/generators.ts CHANGED
@@ -135,12 +135,12 @@ export function run<C extends any = any, M extends { context: C } & Record<strin
135
135
 
136
136
  // If the generator has returned (done), we have our final value
137
137
  if (done) {
138
- return value;
138
+ return value as T;
139
139
  }
140
140
 
141
141
  // Otherwise, the yielded value becomes our new current state
142
142
  // This state will be sent back into the generator on the next iteration
143
- current = value;
143
+ current = value as M;
144
144
  }
145
145
  }
146
146
 
@@ -346,10 +346,10 @@ export function runWithDebug<C extends any = any, M extends { context: C } & Rec
346
346
 
347
347
  if (done) {
348
348
  console.log('Final:', value);
349
- return value;
349
+ return value as T;
350
350
  }
351
351
 
352
- current = value;
352
+ current = value as M;
353
353
  stepCount++;
354
354
  logger(stepCount, current);
355
355
  }
@@ -391,10 +391,10 @@ export async function runAsync<C extends any = any, M extends { context: C } & R
391
391
  const { value, done } = await generator.next(current);
392
392
 
393
393
  if (done) {
394
- return value;
394
+ return value as T;
395
395
  }
396
396
 
397
- current = value;
397
+ current = value as M;
398
398
  }
399
399
  }
400
400
 
package/src/index.ts CHANGED
@@ -60,6 +60,21 @@ export type AsyncTransitionArgs<M extends AsyncMachine<any, any>, K extends keyo
60
60
  ? A extends [...infer Rest, TransitionOptions] ? Rest : A
61
61
  : never;
62
62
 
63
+ /**
64
+ * A helper type to define a distinct state in a state machine (a "typestate").
65
+ * Allows defining the context and transitions in a single generic type.
66
+ * @template C - The context specific to this state.
67
+ * @template T - The transitions available in this state.
68
+ */
69
+ export type TypeState<C extends object, T extends object = {}> = Machine<C, T>;
70
+
71
+ /**
72
+ * A helper type to define a distinct async state in a state machine.
73
+ * @template C - The context specific to this state.
74
+ * @template T - The transitions available in this state.
75
+ */
76
+ export type AsyncTypeState<C extends object, T extends object = {}> = AsyncMachine<C, T>;
77
+
63
78
 
64
79
  /**
65
80
  * Options passed to async transition functions, including cancellation support.
@@ -332,7 +347,7 @@ export type BindTransitions<T> = {
332
347
  */
333
348
  export function createMachine<C extends object, T extends Record<string, (this: C, ...args: any[]) => any>>(
334
349
  context: C,
335
- factory: (transition: (newContext: C) => Machine<C, T>) => T
350
+ factory: (transition: (newContext: C) => Machine<C, any>) => T
336
351
  ): Machine<C, BindTransitions<T>>;
337
352
 
338
353
  /**