@mushi-mushi/web 1.32.2 → 1.32.3

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/headless.cjs CHANGED
@@ -184,6 +184,10 @@ function createNetworkCapture(options = {}) {
184
184
  const _prevXhrState = xhrStateMap.get(this);
185
185
  if (_prevXhrState?._listener) {
186
186
  this.removeEventListener("readystatechange", _prevXhrState._listener);
187
+ if (_prevXhrState.correlationId) {
188
+ const idx = _activeCorrelationStack.lastIndexOf(_prevXhrState.correlationId);
189
+ if (idx !== -1) _activeCorrelationStack.splice(idx, 1);
190
+ }
187
191
  }
188
192
  xhrStateMap.set(this, {
189
193
  method: String(method).toUpperCase(),
@@ -212,6 +216,8 @@ function createNetworkCapture(options = {}) {
212
216
  if (state.correlationId) _activeCorrelationStack.push(state.correlationId);
213
217
  const _readystateHandler = () => {
214
218
  if (this.readyState !== 4) return;
219
+ this.removeEventListener("readystatechange", _readystateHandler);
220
+ state._listener = void 0;
215
221
  if (state.shouldRecord) {
216
222
  const duration = Date.now() - state.startTime;
217
223
  const entry = {
@@ -237,7 +243,19 @@ function createNetworkCapture(options = {}) {
237
243
  state._listener = _readystateHandler;
238
244
  this.addEventListener("readystatechange", _readystateHandler);
239
245
  }
240
- originalXhrSend.call(this, body);
246
+ try {
247
+ originalXhrSend.call(this, body);
248
+ } catch (err) {
249
+ if (state?.correlationId) {
250
+ const idx = _activeCorrelationStack.lastIndexOf(state.correlationId);
251
+ if (idx !== -1) _activeCorrelationStack.splice(idx, 1);
252
+ }
253
+ if (state?._listener) {
254
+ this.removeEventListener("readystatechange", state._listener);
255
+ state._listener = void 0;
256
+ }
257
+ throw err;
258
+ }
241
259
  };
242
260
  }
243
261
  const xhrOpenWrapper = OriginalXHR ? XMLHttpRequest.prototype.open : null;
@@ -903,10 +921,8 @@ function createElementSelector() {
903
921
  }
904
922
 
905
923
  // src/history-patch.ts
906
- var capturedPushState = null;
907
- var capturedReplaceState = null;
908
- var nativePushState = null;
909
- var nativeReplaceState = null;
924
+ var prevPushState = null;
925
+ var prevReplaceState = null;
910
926
  var pushWrapper = null;
911
927
  var replaceWrapper = null;
912
928
  var popListener = null;
@@ -938,24 +954,24 @@ function notifyPop() {
938
954
  }
939
955
  function ensurePatch() {
940
956
  if (patched || typeof window === "undefined") return;
941
- nativePushState = History.prototype.pushState;
942
- nativeReplaceState = History.prototype.replaceState;
943
- capturedPushState = nativePushState.bind(history);
944
- capturedReplaceState = nativeReplaceState.bind(history);
945
- pushWrapper = function historyPatchPushState(...args) {
946
- const call = capturedPushState ?? History.prototype.pushState.bind(history);
947
- const ret = call(...args);
948
- notifyPush();
957
+ const prevPush = history.pushState;
958
+ const prevReplace = history.replaceState;
959
+ prevPushState = prevPush;
960
+ prevReplaceState = prevReplace;
961
+ const push = function historyPatchPushState(...args) {
962
+ const ret = prevPush.apply(history, args);
963
+ if (pushWrapper === push) notifyPush();
949
964
  return ret;
950
965
  };
951
- replaceWrapper = function historyPatchReplaceState(...args) {
952
- const call = capturedReplaceState ?? History.prototype.replaceState.bind(history);
953
- const ret = call(...args);
954
- notifyReplace();
966
+ const replace = function historyPatchReplaceState(...args) {
967
+ const ret = prevReplace.apply(history, args);
968
+ if (replaceWrapper === replace) notifyReplace();
955
969
  return ret;
956
970
  };
957
- history.pushState = pushWrapper;
958
- history.replaceState = replaceWrapper;
971
+ pushWrapper = push;
972
+ replaceWrapper = replace;
973
+ history.pushState = push;
974
+ history.replaceState = replace;
959
975
  popListener = () => notifyPop();
960
976
  window.addEventListener("popstate", popListener);
961
977
  patched = true;
@@ -966,22 +982,22 @@ function uninstallPatch() {
966
982
  window.removeEventListener("popstate", popListener);
967
983
  popListener = null;
968
984
  }
969
- if (pushWrapper && history.pushState === pushWrapper && nativePushState) {
970
- history.pushState = nativePushState;
985
+ if (pushWrapper && history.pushState === pushWrapper && prevPushState) {
986
+ history.pushState = prevPushState;
971
987
  }
972
- if (replaceWrapper && history.replaceState === replaceWrapper && nativeReplaceState) {
973
- history.replaceState = nativeReplaceState;
988
+ if (replaceWrapper && history.replaceState === replaceWrapper && prevReplaceState) {
989
+ history.replaceState = prevReplaceState;
974
990
  }
975
991
  pushWrapper = null;
976
992
  replaceWrapper = null;
977
- capturedPushState = null;
978
- capturedReplaceState = null;
979
- nativePushState = null;
980
- nativeReplaceState = null;
993
+ prevPushState = null;
994
+ prevReplaceState = null;
981
995
  patched = false;
982
996
  }
983
997
  function subscribeHistory(sub) {
984
998
  ensurePatch();
999
+ if (!patched) return () => {
1000
+ };
985
1001
  subscribers.add(sub);
986
1002
  return () => {
987
1003
  subscribers.delete(sub);
@@ -1035,8 +1051,10 @@ function createTimelineCapture() {
1035
1051
  onReplace: () => recordRoute("replaceState"),
1036
1052
  onPop: () => recordRoute("popstate")
1037
1053
  });
1038
- window.addEventListener("hashchange", handleHashChange);
1039
- document.addEventListener("click", handleClick, true);
1054
+ const hasWindow = typeof window !== "undefined";
1055
+ const hasDocument = typeof document !== "undefined";
1056
+ if (hasWindow) window.addEventListener("hashchange", handleHashChange);
1057
+ if (hasDocument) document.addEventListener("click", handleClick, true);
1040
1058
  return {
1041
1059
  setScreen(screen) {
1042
1060
  record({
@@ -1075,8 +1093,8 @@ function createTimelineCapture() {
1075
1093
  },
1076
1094
  destroy() {
1077
1095
  unsubHistory();
1078
- window.removeEventListener("hashchange", handleHashChange);
1079
- document.removeEventListener("click", handleClick, true);
1096
+ if (hasWindow) window.removeEventListener("hashchange", handleHashChange);
1097
+ if (hasDocument) document.removeEventListener("click", handleClick, true);
1080
1098
  }
1081
1099
  };
1082
1100
  }