@cadenza.io/core 1.2.0 → 1.4.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.
package/dist/index.mjs CHANGED
@@ -3,8 +3,8 @@ var SignalBroker = class _SignalBroker {
3
3
  // execId -> emitted signals
4
4
  constructor() {
5
5
  this.signalObservers = /* @__PURE__ */ new Map();
6
- // For loop prevention: Per-execId recent emits (cleared post-run or TTL)
7
6
  this.emitStacks = /* @__PURE__ */ new Map();
7
+ this.addSignal("meta.signal.added");
8
8
  }
9
9
  /**
10
10
  * Singleton instance for signal management.
@@ -21,10 +21,32 @@ var SignalBroker = class _SignalBroker {
21
21
  * @param runner Standard runner for user signals.
22
22
  * @param metaRunner Meta runner for 'meta.' signals (suppresses further meta-emits).
23
23
  */
24
- init(runner, metaRunner) {
24
+ bootstrap(runner, metaRunner) {
25
25
  this.runner = runner;
26
26
  this.metaRunner = metaRunner;
27
27
  }
28
+ init() {
29
+ Cadenza.createMetaTask(
30
+ "Execute and clear queued signals",
31
+ () => {
32
+ for (const [id, signals] of this.emitStacks.entries()) {
33
+ signals.forEach((context, signal) => {
34
+ this.execute(signal, context);
35
+ signals.delete(signal);
36
+ });
37
+ this.emitStacks.delete(id);
38
+ }
39
+ return true;
40
+ },
41
+ "Executes queued signals and clears the stack"
42
+ ).doOn("meta.clear_signal_queue_requested");
43
+ this.getSignalsTask = Cadenza.createMetaTask("Get signals", (ctx) => {
44
+ return {
45
+ __signals: Array.from(this.signalObservers.keys()),
46
+ ...ctx
47
+ };
48
+ });
49
+ }
28
50
  /**
29
51
  * Observes a signal with a routine/task.
30
52
  * @param signal The signal (e.g., 'domain.action', 'domain.*' for wildcards).
@@ -60,30 +82,34 @@ var SignalBroker = class _SignalBroker {
60
82
  */
61
83
  emit(signal, context = {}) {
62
84
  const execId = context.__graphExecId || "global";
63
- if (!this.emitStacks.has(execId)) this.emitStacks.set(execId, /* @__PURE__ */ new Set());
85
+ if (!this.emitStacks.has(execId)) this.emitStacks.set(execId, /* @__PURE__ */ new Map());
64
86
  const stack = this.emitStacks.get(execId);
65
- if (stack.has(signal)) {
66
- throw new Error(`Signal loop detected for ${signal} in exec ${execId}`);
67
- }
68
- stack.add(signal);
69
- this.executeListener(signal, context);
87
+ stack.set(signal, context);
88
+ let executed = false;
70
89
  try {
71
- const parts = signal.slice(0, Math.max(signal.lastIndexOf(":"), signal.lastIndexOf("."))).split(".");
72
- for (let i = parts.length; i > 0; i--) {
73
- const parent = parts.slice(0, i).join(".");
74
- this.executeListener(parent, context);
75
- this.executeListener(parent + ".*", context);
76
- }
90
+ executed = this.execute(signal, context);
77
91
  } finally {
78
- stack.delete(signal);
92
+ if (executed) stack.delete(signal);
93
+ }
94
+ }
95
+ execute(signal, context) {
96
+ let executed;
97
+ executed = this.executeListener(signal, context);
98
+ const parts = signal.slice(0, Math.max(signal.lastIndexOf(":"), signal.lastIndexOf("."))).split(".");
99
+ for (let i = parts.length; i > 0; i--) {
100
+ const parent = parts.slice(0, i).join(".");
101
+ executed = executed || this.executeListener(parent + ".*", context);
79
102
  }
103
+ return executed;
80
104
  }
81
105
  executeListener(signal, context) {
82
106
  const obs = this.signalObservers.get(signal);
83
107
  const runner = this.getRunner(signal);
84
108
  if (obs && runner) {
85
109
  obs.fn(runner, Array.from(obs.tasks), context);
110
+ return true;
86
111
  }
112
+ return false;
87
113
  }
88
114
  addSignal(signal) {
89
115
  if (!this.signalObservers.has(signal)) {
@@ -91,6 +117,7 @@ var SignalBroker = class _SignalBroker {
91
117
  fn: (runner, tasks, context) => runner.run(tasks, context),
92
118
  tasks: /* @__PURE__ */ new Set()
93
119
  });
120
+ this.emit("meta.signal.added", { __signal: signal });
94
121
  }
95
122
  }
96
123
  /**
@@ -1029,13 +1056,14 @@ var SignalParticipant = class extends SignalEmitter {
1029
1056
 
1030
1057
  // src/graph/definition/GraphRoutine.ts
1031
1058
  var GraphRoutine = class extends SignalParticipant {
1032
- constructor(name, tasks, description) {
1059
+ constructor(name, tasks, description, isMeta = false) {
1033
1060
  super();
1034
1061
  this.isMeta = false;
1035
1062
  this.tasks = /* @__PURE__ */ new Set();
1036
1063
  this.id = uuid4();
1037
1064
  this.name = name;
1038
1065
  this.description = description;
1066
+ this.isMeta = isMeta;
1039
1067
  tasks.forEach((t) => this.tasks.add(t));
1040
1068
  this.emit("meta.routine.created", { __routine: this });
1041
1069
  }
@@ -1115,14 +1143,16 @@ var Task = class extends SignalParticipant {
1115
1143
  * @param concurrency Limit.
1116
1144
  * @param timeout ms.
1117
1145
  * @param register Register via signal (default true).
1146
+ * @param isUnique
1147
+ * @param isMeta
1118
1148
  * @edge Emits 'meta.task.created' with { __task: this } for seed.
1119
1149
  */
1120
- constructor(name, task, description = "", concurrency = 0, timeout = 0, register = true) {
1150
+ constructor(name, task, description = "", concurrency = 0, timeout = 0, register = true, isUnique = false, isMeta = false) {
1121
1151
  super();
1152
+ this.isMeta = false;
1122
1153
  this.isUnique = false;
1123
1154
  this.throttled = false;
1124
1155
  this.isSignal = false;
1125
- this.isMeta = false;
1126
1156
  this.isDeputy = false;
1127
1157
  this.isEphemeral = false;
1128
1158
  this.layerIndex = 0;
@@ -1137,6 +1167,8 @@ var Task = class extends SignalParticipant {
1137
1167
  this.description = description;
1138
1168
  this.concurrency = concurrency;
1139
1169
  this.timeout = timeout;
1170
+ this.isUnique = isUnique;
1171
+ this.isMeta = isMeta;
1140
1172
  if (register) {
1141
1173
  this.emit("meta.task.created", { __task: this });
1142
1174
  }
@@ -1304,6 +1336,7 @@ var Task = class extends SignalParticipant {
1304
1336
  __concurrency: this.concurrency,
1305
1337
  __timeout: this.timeout,
1306
1338
  __functionString: this.taskFunction.toString(),
1339
+ __getTagCallback: this.getTag.toString(),
1307
1340
  __nextTasks: Array.from(this.nextTasks).map((t) => t.id),
1308
1341
  __onFailTasks: Array.from(this.onFailTasks).map((t) => t.id),
1309
1342
  __previousTasks: Array.from(this.predecessorTasks).map((t) => t.id)
@@ -1320,20 +1353,12 @@ var Task = class extends SignalParticipant {
1320
1353
  }
1321
1354
  };
1322
1355
 
1323
- // src/graph/definition/meta/MetaTask.ts
1324
- var MetaTask = class extends Task {
1325
- constructor() {
1326
- super(...arguments);
1327
- this.isMeta = true;
1328
- }
1329
- };
1330
-
1331
1356
  // src/registry/GraphRegistry.ts
1332
1357
  var GraphRegistry = class _GraphRegistry {
1333
1358
  constructor() {
1334
1359
  this.tasks = /* @__PURE__ */ new Map();
1335
1360
  this.routines = /* @__PURE__ */ new Map();
1336
- this.registerTask = new MetaTask(
1361
+ this.registerTask = new Task(
1337
1362
  "Registry Seed",
1338
1363
  (context) => {
1339
1364
  const { __task } = context;
@@ -1343,7 +1368,12 @@ var GraphRegistry = class _GraphRegistry {
1343
1368
  }
1344
1369
  return true;
1345
1370
  },
1346
- "Registers tasks. Seed for meta.taskCreated"
1371
+ "Registers tasks. Seed for meta.taskCreated",
1372
+ 0,
1373
+ 0,
1374
+ true,
1375
+ false,
1376
+ true
1347
1377
  ).doOn("meta.task.created");
1348
1378
  this.tasks.set(this.registerTask.id, this.registerTask);
1349
1379
  this.updateTaskId = Cadenza.createMetaTask(
@@ -1640,49 +1670,37 @@ var GraphRunner = class extends SignalEmitter {
1640
1670
  }
1641
1671
  };
1642
1672
 
1643
- // src/graph/definition/UniqueTask.ts
1644
- var UniqueTask = class extends Task {
1645
- constructor() {
1646
- super(...arguments);
1647
- this.isUnique = true;
1648
- }
1649
- };
1650
-
1651
- // src/graph/definition/meta/UniqueMetaTask.ts
1652
- var UniqueMetaTask = class extends UniqueTask {
1653
- constructor() {
1654
- super(...arguments);
1655
- this.isMeta = true;
1656
- }
1657
- };
1658
-
1659
1673
  // src/graph/definition/ThrottledTask.ts
1660
1674
  var ThrottledTask = class extends Task {
1661
- constructor(name, task, description = "", getTagCallback = (context, task2) => "default", concurrency = 1, timeout = 0, register = true) {
1662
- super(name, task, description, concurrency, timeout, register);
1675
+ constructor(name, task, description = "", getTagCallback = (context, task2) => "default", concurrency = 1, timeout = 0, register = true, isUnique = false, isMeta = false) {
1676
+ super(
1677
+ name,
1678
+ task,
1679
+ description,
1680
+ concurrency,
1681
+ timeout,
1682
+ register,
1683
+ isUnique,
1684
+ isMeta
1685
+ );
1663
1686
  this.throttled = true;
1664
1687
  this.getTag = (context) => getTagCallback(context, this);
1665
1688
  }
1666
- export() {
1667
- return {
1668
- ...super.export(),
1669
- __getTagCallback: this.getTag.toString()
1670
- };
1671
- }
1672
- };
1673
-
1674
- // src/graph/definition/meta/ThrottledMetaTask.ts
1675
- var ThrottledMetaTask = class extends ThrottledTask {
1676
- constructor() {
1677
- super(...arguments);
1678
- this.isMeta = true;
1679
- }
1680
1689
  };
1681
1690
 
1682
1691
  // src/graph/definition/DebounceTask.ts
1683
1692
  var DebounceTask = class extends Task {
1684
- constructor(name, task, description = "", debounceTime = 1e3, leading = false, trailing = true, concurrency = 0, timeout = 0, register = true) {
1685
- super(name, task, description, concurrency, timeout, register);
1693
+ constructor(name, task, description = "", debounceTime = 1e3, leading = false, trailing = true, concurrency = 0, timeout = 0, register = true, isUnique = false, isMeta = false) {
1694
+ super(
1695
+ name,
1696
+ task,
1697
+ description,
1698
+ concurrency,
1699
+ timeout,
1700
+ register,
1701
+ isUnique,
1702
+ isMeta
1703
+ );
1686
1704
  this.timer = null;
1687
1705
  this.lastResolve = null;
1688
1706
  this.lastReject = null;
@@ -1751,18 +1769,19 @@ var DebounceTask = class extends Task {
1751
1769
  }
1752
1770
  };
1753
1771
 
1754
- // src/graph/definition/meta/DebouncedMetaTask.ts
1755
- var DebouncedMetaTask = class extends DebounceTask {
1756
- constructor() {
1757
- super(...arguments);
1758
- this.isMeta = true;
1759
- }
1760
- };
1761
-
1762
1772
  // src/graph/definition/EphemeralTask.ts
1763
1773
  var EphemeralTask = class extends Task {
1764
- constructor(name, task, description = "", once = true, condition = () => true, concurrency = 0, timeout = 0, register = false) {
1765
- super(name, task, description, concurrency, timeout, register);
1774
+ constructor(name, task, description = "", once = true, condition = () => true, concurrency = 0, timeout = 0, register = false, isUnique = false, isMeta = false) {
1775
+ super(
1776
+ name,
1777
+ task,
1778
+ description,
1779
+ concurrency,
1780
+ timeout,
1781
+ register,
1782
+ isUnique,
1783
+ isMeta
1784
+ );
1766
1785
  this.isEphemeral = true;
1767
1786
  this.once = once;
1768
1787
  this.condition = condition;
@@ -1777,22 +1796,6 @@ var EphemeralTask = class extends Task {
1777
1796
  }
1778
1797
  };
1779
1798
 
1780
- // src/graph/definition/meta/EphemeralMetaTask.ts
1781
- var EphemeralMetaTask = class extends EphemeralTask {
1782
- constructor() {
1783
- super(...arguments);
1784
- this.isMeta = true;
1785
- }
1786
- };
1787
-
1788
- // src/graph/definition/meta/MetaRoutine.ts
1789
- var MetaRoutine = class extends GraphRoutine {
1790
- constructor() {
1791
- super(...arguments);
1792
- this.isMeta = true;
1793
- }
1794
- };
1795
-
1796
1799
  // src/interfaces/ExecutionChain.ts
1797
1800
  var ExecutionChain = class {
1798
1801
  setNext(next) {
@@ -2307,8 +2310,9 @@ var Cadenza = class {
2307
2310
  this.broker = SignalBroker.instance;
2308
2311
  this.runner = new GraphRunner();
2309
2312
  this.metaRunner = new GraphRunner(true);
2310
- this.broker.init(this.runner, this.metaRunner);
2313
+ this.broker.bootstrap(this.runner, this.metaRunner);
2311
2314
  this.registry = GraphRegistry.instance;
2315
+ this.broker.init();
2312
2316
  this.runner.init();
2313
2317
  this.metaRunner.init();
2314
2318
  }
@@ -2340,7 +2344,9 @@ var Cadenza = class {
2340
2344
  static createTask(name, func, description, options = {
2341
2345
  concurrency: 0,
2342
2346
  timeout: 0,
2343
- register: true
2347
+ register: true,
2348
+ isUnique: false,
2349
+ isMeta: false
2344
2350
  }) {
2345
2351
  this.bootstrap();
2346
2352
  this.validateName(name);
@@ -2350,7 +2356,9 @@ var Cadenza = class {
2350
2356
  description,
2351
2357
  options.concurrency,
2352
2358
  options.timeout,
2353
- options.register
2359
+ options.register,
2360
+ options.isUnique,
2361
+ options.isMeta
2354
2362
  );
2355
2363
  }
2356
2364
  /**
@@ -2366,18 +2374,12 @@ var Cadenza = class {
2366
2374
  static createMetaTask(name, func, description, options = {
2367
2375
  concurrency: 0,
2368
2376
  timeout: 0,
2369
- register: true
2377
+ register: true,
2378
+ isUnique: false,
2379
+ isMeta: true
2370
2380
  }) {
2371
- this.bootstrap();
2372
- this.validateName(name);
2373
- return new MetaTask(
2374
- name,
2375
- func,
2376
- description,
2377
- options.concurrency,
2378
- options.timeout,
2379
- options.register
2380
- );
2381
+ options.isMeta = true;
2382
+ return this.createTask(name, func, description, options);
2381
2383
  }
2382
2384
  /**
2383
2385
  * Creates a UniqueTask (executes once per execution ID, merging parents) and registers it.
@@ -2392,18 +2394,12 @@ var Cadenza = class {
2392
2394
  static createUniqueTask(name, func, description, options = {
2393
2395
  concurrency: 0,
2394
2396
  timeout: 0,
2395
- register: true
2397
+ register: true,
2398
+ isUnique: true,
2399
+ isMeta: false
2396
2400
  }) {
2397
- this.bootstrap();
2398
- this.validateName(name);
2399
- return new UniqueTask(
2400
- name,
2401
- func,
2402
- description,
2403
- options.concurrency,
2404
- options.timeout,
2405
- options.register
2406
- );
2401
+ options.isUnique = true;
2402
+ return this.createTask(name, func, description, options);
2407
2403
  }
2408
2404
  /**
2409
2405
  * Creates a UniqueMetaTask for meta-layer joins.
@@ -2416,18 +2412,12 @@ var Cadenza = class {
2416
2412
  static createUniqueMetaTask(name, func, description, options = {
2417
2413
  concurrency: 0,
2418
2414
  timeout: 0,
2419
- register: true
2415
+ register: true,
2416
+ isUnique: true,
2417
+ isMeta: true
2420
2418
  }) {
2421
- this.bootstrap();
2422
- this.validateName(name);
2423
- return new UniqueMetaTask(
2424
- name,
2425
- func,
2426
- description,
2427
- options.concurrency,
2428
- options.timeout,
2429
- options.register
2430
- );
2419
+ options.isMeta = true;
2420
+ return this.createUniqueTask(name, func, description, options);
2431
2421
  }
2432
2422
  /**
2433
2423
  * Creates a ThrottledTask (rate-limited by concurrency or custom groups) and registers it.
@@ -2442,7 +2432,9 @@ var Cadenza = class {
2442
2432
  static createThrottledTask(name, func, throttledIdGetter, description, options = {
2443
2433
  concurrency: 1,
2444
2434
  timeout: 0,
2445
- register: true
2435
+ register: true,
2436
+ isUnique: false,
2437
+ isMeta: false
2446
2438
  }) {
2447
2439
  this.bootstrap();
2448
2440
  this.validateName(name);
@@ -2453,7 +2445,9 @@ var Cadenza = class {
2453
2445
  throttledIdGetter,
2454
2446
  options.concurrency,
2455
2447
  options.timeout,
2456
- options.register
2448
+ options.register,
2449
+ options.isUnique,
2450
+ options.isMeta
2457
2451
  );
2458
2452
  }
2459
2453
  /**
@@ -2468,18 +2462,17 @@ var Cadenza = class {
2468
2462
  static createThrottledMetaTask(name, func, throttledIdGetter, description, options = {
2469
2463
  concurrency: 0,
2470
2464
  timeout: 0,
2471
- register: true
2465
+ register: true,
2466
+ isUnique: false,
2467
+ isMeta: true
2472
2468
  }) {
2473
- this.bootstrap();
2474
- this.validateName(name);
2475
- return new ThrottledMetaTask(
2469
+ options.isMeta = true;
2470
+ return this.createThrottledTask(
2476
2471
  name,
2477
2472
  func,
2478
- description,
2479
2473
  throttledIdGetter,
2480
- options.concurrency,
2481
- options.timeout,
2482
- options.register
2474
+ description,
2475
+ options
2483
2476
  );
2484
2477
  }
2485
2478
  /**
@@ -2497,7 +2490,9 @@ var Cadenza = class {
2497
2490
  timeout: 0,
2498
2491
  register: true,
2499
2492
  leading: false,
2500
- trailing: true
2493
+ trailing: true,
2494
+ isUnique: false,
2495
+ isMeta: false
2501
2496
  }) {
2502
2497
  this.bootstrap();
2503
2498
  this.validateName(name);
@@ -2510,7 +2505,9 @@ var Cadenza = class {
2510
2505
  options.trailing,
2511
2506
  options.concurrency,
2512
2507
  options.timeout,
2513
- options.register
2508
+ options.register,
2509
+ options.isUnique,
2510
+ options.isMeta
2514
2511
  );
2515
2512
  }
2516
2513
  /**
@@ -2527,20 +2524,17 @@ var Cadenza = class {
2527
2524
  timeout: 0,
2528
2525
  register: true,
2529
2526
  leading: false,
2530
- trailing: true
2527
+ trailing: true,
2528
+ isUnique: false,
2529
+ isMeta: false
2531
2530
  }) {
2532
- this.bootstrap();
2533
- this.validateName(name);
2534
- return new DebouncedMetaTask(
2531
+ options.isMeta = true;
2532
+ return this.createDebounceTask(
2535
2533
  name,
2536
2534
  func,
2537
2535
  description,
2538
2536
  debounceTime,
2539
- options.leading,
2540
- options.trailing,
2541
- options.concurrency,
2542
- options.timeout,
2543
- options.register
2537
+ options
2544
2538
  );
2545
2539
  }
2546
2540
  /**
@@ -2558,7 +2552,9 @@ var Cadenza = class {
2558
2552
  static createEphemeralTask(name, func, description, once = true, destroyCondition = () => true, options = {
2559
2553
  concurrency: 0,
2560
2554
  timeout: 0,
2561
- register: true
2555
+ register: true,
2556
+ isUnique: false,
2557
+ isMeta: false
2562
2558
  }) {
2563
2559
  this.bootstrap();
2564
2560
  this.validateName(name);
@@ -2570,7 +2566,9 @@ var Cadenza = class {
2570
2566
  destroyCondition,
2571
2567
  options.concurrency,
2572
2568
  options.timeout,
2573
- options.register
2569
+ options.register,
2570
+ options.isUnique,
2571
+ options.isMeta
2574
2572
  );
2575
2573
  }
2576
2574
  /**
@@ -2588,17 +2586,14 @@ var Cadenza = class {
2588
2586
  timeout: 0,
2589
2587
  register: true
2590
2588
  }) {
2591
- this.bootstrap();
2592
- this.validateName(name);
2593
- return new EphemeralMetaTask(
2589
+ options.isMeta = true;
2590
+ return this.createEphemeralTask(
2594
2591
  name,
2595
2592
  func,
2596
2593
  description,
2597
2594
  once,
2598
2595
  destroyCondition,
2599
- options.concurrency,
2600
- options.timeout,
2601
- options.register
2596
+ options
2602
2597
  );
2603
2598
  }
2604
2599
  /**
@@ -2628,9 +2623,9 @@ var Cadenza = class {
2628
2623
  this.bootstrap();
2629
2624
  this.validateName(name);
2630
2625
  if (tasks.length === 0) {
2631
- console.warn(`MetaRoutine '${name}' created with no starting tasks.`);
2626
+ console.warn(`Routine '${name}' created with no starting tasks (no-op).`);
2632
2627
  }
2633
- return new MetaRoutine(name, tasks, description);
2628
+ return new GraphRoutine(name, tasks, description, true);
2634
2629
  }
2635
2630
  static reset() {
2636
2631
  var _a, _b;
@@ -2648,11 +2643,11 @@ var SignalTask = class extends Task {
2648
2643
  }
2649
2644
  };
2650
2645
 
2651
- // src/graph/definition/meta/SignalMetaTask.ts
2652
- var SignalMetaTask = class extends SignalTask {
2646
+ // src/graph/definition/UniqueTask.ts
2647
+ var UniqueTask = class extends Task {
2653
2648
  constructor() {
2654
2649
  super(...arguments);
2655
- this.isMeta = true;
2650
+ this.isUnique = true;
2656
2651
  }
2657
2652
  };
2658
2653
 
@@ -2660,22 +2655,16 @@ var SignalMetaTask = class extends SignalTask {
2660
2655
  var index_default = Cadenza;
2661
2656
  export {
2662
2657
  DebounceTask,
2663
- DebouncedMetaTask,
2664
- EphemeralMetaTask,
2665
2658
  EphemeralTask,
2666
2659
  GraphContext,
2667
2660
  GraphRegistry,
2668
2661
  GraphRoutine,
2669
- MetaRoutine,
2670
- MetaTask,
2662
+ GraphRun,
2671
2663
  SignalEmitter,
2672
- SignalMetaTask,
2673
2664
  SignalParticipant,
2674
2665
  SignalTask,
2675
2666
  Task,
2676
- ThrottledMetaTask,
2677
2667
  ThrottledTask,
2678
- UniqueMetaTask,
2679
2668
  UniqueTask,
2680
2669
  index_default as default
2681
2670
  };