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