@chromahq/core 1.0.43 → 1.0.44

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,159 @@ 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
+ chrome.alarms.onAlarm.addListener(this.handleAlarm);
513
+ this.listenerRegistered = true;
514
+ }
515
+ };
516
+ /**
517
+ * Check if Chrome Alarms API is available
518
+ */
519
+ this.isChromeAlarmsAvailable = () => {
520
+ return !!(typeof chrome !== "undefined" && chrome.alarms && typeof chrome.alarms.create === "function" && typeof chrome.alarms.clear === "function" && chrome.alarms.onAlarm?.addListener);
521
+ };
522
+ /**
523
+ * Handle alarm trigger from Chrome
524
+ */
525
+ this.handleAlarm = (alarm) => {
526
+ if (!alarm.name.startsWith(_AlarmAdapter.ALARM_PREFIX)) {
527
+ return;
528
+ }
529
+ const jobId = alarm.name.slice(_AlarmAdapter.ALARM_PREFIX.length);
530
+ this.callbacks.delete(jobId);
531
+ this.triggerCallback?.(jobId);
532
+ };
533
+ this.onTrigger = (callback) => {
534
+ this.triggerCallback = callback;
535
+ };
536
+ this.schedule = (id, when) => {
537
+ this.cancel(id);
538
+ const delay = Math.max(0, when - Date.now());
539
+ const delayInMinutes = delay / 6e4;
540
+ if (this.isChromeAlarmsAvailable() && delayInMinutes >= 0.5) {
541
+ const alarmName = `${_AlarmAdapter.ALARM_PREFIX}${id}`;
542
+ chrome.alarms.create(alarmName, {
543
+ when
544
+ });
545
+ this.callbacks.set(id, () => {
546
+ chrome.alarms.clear(alarmName);
547
+ });
548
+ return null;
549
+ }
550
+ const timeoutId = setTimeout(() => {
551
+ this.callbacks.delete(id);
552
+ this.triggerCallback?.(id);
553
+ }, delay);
554
+ this.callbacks.set(id, () => clearTimeout(timeoutId));
555
+ return timeoutId;
556
+ };
557
+ this.cancel = (id) => {
558
+ const callback = this.callbacks.get(id);
559
+ if (callback) {
560
+ callback();
561
+ this.callbacks.delete(id);
562
+ }
563
+ if (this.isChromeAlarmsAvailable()) {
564
+ const alarmName = `${_AlarmAdapter.ALARM_PREFIX}${id}`;
565
+ chrome.alarms.clear(alarmName);
566
+ }
567
+ };
568
+ /**
569
+ * Get the number of active alarms (for debugging/monitoring)
570
+ */
571
+ this.size = () => {
572
+ return this.callbacks.size;
573
+ };
574
+ /**
575
+ * Clear all alarms (for shutdown)
576
+ */
577
+ this.clear = () => {
578
+ for (const [id, callback] of this.callbacks.entries()) {
579
+ callback();
580
+ }
581
+ this.callbacks.clear();
582
+ if (this.isChromeAlarmsAvailable()) {
583
+ chrome.alarms.getAll((alarms) => {
584
+ for (const alarm of alarms) {
585
+ if (alarm.name.startsWith(_AlarmAdapter.ALARM_PREFIX)) {
586
+ chrome.alarms.clear(alarm.name);
587
+ }
588
+ }
589
+ });
590
+ }
591
+ };
592
+ /**
593
+ * Get diagnostic info about active alarms
594
+ */
595
+ this.getDiagnostics = async () => {
596
+ const chromeAlarms = [];
597
+ if (this.isChromeAlarmsAvailable()) {
598
+ await new Promise((resolve) => {
599
+ chrome.alarms.getAll((alarms) => {
600
+ chromeAlarms.push(...alarms.filter((a) => a.name.startsWith(_AlarmAdapter.ALARM_PREFIX)));
601
+ resolve();
602
+ });
603
+ });
604
+ }
605
+ return {
606
+ trackedAlarms: this.callbacks.size,
607
+ chromeAlarms,
608
+ usingChromeApi: this.isChromeAlarmsAvailable()
609
+ };
610
+ };
611
+ this.initializeAlarmListener();
505
612
  }
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
- }
613
+ };
614
+ _AlarmAdapter.ALARM_PREFIX = "chroma_job_";
615
+ let AlarmAdapter = _AlarmAdapter;
542
616
 
543
617
  class TimeoutAdapter {
544
618
  constructor() {
545
619
  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();
620
+ this.onTrigger = (callback) => {
621
+ this.triggerCallback = callback;
622
+ };
623
+ this.schedule = (id, when) => {
624
+ this.cancel(id);
625
+ const delay = Math.max(0, when - Date.now());
626
+ const timeoutId = setTimeout(() => {
627
+ this.callbacks.delete(id);
628
+ this.triggerCallback?.(id);
629
+ }, delay);
630
+ this.callbacks.set(id, () => clearTimeout(timeoutId));
631
+ return timeoutId;
632
+ };
633
+ this.cancel = (id) => {
634
+ const callback = this.callbacks.get(id);
635
+ if (callback) {
636
+ callback();
637
+ this.callbacks.delete(id);
638
+ }
639
+ };
640
+ /**
641
+ * Get the number of active timers (for debugging/monitoring)
642
+ */
643
+ this.size = () => {
644
+ return this.callbacks.size;
645
+ };
646
+ /**
647
+ * Clear all timers (for shutdown)
648
+ */
649
+ this.clear = () => {
650
+ for (const callback of this.callbacks.values()) {
651
+ callback();
652
+ }
653
+ this.callbacks.clear();
654
+ };
581
655
  }
582
656
  }
583
657
 
@@ -1351,11 +1425,11 @@ class Scheduler {
1351
1425
  this.registry.clearTimers(id);
1352
1426
  const adapter = when - now < 6e4 ? this.timeout : this.alarm;
1353
1427
  const timerId = adapter.schedule(id, when);
1354
- if (adapter === this.timeout) {
1428
+ if (adapter === this.timeout && timerId) {
1355
1429
  this.registry.setTimeoutId(id, timerId);
1356
1430
  }
1357
1431
  this.logger.debug(
1358
- `Job ${id} scheduled for ${new Date(when).toISOString()} (in ${Math.round((when - now) / 1e3)}s)`
1432
+ `Job ${id} scheduled for ${new Date(when).toISOString()} (in ${Math.round((when - now) / 1e3)}s) using ${adapter === this.alarm ? "alarm" : "timeout"}`
1359
1433
  );
1360
1434
  }
1361
1435
  pause(id) {
@@ -1970,4 +2044,4 @@ exports.JobState = JobState;
1970
2044
  exports.bootstrap = bootstrap;
1971
2045
  exports.container = container;
1972
2046
  exports.create = create;
1973
- //# sourceMappingURL=boot-DT7r8uVG.js.map
2047
+ //# sourceMappingURL=boot-BzVib0b9.js.map