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