@mushi-mushi/web 1.22.0 → 1.22.2

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.cjs CHANGED
@@ -2553,7 +2553,7 @@ function getWidgetStyles(theme, accent = "", accentText = "") {
2553
2553
 
2554
2554
  // src/version.ts
2555
2555
  var MUSHI_SDK_PACKAGE = "@mushi-mushi/web";
2556
- var MUSHI_SDK_VERSION = "1.22.0" ;
2556
+ var MUSHI_SDK_VERSION = "1.22.2" ;
2557
2557
  var CATEGORY_ICONS = {
2558
2558
  bug: "\u26A0\uFE0F",
2559
2559
  slow: "\u{1F40C}",
@@ -5579,13 +5579,42 @@ async function fetchLeaderboard(limit = 10) {
5579
5579
  return null;
5580
5580
  }
5581
5581
  }
5582
+ function teardown() {
5583
+ if (flushTimer) {
5584
+ clearInterval(flushTimer);
5585
+ flushTimer = null;
5586
+ }
5587
+ if (dwellTimer) {
5588
+ clearInterval(dwellTimer);
5589
+ dwellTimer = null;
5590
+ }
5591
+ removeActivityListeners();
5592
+ removeTierBadge();
5593
+ if (consentHost) {
5594
+ consentHost.remove();
5595
+ consentHost = null;
5596
+ }
5597
+ pendingEvents = [];
5598
+ currentUserId = null;
5599
+ apiClient = null;
5600
+ optedIn = false;
5601
+ tierCache = null;
5602
+ rewardsApiBackoffUntil = 0;
5603
+ }
5582
5604
  var routeObserver = null;
5583
5605
  var clickHandler = null;
5606
+ var popstateHandler = null;
5584
5607
  var origPushState = null;
5608
+ var pushStateWrapper = null;
5585
5609
  var lastRoute = "";
5586
5610
  var listenersInstalled = false;
5587
5611
  function installActivityListeners(projectId) {
5588
- if (listenersInstalled) return;
5612
+ if (listenersInstalled && pushStateWrapper && history.pushState === pushStateWrapper) {
5613
+ return;
5614
+ }
5615
+ if (listenersInstalled) {
5616
+ removeActivityListeners();
5617
+ }
5589
5618
  listenersInstalled = true;
5590
5619
  const emitRoute = () => {
5591
5620
  const route = location.pathname;
@@ -5597,11 +5626,14 @@ function installActivityListeners(projectId) {
5597
5626
  enqueue({ action: "screen_view_unique_per_day", metadata: { route } });
5598
5627
  }
5599
5628
  };
5600
- origPushState = history.pushState.bind(history);
5601
- history.pushState = function(...args) {
5602
- origPushState(...args);
5629
+ const capturedPushState = history.pushState.bind(history);
5630
+ origPushState = capturedPushState;
5631
+ pushStateWrapper = function(...args) {
5632
+ capturedPushState(...args);
5603
5633
  emitRoute();
5604
5634
  };
5635
+ history.pushState = pushStateWrapper;
5636
+ popstateHandler = emitRoute;
5605
5637
  window.addEventListener("popstate", emitRoute);
5606
5638
  emitRoute();
5607
5639
  routeObserver = new MutationObserver(() => emitRoute());
@@ -5616,6 +5648,24 @@ function installActivityListeners(projectId) {
5616
5648
  };
5617
5649
  document.addEventListener("click", clickHandler, { capture: true, passive: true });
5618
5650
  }
5651
+ function removeActivityListeners() {
5652
+ listenersInstalled = false;
5653
+ if (origPushState && history.pushState === pushStateWrapper) {
5654
+ history.pushState = origPushState;
5655
+ }
5656
+ origPushState = null;
5657
+ pushStateWrapper = null;
5658
+ if (popstateHandler) {
5659
+ window.removeEventListener("popstate", popstateHandler);
5660
+ popstateHandler = null;
5661
+ }
5662
+ routeObserver?.disconnect();
5663
+ routeObserver = null;
5664
+ if (clickHandler) {
5665
+ document.removeEventListener("click", clickHandler, { capture: true });
5666
+ clickHandler = null;
5667
+ }
5668
+ }
5619
5669
  var badgeHost = null;
5620
5670
  function renderTierBadge(tier, config) {
5621
5671
  if (!config.showInWidget) return;
@@ -5664,6 +5714,10 @@ function renderTierBadge(tier, config) {
5664
5714
  `;
5665
5715
  document.body.appendChild(badgeHost);
5666
5716
  }
5717
+ function removeTierBadge() {
5718
+ badgeHost?.remove();
5719
+ badgeHost = null;
5720
+ }
5667
5721
  var consentHost = null;
5668
5722
  function renderConsentBanner(projectId, config) {
5669
5723
  if (consentHost) return;
@@ -5738,11 +5792,12 @@ var MAX_MESSAGE_LENGTH = 500;
5738
5792
  function createConsoleCapture() {
5739
5793
  const entries = [];
5740
5794
  const originals = {};
5795
+ const wrappers = {};
5741
5796
  const levels = ["log", "warn", "error", "info", "debug"];
5742
5797
  for (const level of levels) {
5743
5798
  const original = console[level];
5744
5799
  originals[level] = original;
5745
- console[level] = (...args) => {
5800
+ const wrapper = (...args) => {
5746
5801
  const message = args.map((arg) => {
5747
5802
  try {
5748
5803
  return typeof arg === "string" ? arg : JSON.stringify(arg);
@@ -5764,6 +5819,8 @@ function createConsoleCapture() {
5764
5819
  }
5765
5820
  original.call(console, ...args);
5766
5821
  };
5822
+ wrappers[level] = wrapper;
5823
+ console[level] = wrapper;
5767
5824
  }
5768
5825
  return {
5769
5826
  getEntries() {
@@ -5774,7 +5831,7 @@ function createConsoleCapture() {
5774
5831
  },
5775
5832
  destroy() {
5776
5833
  for (const level of levels) {
5777
- if (originals[level]) {
5834
+ if (originals[level] && console[level] === wrappers[level]) {
5778
5835
  console[level] = originals[level];
5779
5836
  }
5780
5837
  }
@@ -5872,7 +5929,8 @@ function createNetworkCapture(options = {}) {
5872
5929
  const entries = [];
5873
5930
  const originalFetch = globalThis.fetch;
5874
5931
  let activeOptions = options;
5875
- globalThis.fetch = async function mushiFetchInterceptor(input, init) {
5932
+ let fetchWrapper;
5933
+ globalThis.fetch = fetchWrapper = async function mushiFetchInterceptor(input, init) {
5876
5934
  const startTime = Date.now();
5877
5935
  const method = init?.method?.toUpperCase() ?? "GET";
5878
5936
  const url = getRequestUrl(input);
@@ -5936,7 +5994,9 @@ function createNetworkCapture(options = {}) {
5936
5994
  activeOptions = nextOptions;
5937
5995
  },
5938
5996
  destroy() {
5939
- globalThis.fetch = originalFetch;
5997
+ if (globalThis.fetch === fetchWrapper) {
5998
+ globalThis.fetch = originalFetch;
5999
+ }
5940
6000
  }
5941
6001
  };
5942
6002
  }
@@ -6388,8 +6448,8 @@ function createElementSelector() {
6388
6448
  var MAX_TIMELINE_ENTRIES = 120;
6389
6449
  function createTimelineCapture() {
6390
6450
  const entries = [];
6391
- const originalPushState = history.pushState;
6392
- const originalReplaceState = history.replaceState;
6451
+ const capturedPushState = history.pushState.bind(history);
6452
+ const capturedReplaceState = history.replaceState.bind(history);
6393
6453
  const handlePopState = () => recordRoute("popstate");
6394
6454
  const handleHashChange = () => recordRoute("hashchange");
6395
6455
  recordRoute("initial");
@@ -6423,16 +6483,18 @@ function createTimelineCapture() {
6423
6483
  }
6424
6484
  });
6425
6485
  }
6426
- history.pushState = function mushiPushState(...args) {
6427
- const result = originalPushState.apply(this, args);
6486
+ const pushStateWrapper2 = function mushiPushState(...args) {
6487
+ const result = capturedPushState(...args);
6428
6488
  recordRoute("pushState");
6429
6489
  return result;
6430
6490
  };
6431
- history.replaceState = function mushiReplaceState(...args) {
6432
- const result = originalReplaceState.apply(this, args);
6491
+ const replaceStateWrapper = function mushiReplaceState(...args) {
6492
+ const result = capturedReplaceState(...args);
6433
6493
  recordRoute("replaceState");
6434
6494
  return result;
6435
6495
  };
6496
+ history.pushState = pushStateWrapper2;
6497
+ history.replaceState = replaceStateWrapper;
6436
6498
  window.addEventListener("popstate", handlePopState);
6437
6499
  window.addEventListener("hashchange", handleHashChange);
6438
6500
  document.addEventListener("click", handleClick, true);
@@ -6473,8 +6535,12 @@ function createTimelineCapture() {
6473
6535
  entries.length = 0;
6474
6536
  },
6475
6537
  destroy() {
6476
- history.pushState = originalPushState;
6477
- history.replaceState = originalReplaceState;
6538
+ if (history.pushState === pushStateWrapper2) {
6539
+ history.pushState = capturedPushState;
6540
+ }
6541
+ if (history.replaceState === replaceStateWrapper) {
6542
+ history.replaceState = capturedReplaceState;
6543
+ }
6478
6544
  window.removeEventListener("popstate", handlePopState);
6479
6545
  window.removeEventListener("hashchange", handleHashChange);
6480
6546
  document.removeEventListener("click", handleClick, true);
@@ -8390,6 +8456,8 @@ function createInstance(config) {
8390
8456
  networkCap?.destroy();
8391
8457
  perfCap?.destroy();
8392
8458
  elementSelector?.deactivate();
8459
+ detachAutoBreadcrumbs?.();
8460
+ detachAutoBreadcrumbs = null;
8393
8461
  timelineCap.destroy();
8394
8462
  discoveryCap?.destroy();
8395
8463
  discoveryCap = null;
@@ -8397,8 +8465,7 @@ function createInstance(config) {
8397
8465
  replayCap = null;
8398
8466
  offlineQueue.stopAutoSync();
8399
8467
  stopReporterInboxPolling();
8400
- detachAutoBreadcrumbs?.();
8401
- detachAutoBreadcrumbs = null;
8468
+ teardown();
8402
8469
  breadcrumbs.clear();
8403
8470
  listeners.clear();
8404
8471
  instance = null;
@@ -9063,34 +9130,40 @@ function installAutoBreadcrumbs(buffer) {
9063
9130
  const onPop = () => dispatchRouteChange("popstate");
9064
9131
  window.addEventListener("popstate", onPop, { passive: true });
9065
9132
  cleanups.push(() => window.removeEventListener("popstate", onPop));
9066
- const origPush = window.history.pushState;
9067
- const origReplace = window.history.replaceState;
9068
- window.history.pushState = function patched(...args) {
9069
- const ret = origPush.apply(this, args);
9133
+ const capturedPush = window.history.pushState.bind(window.history);
9134
+ const capturedReplace = window.history.replaceState.bind(window.history);
9135
+ const pushPatched = function patched(...args) {
9136
+ const ret = capturedPush(...args);
9070
9137
  try {
9071
9138
  dispatchRouteChange("pushState");
9072
9139
  } catch {
9073
9140
  }
9074
9141
  return ret;
9075
9142
  };
9076
- window.history.replaceState = function patched(...args) {
9077
- const ret = origReplace.apply(this, args);
9143
+ const replacePatched = function patched(...args) {
9144
+ const ret = capturedReplace(...args);
9078
9145
  try {
9079
9146
  dispatchRouteChange("replaceState");
9080
9147
  } catch {
9081
9148
  }
9082
9149
  return ret;
9083
9150
  };
9151
+ window.history.pushState = pushPatched;
9152
+ window.history.replaceState = replacePatched;
9084
9153
  cleanups.push(() => {
9085
- window.history.pushState = origPush;
9086
- window.history.replaceState = origReplace;
9154
+ if (window.history.pushState === pushPatched) {
9155
+ window.history.pushState = capturedPush;
9156
+ }
9157
+ if (window.history.replaceState === replacePatched) {
9158
+ window.history.replaceState = capturedReplace;
9159
+ }
9087
9160
  });
9088
9161
  } catch {
9089
9162
  }
9090
9163
  try {
9091
9164
  const origError = console.error;
9092
9165
  const origWarn = console.warn;
9093
- console.error = function(...args) {
9166
+ const errorWrapper = function(...args) {
9094
9167
  try {
9095
9168
  buffer.add({
9096
9169
  category: "console",
@@ -9101,7 +9174,7 @@ function installAutoBreadcrumbs(buffer) {
9101
9174
  }
9102
9175
  return origError.apply(this, args);
9103
9176
  };
9104
- console.warn = function(...args) {
9177
+ const warnWrapper = function(...args) {
9105
9178
  try {
9106
9179
  buffer.add({
9107
9180
  category: "console",
@@ -9112,9 +9185,11 @@ function installAutoBreadcrumbs(buffer) {
9112
9185
  }
9113
9186
  return origWarn.apply(this, args);
9114
9187
  };
9188
+ console.error = errorWrapper;
9189
+ console.warn = warnWrapper;
9115
9190
  cleanups.push(() => {
9116
- console.error = origError;
9117
- console.warn = origWarn;
9191
+ if (console.error === errorWrapper) console.error = origError;
9192
+ if (console.warn === warnWrapper) console.warn = origWarn;
9118
9193
  });
9119
9194
  } catch {
9120
9195
  }