@chromahq/core 1.0.51 → 1.0.53

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.
@@ -263,6 +263,139 @@ function getNonceService() {
263
263
  return nonceServiceInstance;
264
264
  }
265
265
 
266
+ const _PopupVisibilityService = class _PopupVisibilityService {
267
+ /**
268
+ * Private constructor - use PopupVisibilityService.instance instead.
269
+ */
270
+ constructor() {
271
+ /** Number of currently connected ports (popup views) */
272
+ this.connectedPortCount = 0;
273
+ /** Listeners for visibility changes */
274
+ this.listeners = /* @__PURE__ */ new Set();
275
+ /** Timestamp of last visibility change */
276
+ this.lastVisibilityChangeAt = 0;
277
+ }
278
+ /**
279
+ * Get the singleton instance of the service.
280
+ */
281
+ static get instance() {
282
+ if (!_PopupVisibilityService._instance) {
283
+ _PopupVisibilityService._instance = new _PopupVisibilityService();
284
+ }
285
+ return _PopupVisibilityService._instance;
286
+ }
287
+ /**
288
+ * Reset the singleton instance (primarily for testing).
289
+ * @internal
290
+ */
291
+ static resetInstance() {
292
+ if (_PopupVisibilityService._instance) {
293
+ _PopupVisibilityService._instance.listeners.clear();
294
+ }
295
+ _PopupVisibilityService._instance = null;
296
+ }
297
+ // ─────────────────────────────────────────────────────────────────────────────
298
+ // Public API
299
+ // ─────────────────────────────────────────────────────────────────────────────
300
+ /**
301
+ * Check if the popup (or any extension view) is currently visible.
302
+ *
303
+ * @returns true if at least one port is connected (popup is open)
304
+ */
305
+ isPopupVisible() {
306
+ return this.connectedPortCount > 0;
307
+ }
308
+ /**
309
+ * Get the number of connected ports.
310
+ *
311
+ * @returns The current count of connected extension views
312
+ */
313
+ getConnectedPortCount() {
314
+ return this.connectedPortCount;
315
+ }
316
+ /**
317
+ * Get the timestamp of the last visibility change.
318
+ *
319
+ * @returns Unix timestamp in milliseconds, or 0 if never changed
320
+ */
321
+ getLastVisibilityChangeAt() {
322
+ return this.lastVisibilityChangeAt;
323
+ }
324
+ /**
325
+ * Register a callback to be notified when visibility changes.
326
+ *
327
+ * @param callback - Function to call when visibility changes
328
+ * @returns Unsubscribe function to remove the listener
329
+ */
330
+ onVisibilityChange(callback) {
331
+ this.listeners.add(callback);
332
+ return () => {
333
+ this.listeners.delete(callback);
334
+ };
335
+ }
336
+ // ─────────────────────────────────────────────────────────────────────────────
337
+ // Internal API (called by BridgeRuntime)
338
+ // ─────────────────────────────────────────────────────────────────────────────
339
+ /**
340
+ * Called when a port connects (popup opens).
341
+ * @internal
342
+ */
343
+ onPortConnected() {
344
+ const wasVisible = this.isPopupVisible();
345
+ this.connectedPortCount++;
346
+ if (!wasVisible && this.isPopupVisible()) {
347
+ this.lastVisibilityChangeAt = Date.now();
348
+ this.notifyListeners(true);
349
+ }
350
+ }
351
+ /**
352
+ * Called when a port disconnects (popup closes).
353
+ * @internal
354
+ */
355
+ onPortDisconnected() {
356
+ const wasVisible = this.isPopupVisible();
357
+ this.connectedPortCount = Math.max(0, this.connectedPortCount - 1);
358
+ if (wasVisible && !this.isPopupVisible()) {
359
+ this.lastVisibilityChangeAt = Date.now();
360
+ this.notifyListeners(false);
361
+ }
362
+ }
363
+ /**
364
+ * Sync the port count with the actual connected ports set.
365
+ * Called by BridgeRuntime to ensure consistency.
366
+ * @internal
367
+ */
368
+ syncPortCount(count) {
369
+ const wasVisible = this.isPopupVisible();
370
+ this.connectedPortCount = count;
371
+ const isNowVisible = this.isPopupVisible();
372
+ if (wasVisible !== isNowVisible) {
373
+ this.lastVisibilityChangeAt = Date.now();
374
+ this.notifyListeners(isNowVisible);
375
+ }
376
+ }
377
+ // ─────────────────────────────────────────────────────────────────────────────
378
+ // Private Methods
379
+ // ─────────────────────────────────────────────────────────────────────────────
380
+ /**
381
+ * Notify all registered listeners of a visibility change.
382
+ */
383
+ notifyListeners(isVisible) {
384
+ this.listeners.forEach((callback) => {
385
+ try {
386
+ callback(isVisible);
387
+ } catch (error) {
388
+ console.error("[PopupVisibilityService] Listener error:", error);
389
+ }
390
+ });
391
+ }
392
+ };
393
+ _PopupVisibilityService._instance = null;
394
+ let PopupVisibilityService = _PopupVisibilityService;
395
+ function getPopupVisibilityService() {
396
+ return PopupVisibilityService.instance;
397
+ }
398
+
266
399
  const DEFAULT_PORT_NAME$1 = "chroma-bridge";
267
400
  const earlyPorts = [];
268
401
  let listenerSetup = false;
@@ -517,6 +650,7 @@ const _BridgeRuntimeManager = class _BridgeRuntimeManager {
517
650
  */
518
651
  setupMessageHandler(port) {
519
652
  this.connectedPorts.add(port);
653
+ PopupVisibilityService.instance.onPortConnected();
520
654
  if (this.keepAlive && this.connectedPorts.size === 1) {
521
655
  this.startKeepAlive();
522
656
  }
@@ -552,6 +686,7 @@ const _BridgeRuntimeManager = class _BridgeRuntimeManager {
552
686
  });
553
687
  port.onDisconnect.addListener(() => {
554
688
  this.connectedPorts.delete(port);
689
+ PopupVisibilityService.instance.onPortDisconnected();
555
690
  const runtimeErrorMessage = chrome.runtime.lastError?.message;
556
691
  if (runtimeErrorMessage) {
557
692
  this.logger.warn(`\u{1F4F4} Port disconnected with error: ${runtimeErrorMessage}`);
@@ -840,7 +975,8 @@ const _BridgeRuntimeManager = class _BridgeRuntimeManager {
840
975
  }
841
976
  }
842
977
  };
843
- _BridgeRuntimeManager.KEEP_ALIVE_INTERVAL_MS = 25e3;
978
+ _BridgeRuntimeManager.KEEP_ALIVE_INTERVAL_MS = 5e4;
979
+ // 50 seconds (increased from 25s to reduce wake-ups)
844
980
  _BridgeRuntimeManager.KEEP_ALIVE_ALARM_NAME = "chroma-bridge-keep-alive";
845
981
  _BridgeRuntimeManager.KEEP_ALIVE_ALARM_PERIOD_MINUTES = 1;
846
982
  let BridgeRuntimeManager = _BridgeRuntimeManager;
@@ -1885,6 +2021,7 @@ class Scheduler {
1885
2021
  async execute(id) {
1886
2022
  const job = this.registry.resolve(id);
1887
2023
  const context = this.registry.getContext(id);
2024
+ const options = this.registry.meta(id);
1888
2025
  if (!job || !context) {
1889
2026
  this.logger.debug(`Job ${id} not found or no context`);
1890
2027
  return;
@@ -1893,6 +2030,16 @@ class Scheduler {
1893
2030
  this.logger.debug(`Job ${id} is paused or stopped, skipping execution`);
1894
2031
  return;
1895
2032
  }
2033
+ if (options?.requiresPopup) {
2034
+ const isPopupVisible = PopupVisibilityService.instance.isPopupVisible();
2035
+ if (!isPopupVisible) {
2036
+ this.logger.debug(`Job ${id} requires popup but popup is not visible, skipping`);
2037
+ if (options?.cron || options?.recurring) {
2038
+ this.schedule(id, options);
2039
+ }
2040
+ return;
2041
+ }
2042
+ }
1896
2043
  try {
1897
2044
  this.registry.updateState(id, JobState.RUNNING);
1898
2045
  this.logger.info(`Executing job ${id}`);
@@ -1901,7 +2048,6 @@ class Scheduler {
1901
2048
  await jobInstance.handle.bind(jobInstance).call(jobInstance, context);
1902
2049
  if (!context.isStopped() && !context.isPaused()) {
1903
2050
  this.registry.updateState(id, JobState.COMPLETED);
1904
- const options = this.registry.meta(id);
1905
2051
  if (options?.cron || options?.recurring) {
1906
2052
  this.registry.updateState(id, JobState.SCHEDULED);
1907
2053
  this.schedule(id, options);
@@ -1910,7 +2056,6 @@ class Scheduler {
1910
2056
  } catch (error) {
1911
2057
  this.logger.error(`Job ${id} execution failed:`, error);
1912
2058
  context.fail(error);
1913
- const options = this.registry.meta(id);
1914
2059
  if (options?.cron || options?.recurring) {
1915
2060
  this.logger.info(`Rescheduling failed recurring job ${id}`);
1916
2061
  this.registry.updateState(id, JobState.SCHEDULED);
@@ -2488,6 +2633,7 @@ class BootstrapBuilder {
2488
2633
  exports.JobRegistry = JobRegistry;
2489
2634
  exports.JobState = JobState;
2490
2635
  exports.NonceService = NonceService;
2636
+ exports.PopupVisibilityService = PopupVisibilityService;
2491
2637
  exports.Scheduler = Scheduler;
2492
2638
  exports.arePortsClaimed = arePortsClaimed;
2493
2639
  exports.bootstrap = bootstrap;
@@ -2495,6 +2641,7 @@ exports.claimEarlyPorts = claimEarlyPorts;
2495
2641
  exports.container = container;
2496
2642
  exports.create = create;
2497
2643
  exports.getNonceService = getNonceService;
2644
+ exports.getPopupVisibilityService = getPopupVisibilityService;
2498
2645
  exports.isEarlyListenerSetup = isEarlyListenerSetup;
2499
2646
  exports.setupEarlyListener = setupEarlyListener;
2500
- //# sourceMappingURL=boot-BQphqIqp.js.map
2647
+ //# sourceMappingURL=boot-CscXvoFf.js.map