@chromahq/core 1.0.43 → 1.0.45

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.
@@ -497,85 +497,189 @@ function bootstrap$1(options) {
497
497
  return runtime;
498
498
  }
499
499
 
500
- class AlarmAdapter {
500
+ const _AlarmAdapter = class _AlarmAdapter {
501
501
  constructor() {
502
502
  this.callbacks = /* @__PURE__ */ new Map();
503
+ this.listenerRegistered = false;
504
+ /**
505
+ * Initialize the Chrome Alarms listener (once)
506
+ */
507
+ this.initializeAlarmListener = () => {
508
+ if (this.listenerRegistered) return;
509
+ if (this.isChromeAlarmsAvailable()) {
510
+ this.clearStaleAlarms();
511
+ chrome.alarms.onAlarm.addListener(this.handleAlarm);
512
+ this.listenerRegistered = true;
513
+ console.log("[AlarmAdapter] \u2705 Chrome Alarms API available and listener registered");
514
+ } else {
515
+ console.log(
516
+ "[AlarmAdapter] \u26A0\uFE0F Chrome Alarms API not available - will use setTimeout fallback"
517
+ );
518
+ }
519
+ };
520
+ /**
521
+ * Clear any stale chroma alarms from previous SW instances
522
+ */
523
+ this.clearStaleAlarms = () => {
524
+ if (!this.isChromeAlarmsAvailable()) return;
525
+ chrome.alarms.getAll((alarms) => {
526
+ const staleAlarms = alarms.filter((a) => a.name.startsWith(_AlarmAdapter.ALARM_PREFIX));
527
+ if (staleAlarms.length > 0) {
528
+ console.log(
529
+ `[AlarmAdapter] \u{1F9F9} Clearing ${staleAlarms.length} stale alarms from previous session`
530
+ );
531
+ staleAlarms.forEach((alarm) => {
532
+ chrome.alarms.clear(alarm.name);
533
+ });
534
+ }
535
+ });
536
+ };
537
+ /**
538
+ * Check if Chrome Alarms API is available
539
+ */
540
+ this.isChromeAlarmsAvailable = () => {
541
+ return !!(typeof chrome !== "undefined" && chrome.alarms && typeof chrome.alarms.create === "function" && typeof chrome.alarms.clear === "function" && chrome.alarms.onAlarm?.addListener);
542
+ };
543
+ /**
544
+ * Handle alarm trigger from Chrome
545
+ */
546
+ this.handleAlarm = (alarm) => {
547
+ if (!alarm.name.startsWith(_AlarmAdapter.ALARM_PREFIX)) {
548
+ return;
549
+ }
550
+ const jobId = alarm.name.slice(_AlarmAdapter.ALARM_PREFIX.length);
551
+ console.log(`[AlarmAdapter] \u{1F514} Chrome Alarm fired: ${jobId}`);
552
+ this.callbacks.delete(jobId);
553
+ this.triggerCallback?.(jobId);
554
+ };
555
+ this.onTrigger = (callback) => {
556
+ this.triggerCallback = callback;
557
+ };
558
+ this.schedule = (id, when) => {
559
+ this.cancel(id);
560
+ const delay = Math.max(0, when - Date.now());
561
+ const delayInMinutes = delay / 6e4;
562
+ if (this.isChromeAlarmsAvailable() && delayInMinutes >= 0.5) {
563
+ const alarmName = `${_AlarmAdapter.ALARM_PREFIX}${id}`;
564
+ chrome.alarms.create(alarmName, {
565
+ when
566
+ });
567
+ console.log(
568
+ `[AlarmAdapter] \u23F0 Chrome Alarm scheduled: ${id} in ${Math.round(delay / 1e3)}s`
569
+ );
570
+ this.callbacks.set(id, () => {
571
+ chrome.alarms.clear(alarmName);
572
+ });
573
+ return null;
574
+ }
575
+ console.log(
576
+ `[AlarmAdapter] \u23F1\uFE0F setTimeout fallback: ${id} in ${Math.round(delay / 1e3)}s (Chrome Alarms: ${this.isChromeAlarmsAvailable() ? "available but delay too short" : "unavailable"})`
577
+ );
578
+ const timeoutId = setTimeout(() => {
579
+ this.callbacks.delete(id);
580
+ this.triggerCallback?.(id);
581
+ }, delay);
582
+ this.callbacks.set(id, () => clearTimeout(timeoutId));
583
+ return timeoutId;
584
+ };
585
+ this.cancel = (id) => {
586
+ const callback = this.callbacks.get(id);
587
+ if (callback) {
588
+ callback();
589
+ this.callbacks.delete(id);
590
+ }
591
+ if (this.isChromeAlarmsAvailable()) {
592
+ const alarmName = `${_AlarmAdapter.ALARM_PREFIX}${id}`;
593
+ chrome.alarms.clear(alarmName);
594
+ }
595
+ };
596
+ /**
597
+ * Get the number of active alarms (for debugging/monitoring)
598
+ */
599
+ this.size = () => {
600
+ return this.callbacks.size;
601
+ };
602
+ /**
603
+ * Clear all alarms (for shutdown)
604
+ */
605
+ this.clear = () => {
606
+ for (const [id, callback] of this.callbacks.entries()) {
607
+ callback();
608
+ }
609
+ this.callbacks.clear();
610
+ if (this.isChromeAlarmsAvailable()) {
611
+ chrome.alarms.getAll((alarms) => {
612
+ for (const alarm of alarms) {
613
+ if (alarm.name.startsWith(_AlarmAdapter.ALARM_PREFIX)) {
614
+ chrome.alarms.clear(alarm.name);
615
+ }
616
+ }
617
+ });
618
+ }
619
+ };
620
+ /**
621
+ * Get diagnostic info about active alarms
622
+ */
623
+ this.getDiagnostics = async () => {
624
+ const chromeAlarms = [];
625
+ if (this.isChromeAlarmsAvailable()) {
626
+ await new Promise((resolve) => {
627
+ chrome.alarms.getAll((alarms) => {
628
+ chromeAlarms.push(...alarms.filter((a) => a.name.startsWith(_AlarmAdapter.ALARM_PREFIX)));
629
+ resolve();
630
+ });
631
+ });
632
+ }
633
+ return {
634
+ trackedAlarms: this.callbacks.size,
635
+ chromeAlarms,
636
+ usingChromeApi: this.isChromeAlarmsAvailable()
637
+ };
638
+ };
639
+ this.initializeAlarmListener();
503
640
  }
504
- onTrigger(callback) {
505
- this.triggerCallback = callback;
506
- }
507
- schedule(id, when) {
508
- this.cancel(id);
509
- const delay = Math.max(0, when - Date.now());
510
- const timeoutId = setTimeout(() => {
511
- this.callbacks.delete(id);
512
- this.triggerCallback?.(id);
513
- }, delay);
514
- this.callbacks.set(id, () => clearTimeout(timeoutId));
515
- return timeoutId;
516
- }
517
- cancel(id) {
518
- const callback = this.callbacks.get(id);
519
- if (callback) {
520
- callback();
521
- this.callbacks.delete(id);
522
- }
523
- }
524
- /**
525
- * Get the number of active alarms (for debugging/monitoring)
526
- */
527
- size() {
528
- return this.callbacks.size;
529
- }
530
- /**
531
- * Clear all alarms (for shutdown)
532
- */
533
- clear() {
534
- for (const callback of this.callbacks.values()) {
535
- callback();
536
- }
537
- this.callbacks.clear();
538
- }
539
- }
641
+ };
642
+ _AlarmAdapter.ALARM_PREFIX = "chroma_job_";
643
+ let AlarmAdapter = _AlarmAdapter;
540
644
 
541
645
  class TimeoutAdapter {
542
646
  constructor() {
543
647
  this.callbacks = /* @__PURE__ */ new Map();
544
- }
545
- onTrigger(callback) {
546
- this.triggerCallback = callback;
547
- }
548
- schedule(id, when) {
549
- this.cancel(id);
550
- const delay = Math.max(0, when - Date.now());
551
- const timeoutId = setTimeout(() => {
552
- this.callbacks.delete(id);
553
- this.triggerCallback?.(id);
554
- }, delay);
555
- this.callbacks.set(id, () => clearTimeout(timeoutId));
556
- return timeoutId;
557
- }
558
- cancel(id) {
559
- const callback = this.callbacks.get(id);
560
- if (callback) {
561
- callback();
562
- this.callbacks.delete(id);
563
- }
564
- }
565
- /**
566
- * Get the number of active timers (for debugging/monitoring)
567
- */
568
- size() {
569
- return this.callbacks.size;
570
- }
571
- /**
572
- * Clear all timers (for shutdown)
573
- */
574
- clear() {
575
- for (const callback of this.callbacks.values()) {
576
- callback();
577
- }
578
- this.callbacks.clear();
648
+ this.onTrigger = (callback) => {
649
+ this.triggerCallback = callback;
650
+ };
651
+ this.schedule = (id, when) => {
652
+ this.cancel(id);
653
+ const delay = Math.max(0, when - Date.now());
654
+ const timeoutId = setTimeout(() => {
655
+ this.callbacks.delete(id);
656
+ this.triggerCallback?.(id);
657
+ }, delay);
658
+ this.callbacks.set(id, () => clearTimeout(timeoutId));
659
+ return timeoutId;
660
+ };
661
+ this.cancel = (id) => {
662
+ const callback = this.callbacks.get(id);
663
+ if (callback) {
664
+ callback();
665
+ this.callbacks.delete(id);
666
+ }
667
+ };
668
+ /**
669
+ * Get the number of active timers (for debugging/monitoring)
670
+ */
671
+ this.size = () => {
672
+ return this.callbacks.size;
673
+ };
674
+ /**
675
+ * Clear all timers (for shutdown)
676
+ */
677
+ this.clear = () => {
678
+ for (const callback of this.callbacks.values()) {
679
+ callback();
680
+ }
681
+ this.callbacks.clear();
682
+ };
579
683
  }
580
684
  }
581
685
 
@@ -1347,13 +1451,14 @@ class Scheduler {
1347
1451
  this.alarm.cancel(id);
1348
1452
  this.timeout.cancel(id);
1349
1453
  this.registry.clearTimers(id);
1350
- const adapter = when - now < 6e4 ? this.timeout : this.alarm;
1454
+ const delayMs = when - now;
1455
+ const adapter = delayMs < 3e4 ? this.timeout : this.alarm;
1351
1456
  const timerId = adapter.schedule(id, when);
1352
- if (adapter === this.timeout) {
1457
+ if (adapter === this.timeout && timerId) {
1353
1458
  this.registry.setTimeoutId(id, timerId);
1354
1459
  }
1355
- this.logger.debug(
1356
- `Job ${id} scheduled for ${new Date(when).toISOString()} (in ${Math.round((when - now) / 1e3)}s)`
1460
+ this.logger.info(
1461
+ `[Scheduler] Job "${id}" scheduled for ${new Date(when).toISOString()} (in ${Math.round(delayMs / 1e3)}s) \u2192 ${adapter === this.alarm ? "\u23F0 AlarmAdapter" : "\u23F1\uFE0F TimeoutAdapter"}`
1357
1462
  );
1358
1463
  }
1359
1464
  pause(id) {
@@ -1965,4 +2070,4 @@ class BootstrapBuilder {
1965
2070
  }
1966
2071
 
1967
2072
  export { JobState as J, create as a, bootstrap as b, container as c };
1968
- //# sourceMappingURL=boot-DKPBDDrp.js.map
2073
+ //# sourceMappingURL=boot-PbJQqgzX.js.map