@doeixd/machine 0.0.8 → 0.0.10

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.
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ ADVANCED_CONFIG_EXAMPLES: () => ADVANCED_CONFIG_EXAMPLES,
23
24
  BoundMachine: () => BoundMachine,
24
25
  META_KEY: () => META_KEY,
25
26
  MachineBase: () => MachineBase,
@@ -61,6 +62,7 @@ __export(src_exports, {
61
62
  generateChart: () => generateChart,
62
63
  generateStatechart: () => generateStatechart,
63
64
  guard: () => guard,
65
+ guardAsync: () => guardAsync,
64
66
  guarded: () => guarded,
65
67
  hasState: () => hasState,
66
68
  inDevelopment: () => inDevelopment,
@@ -90,6 +92,7 @@ __export(src_exports, {
90
92
  when: () => when,
91
93
  whenContext: () => whenContext,
92
94
  whenGuard: () => whenGuard,
95
+ whenGuardAsync: () => whenGuardAsync,
93
96
  withAnalytics: () => withAnalytics,
94
97
  withDebugging: () => withDebugging,
95
98
  withErrorReporting: () => withErrorReporting,
@@ -228,6 +231,47 @@ function action(action2, transition) {
228
231
  return transition;
229
232
  }
230
233
  function guard(condition, transition, options = {}) {
234
+ const { onFail = "throw", errorMessage, description } = options;
235
+ const fullOptions = { ...options, onFail, errorMessage, description };
236
+ const guardedTransition = function(...args) {
237
+ const isMachine = typeof this === "object" && "context" in this;
238
+ const ctx = isMachine ? this.context : this;
239
+ const conditionResult = condition(ctx, ...args);
240
+ if (conditionResult) {
241
+ const contextForTransition = isMachine ? this.context : this;
242
+ return transition.apply(contextForTransition, args);
243
+ } else {
244
+ if (onFail === "throw") {
245
+ const message = errorMessage || "Guard condition failed";
246
+ throw new Error(message);
247
+ } else if (onFail === "ignore") {
248
+ if (isMachine) {
249
+ return this;
250
+ } else {
251
+ throw new Error('Cannot use "ignore" mode with context-only binding. Use full machine binding or provide fallback.');
252
+ }
253
+ } else if (typeof onFail === "function") {
254
+ if (isMachine) {
255
+ return onFail.apply(this, args);
256
+ } else {
257
+ throw new Error("Cannot use function fallback with context-only binding. Use full machine binding.");
258
+ }
259
+ } else {
260
+ return onFail;
261
+ }
262
+ }
263
+ };
264
+ Object.defineProperty(guardedTransition, "__guard", { value: true, enumerable: false });
265
+ Object.defineProperty(guardedTransition, "condition", { value: condition, enumerable: false });
266
+ Object.defineProperty(guardedTransition, "transition", { value: transition, enumerable: false });
267
+ Object.defineProperty(guardedTransition, "options", { value: fullOptions, enumerable: false });
268
+ attachRuntimeMeta(guardedTransition, {
269
+ description: description || "Synchronous guarded transition",
270
+ guards: [{ name: "runtime_guard", description: description || "Synchronous condition check" }]
271
+ });
272
+ return guardedTransition;
273
+ }
274
+ function guardAsync(condition, transition, options = {}) {
231
275
  const { onFail = "throw", errorMessage, description } = options;
232
276
  const fullOptions = { ...options, onFail, errorMessage, description };
233
277
  const guardedTransition = async function(...args) {
@@ -283,6 +327,21 @@ function whenGuard(condition) {
283
327
  }
284
328
  };
285
329
  }
330
+ function whenGuardAsync(condition) {
331
+ return {
332
+ /**
333
+ * Define the transition to execute when the condition passes.
334
+ * Returns a guarded transition that can optionally have an else clause.
335
+ */
336
+ do(transition) {
337
+ const guarded2 = guardAsync(condition, transition);
338
+ guarded2.else = function(fallback) {
339
+ return guardAsync(condition, transition, { onFail: fallback });
340
+ };
341
+ return guarded2;
342
+ }
343
+ };
344
+ }
286
345
  function metadata(_meta, value) {
287
346
  return value;
288
347
  }
@@ -416,6 +475,36 @@ function extractFromCallExpression(call2, verbose = false) {
416
475
  }
417
476
  }
418
477
  break;
478
+ case "guard":
479
+ if (args[2]) {
480
+ const options = parseObjectLiteral(args[2]);
481
+ if (options.description) {
482
+ metadata2.description = options.description;
483
+ }
484
+ }
485
+ metadata2.guards = [{ name: "runtime_guard", description: metadata2.description || "Synchronous condition check" }];
486
+ if (args[1] && import_ts_morph.Node.isCallExpression(args[1])) {
487
+ const nested = extractFromCallExpression(args[1], verbose);
488
+ if (nested) {
489
+ Object.assign(metadata2, nested);
490
+ }
491
+ }
492
+ break;
493
+ case "guardAsync":
494
+ if (args[2]) {
495
+ const options = parseObjectLiteral(args[2]);
496
+ if (options.description) {
497
+ metadata2.description = options.description;
498
+ }
499
+ }
500
+ metadata2.guards = [{ name: "runtime_guard_async", description: metadata2.description || "Asynchronous condition check" }];
501
+ if (args[1] && import_ts_morph.Node.isCallExpression(args[1])) {
502
+ const nested = extractFromCallExpression(args[1], verbose);
503
+ if (nested) {
504
+ Object.assign(metadata2, nested);
505
+ }
506
+ }
507
+ break;
419
508
  default:
420
509
  return null;
421
510
  }
@@ -657,6 +746,37 @@ function generateChart() {
657
746
  process.exit(1);
658
747
  }
659
748
  }
749
+ var ADVANCED_CONFIG_EXAMPLES = {
750
+ hierarchical: {
751
+ input: "examples/dashboardMachine.ts",
752
+ id: "dashboard",
753
+ classes: ["DashboardMachine", "LoggedOutMachine"],
754
+ initialState: "DashboardMachine",
755
+ children: {
756
+ contextProperty: "child",
757
+ initialState: "ViewingChildMachine",
758
+ classes: ["ViewingChildMachine", "EditingChildMachine"]
759
+ }
760
+ },
761
+ parallel: {
762
+ input: "examples/editorMachine.ts",
763
+ id: "editor",
764
+ parallel: {
765
+ regions: [
766
+ {
767
+ name: "fontWeight",
768
+ initialState: "NormalWeight",
769
+ classes: ["NormalWeight", "BoldWeight"]
770
+ },
771
+ {
772
+ name: "textDecoration",
773
+ initialState: "NoDecoration",
774
+ classes: ["NoDecoration", "UnderlineState"]
775
+ }
776
+ ]
777
+ }
778
+ }
779
+ };
660
780
  if (require.main === module) {
661
781
  generateChart();
662
782
  }