@loggerjs/browser 0.1.0 → 0.3.1

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.
package/dist/index.js CHANGED
@@ -470,7 +470,7 @@ function browserServiceWorkerTransport(options = {}) {
470
470
  const mapBatch = options.mapBatch ?? defaultMapBatch;
471
471
  const queue = [];
472
472
  let readyWorker;
473
- let readyStarted = false;
473
+ let readyPromise;
474
474
  let lastContext;
475
475
  const reportError = (error, context, operation, droppedEvents = 0) => {
476
476
  try {
@@ -521,27 +521,35 @@ function browserServiceWorkerTransport(options = {}) {
521
521
  post(worker, item, context);
522
522
  }
523
523
  };
524
- const startReady = (context) => {
525
- if (target !== "ready" || readyStarted) return;
526
- readyStarted = true;
524
+ const dropQueued = (reason) => {
525
+ const pending = queue.splice(0);
526
+ for (const item of pending) reportDrop(item, reason);
527
+ return pending.reduce((total, item) => total + item.events.length, 0);
528
+ };
529
+ const waitForReady = (context) => {
530
+ if (currentWorker()) return Promise.resolve();
531
+ if (target !== "ready") return Promise.reject(/* @__PURE__ */ new Error("service worker controller is not available"));
532
+ if (readyPromise) return readyPromise;
527
533
  const container = options.serviceWorker ?? serviceWorkerContainer();
528
534
  if (!container?.ready) {
529
- reportError(/* @__PURE__ */ new Error("serviceWorker.ready is not available"), context, "ready");
530
- return;
535
+ const error = /* @__PURE__ */ new Error("serviceWorker.ready is not available");
536
+ reportError(error, context, "ready", dropQueued("ready"));
537
+ return Promise.reject(error);
531
538
  }
532
- container.ready.then((registration) => {
539
+ readyPromise = container.ready.then((registration) => {
533
540
  readyWorker = workerFromRegistration(registration);
534
- if (!readyWorker) {
535
- const pending = queue.splice(0);
536
- for (const item of pending) reportDrop(item, "unavailable");
537
- return;
538
- }
539
- drain(context);
541
+ if (!readyWorker) throw new Error("service worker registration has no active worker");
542
+ drain(context ?? lastContext);
540
543
  }).catch((error) => {
541
- const pending = queue.splice(0);
542
- for (const item of pending) reportDrop(item, "ready");
543
- reportError(error, context, "ready", pending.reduce((total, item) => total + item.events.length, 0));
544
+ readyPromise = void 0;
545
+ const droppedEvents = dropQueued("ready");
546
+ reportError(error, context ?? lastContext, "ready", droppedEvents);
547
+ throw error;
544
548
  });
549
+ return readyPromise;
550
+ };
551
+ const startReady = (context) => {
552
+ waitForReady(context).catch(() => {});
545
553
  };
546
554
  const enqueue = (item) => {
547
555
  if (queue.length >= maxQueueSize) {
@@ -575,6 +583,9 @@ function browserServiceWorkerTransport(options = {}) {
575
583
  queueSize() {
576
584
  return queue.length;
577
585
  },
586
+ ready() {
587
+ return waitForReady();
588
+ },
578
589
  log(event, context) {
579
590
  send({
580
591
  events: [event],