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