@quazardous/quarkernel 2.2.4 → 2.3.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/fsm.js CHANGED
@@ -458,6 +458,39 @@ var Composition = class {
458
458
  onComposed(listener, options) {
459
459
  return this.kernel.on(COMPOSED_EVENT, listener, options);
460
460
  }
461
+ /**
462
+ * Wait for the next composition completion as a Promise
463
+ *
464
+ * @param options - Optional timeout configuration
465
+ * @returns Promise resolving with composed event data
466
+ *
467
+ * @example
468
+ * ```typescript
469
+ * // .then() receives: { sources, contexts, merged }
470
+ * const result = await composition.once();
471
+ * console.log(result.data.merged); // merged context from all sources
472
+ * console.log(result.data.sources); // ['event1', 'event2']
473
+ *
474
+ * // With timeout
475
+ * const result = await composition.once({ timeout: 5000 });
476
+ * ```
477
+ */
478
+ once(options) {
479
+ return new Promise((resolve, reject) => {
480
+ let timeoutId;
481
+ const listener = (event) => {
482
+ if (timeoutId) clearTimeout(timeoutId);
483
+ resolve(event);
484
+ };
485
+ const unbind = this.kernel.on(COMPOSED_EVENT, listener, { once: true });
486
+ if (options?.timeout) {
487
+ timeoutId = setTimeout(() => {
488
+ unbind();
489
+ reject(new Error(`composition.once() timed out after ${options.timeout}ms`));
490
+ }, options.timeout);
491
+ }
492
+ });
493
+ }
461
494
  /**
462
495
  * Remove a listener for composed events
463
496
  */
@@ -709,6 +742,42 @@ var Kernel = class _Kernel {
709
742
  }
710
743
  return () => this.off(event, listener);
711
744
  }
745
+ /**
746
+ * Wait for an event once (Promise-based)
747
+ *
748
+ * For callback style, use: `qk.on(event, listener, { once: true })`
749
+ *
750
+ * @param eventName - Event to wait for
751
+ * @param options - Optional timeout in ms
752
+ * @returns Promise resolving with the event
753
+ *
754
+ * @example
755
+ * ```typescript
756
+ * // .then() receives: IKernelEvent { name, data, context, timestamp }
757
+ * const event = await qk.once('user:loaded');
758
+ * console.log(event.data); // event payload
759
+ * console.log(event.context); // shared context
760
+ *
761
+ * // With timeout (rejects if event doesn't fire)
762
+ * const event = await qk.once('user:loaded', { timeout: 5000 });
763
+ * ```
764
+ */
765
+ once(eventName, options) {
766
+ return new Promise((resolve, reject) => {
767
+ let timeoutId;
768
+ const listener = (event) => {
769
+ if (timeoutId) clearTimeout(timeoutId);
770
+ resolve(event);
771
+ };
772
+ const unbind = this.on(eventName, listener, { once: true });
773
+ if (options?.timeout) {
774
+ timeoutId = setTimeout(() => {
775
+ unbind();
776
+ reject(new Error(`once('${String(eventName)}') timed out after ${options.timeout}ms`));
777
+ }, options.timeout);
778
+ }
779
+ });
780
+ }
712
781
  /**
713
782
  * Remove an event listener
714
783
  * If no listener provided, removes all listeners for the event
@@ -1308,6 +1377,37 @@ function useMachine(kernel, config) {
1308
1377
  history = [...snapshot2.history];
1309
1378
  }
1310
1379
  },
1380
+ waitFor(targetState, options) {
1381
+ if (currentState === targetState) {
1382
+ return Promise.resolve({
1383
+ state: currentState,
1384
+ context: structuredClone(context)
1385
+ });
1386
+ }
1387
+ return new Promise((resolve, reject) => {
1388
+ let timeoutId;
1389
+ const unbind = kernel.on(
1390
+ `${prefix}:enter:${targetState}`,
1391
+ (event) => {
1392
+ if (timeoutId) clearTimeout(timeoutId);
1393
+ unbind();
1394
+ resolve({
1395
+ state: targetState,
1396
+ from: event.data?.from,
1397
+ event: event.data?.event,
1398
+ context: structuredClone(context)
1399
+ });
1400
+ }
1401
+ );
1402
+ cleanupFns.push(unbind);
1403
+ if (options?.timeout) {
1404
+ timeoutId = setTimeout(() => {
1405
+ unbind();
1406
+ reject(new Error(`waitFor('${targetState}') timed out after ${options.timeout}ms`));
1407
+ }, options.timeout);
1408
+ }
1409
+ });
1410
+ },
1311
1411
  destroy() {
1312
1412
  for (const cleanup of cleanupFns) {
1313
1413
  cleanup();