@glimt/record 0.0.73 → 0.0.74

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/record.cjs CHANGED
@@ -9956,6 +9956,21 @@ function inDom(n2) {
9956
9956
  if (!doc) return false;
9957
9957
  return index$1.contains(doc, n2) || shadowHostInDom(n2);
9958
9958
  }
9959
+ const isDebug = () => {
9960
+ return "_rrweb_is_debug" in window && window._rrweb_is_debug ? window._rrweb_is_debug : false;
9961
+ };
9962
+ const debugLog = (...args) => {
9963
+ if (!isDebug()) return;
9964
+ console.log("[recapt:rrweb]", ...args);
9965
+ };
9966
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9967
+ function makeid(length = 8) {
9968
+ let result2 = "";
9969
+ for (let i2 = 0; i2 < length; i2++) {
9970
+ result2 += characters.charAt(Math.floor(Math.random() * characters.length));
9971
+ }
9972
+ return result2;
9973
+ }
9959
9974
  let errorHandler;
9960
9975
  function registerErrorHandler(handler) {
9961
9976
  errorHandler = handler;
@@ -10274,13 +10289,52 @@ class IframeManager {
10274
10289
  }
10275
10290
  }
10276
10291
  }
10277
- const isDebug = () => {
10278
- return "_rrweb_is_debug" in window && window._rrweb_is_debug ? window._rrweb_is_debug : false;
10279
- };
10280
- const debugLog = (...args) => {
10281
- if (!isDebug()) return;
10282
- console.log("[recapt:rrweb]", ...args);
10292
+ const _ObserveManager = class _ObserveManager2 {
10293
+ constructor() {
10294
+ __publicField(this, "docsObservers", /* @__PURE__ */ new WeakMap());
10295
+ __publicField(this, "shadowRootsObserved", /* @__PURE__ */ new WeakMap());
10296
+ if (_ObserveManager2.instance) {
10297
+ return _ObserveManager2.instance;
10298
+ }
10299
+ _ObserveManager2.instance = this;
10300
+ }
10301
+ observerAttached(doc, onCleanup) {
10302
+ debugLog("[doc] attaching observer to doc", doc);
10303
+ if (this.docsObservers.has(doc)) {
10304
+ debugLog("[doc] detected existing observer, cleaning up old observer");
10305
+ const cleanupFn = this.docsObservers.get(doc);
10306
+ cleanupFn == null ? void 0 : cleanupFn();
10307
+ }
10308
+ this.docsObservers.set(doc, onCleanup);
10309
+ }
10310
+ observerAttachedToShadow(shadowRoot2, onCleanup) {
10311
+ debugLog("[shadow] attaching observer to shadowRoot", shadowRoot2);
10312
+ if (this.shadowRootsObserved.has(shadowRoot2)) {
10313
+ debugLog("[shadow] detected existing observer, cleaning up old observer");
10314
+ const cleanupFn = this.shadowRootsObserved.get(shadowRoot2);
10315
+ cleanupFn == null ? void 0 : cleanupFn();
10316
+ }
10317
+ this.shadowRootsObserved.set(shadowRoot2, onCleanup);
10318
+ }
10319
+ // attachObserverToDoc(doc: Document, observerMethod: (doc: Document)) {}
10320
+ // canObserveDoc(doc: Document) {
10321
+ // if (this.docsObservers.has(doc)) return false;
10322
+ // this.docsObservers.set(doc);
10323
+ // return true;
10324
+ // }
10325
+ // canObserveShadow(shadowRoot: ShadowRoot) {
10326
+ // if (this.shadowRootsObserved.has(shadowRoot)) return false;
10327
+ // this.shadowRootsObserved.add(shadowRoot);
10328
+ // return true;
10329
+ // }
10330
+ destroy() {
10331
+ this.docsObservers = /* @__PURE__ */ new WeakMap();
10332
+ this.shadowRootsObserved = /* @__PURE__ */ new WeakMap();
10333
+ }
10283
10334
  };
10335
+ __publicField(_ObserveManager, "instance");
10336
+ let ObserveManager = _ObserveManager;
10337
+ const observeManager = new ObserveManager();
10284
10338
  const _MutationRateLimiter = class _MutationRateLimiter2 {
10285
10339
  constructor() {
10286
10340
  __publicField(this, "mutTracker");
@@ -10544,14 +10598,6 @@ class DoubleLinkedList {
10544
10598
  }
10545
10599
  }
10546
10600
  const moveKey = (id, parentId) => `${id}@${parentId}`;
10547
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
10548
- function makeid(length = 8) {
10549
- let result2 = "";
10550
- for (let i2 = 0; i2 < length; i2++) {
10551
- result2 += characters.charAt(Math.floor(Math.random() * characters.length));
10552
- }
10553
- return result2;
10554
- }
10555
10601
  class MutationBuffer {
10556
10602
  constructor() {
10557
10603
  __publicField(this, "frozen", false);
@@ -12192,7 +12238,6 @@ function initObservers(o2, hooks = {}) {
12192
12238
  mergeHooks(o2, hooks);
12193
12239
  let mutationObserver;
12194
12240
  if (o2.recordDOM) {
12195
- debugLog("adding mutation observer in initObservers");
12196
12241
  mutationObserver = initMutationObserver(o2, o2.doc);
12197
12242
  }
12198
12243
  const mousemoveHandler = initMoveObserver(o2);
@@ -12828,6 +12873,7 @@ class ShadowDomManager {
12828
12873
  __publicField(this, "bypassOptions");
12829
12874
  __publicField(this, "mirror");
12830
12875
  __publicField(this, "restoreHandlers", []);
12876
+ __publicField(this, "mappedRestoreHandlers", {});
12831
12877
  this.mutationCb = options.mutationCb;
12832
12878
  this.scrollCb = options.scrollCb;
12833
12879
  this.bypassOptions = options.bypassOptions;
@@ -12843,6 +12889,7 @@ class ShadowDomManager {
12843
12889
  if (this.shadowDoms.has(shadowRoot2)) return;
12844
12890
  this.shadowDoms.add(shadowRoot2);
12845
12891
  debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12892
+ const observeId = makeid();
12846
12893
  const observer = initMutationObserver(
12847
12894
  {
12848
12895
  ...this.bypassOptions,
@@ -12853,8 +12900,8 @@ class ShadowDomManager {
12853
12900
  },
12854
12901
  shadowRoot2
12855
12902
  );
12856
- this.restoreHandlers.push(() => observer.disconnect());
12857
- this.restoreHandlers.push(
12903
+ this.mappedRestoreHandlers[observeId] = [
12904
+ () => observer.disconnect(),
12858
12905
  initScrollObserver({
12859
12906
  ...this.bypassOptions,
12860
12907
  scrollCb: this.scrollCb,
@@ -12863,14 +12910,14 @@ class ShadowDomManager {
12863
12910
  doc: shadowRoot2,
12864
12911
  mirror: this.mirror
12865
12912
  })
12866
- );
12913
+ ];
12867
12914
  setTimeout(() => {
12868
12915
  if (shadowRoot2.adoptedStyleSheets && shadowRoot2.adoptedStyleSheets.length > 0)
12869
12916
  this.bypassOptions.stylesheetManager.adoptStyleSheets(
12870
12917
  shadowRoot2.adoptedStyleSheets,
12871
12918
  this.mirror.getId(index$1.host(shadowRoot2))
12872
12919
  );
12873
- this.restoreHandlers.push(
12920
+ this.mappedRestoreHandlers[observeId].push(
12874
12921
  initAdoptedStyleSheetObserver(
12875
12922
  {
12876
12923
  mirror: this.mirror,
@@ -12880,6 +12927,18 @@ class ShadowDomManager {
12880
12927
  )
12881
12928
  );
12882
12929
  }, 0);
12930
+ observeManager.observerAttachedToShadow(shadowRoot2, () => {
12931
+ const handlers = this.mappedRestoreHandlers[observeId];
12932
+ if (handlers) {
12933
+ for (const handler of handlers) {
12934
+ try {
12935
+ handler();
12936
+ } catch (e2) {
12937
+ }
12938
+ }
12939
+ }
12940
+ delete this.mappedRestoreHandlers[observeId];
12941
+ });
12883
12942
  }
12884
12943
  /**
12885
12944
  * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms.
@@ -12919,6 +12978,15 @@ class ShadowDomManager {
12919
12978
  } catch (e2) {
12920
12979
  }
12921
12980
  });
12981
+ Object.values(this.mappedRestoreHandlers).forEach((handlers) => {
12982
+ for (const handler of handlers) {
12983
+ try {
12984
+ handler();
12985
+ } catch (e2) {
12986
+ }
12987
+ }
12988
+ });
12989
+ this.mappedRestoreHandlers = {};
12922
12990
  this.restoreHandlers = [];
12923
12991
  this.shadowDoms = /* @__PURE__ */ new WeakSet();
12924
12992
  }
@@ -13297,6 +13365,7 @@ function record(options = {}) {
13297
13365
  };
13298
13366
  try {
13299
13367
  const handlers = [];
13368
+ const registeredHandlers = {};
13300
13369
  const observe = (doc) => {
13301
13370
  var _a2;
13302
13371
  return callbackWrapper(initObservers)(
@@ -13422,8 +13491,13 @@ function record(options = {}) {
13422
13491
  };
13423
13492
  iframeManager.addLoadListener((iframeEl) => {
13424
13493
  try {
13425
- debugLog("adding observers for new iframe", iframeEl);
13426
- handlers.push(observe(iframeEl.contentDocument));
13494
+ const stopObserve = observe(iframeEl.contentDocument);
13495
+ const id = makeid();
13496
+ registeredHandlers[id] = stopObserve;
13497
+ observeManager.observerAttached(iframeEl.contentDocument, () => {
13498
+ stopObserve();
13499
+ delete registeredHandlers[id];
13500
+ });
13427
13501
  } catch (error) {
13428
13502
  if (isDebug()) {
13429
13503
  console.warn("internal error");
@@ -13482,7 +13556,9 @@ function record(options = {}) {
13482
13556
  }
13483
13557
  return () => {
13484
13558
  handlers.forEach((h) => h());
13559
+ Object.values(registeredHandlers).forEach((h) => h());
13485
13560
  processedNodeManager.destroy();
13561
+ observeManager.destroy();
13486
13562
  recording = false;
13487
13563
  unregisterErrorHandler();
13488
13564
  };