@glimt/record 0.0.72 → 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");
@@ -10372,6 +10426,7 @@ const _MutationRateLimiter = class _MutationRateLimiter2 {
10372
10426
  if (!this.inGlobalStorm) return;
10373
10427
  this.debounceTimeout = setTimeout(() => {
10374
10428
  this.debounceTimeout = null;
10429
+ debugLog(`MutationRateLimiter, stopping storm because of debounce`);
10375
10430
  this.stormStopped();
10376
10431
  }, this.debounceTimeoutMs);
10377
10432
  }
@@ -10543,14 +10598,6 @@ class DoubleLinkedList {
10543
10598
  }
10544
10599
  }
10545
10600
  const moveKey = (id, parentId) => `${id}@${parentId}`;
10546
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
10547
- function makeid(length = 8) {
10548
- let result2 = "";
10549
- for (let i2 = 0; i2 < length; i2++) {
10550
- result2 += characters.charAt(Math.floor(Math.random() * characters.length));
10551
- }
10552
- return result2;
10553
- }
10554
10601
  class MutationBuffer {
10555
10602
  constructor() {
10556
10603
  __publicField(this, "frozen", false);
@@ -12826,6 +12873,7 @@ class ShadowDomManager {
12826
12873
  __publicField(this, "bypassOptions");
12827
12874
  __publicField(this, "mirror");
12828
12875
  __publicField(this, "restoreHandlers", []);
12876
+ __publicField(this, "mappedRestoreHandlers", {});
12829
12877
  this.mutationCb = options.mutationCb;
12830
12878
  this.scrollCb = options.scrollCb;
12831
12879
  this.bypassOptions = options.bypassOptions;
@@ -12840,6 +12888,8 @@ class ShadowDomManager {
12840
12888
  if (!isNativeShadowDom(shadowRoot2)) return;
12841
12889
  if (this.shadowDoms.has(shadowRoot2)) return;
12842
12890
  this.shadowDoms.add(shadowRoot2);
12891
+ debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12892
+ const observeId = makeid();
12843
12893
  const observer = initMutationObserver(
12844
12894
  {
12845
12895
  ...this.bypassOptions,
@@ -12850,8 +12900,8 @@ class ShadowDomManager {
12850
12900
  },
12851
12901
  shadowRoot2
12852
12902
  );
12853
- this.restoreHandlers.push(() => observer.disconnect());
12854
- this.restoreHandlers.push(
12903
+ this.mappedRestoreHandlers[observeId] = [
12904
+ () => observer.disconnect(),
12855
12905
  initScrollObserver({
12856
12906
  ...this.bypassOptions,
12857
12907
  scrollCb: this.scrollCb,
@@ -12860,14 +12910,14 @@ class ShadowDomManager {
12860
12910
  doc: shadowRoot2,
12861
12911
  mirror: this.mirror
12862
12912
  })
12863
- );
12913
+ ];
12864
12914
  setTimeout(() => {
12865
12915
  if (shadowRoot2.adoptedStyleSheets && shadowRoot2.adoptedStyleSheets.length > 0)
12866
12916
  this.bypassOptions.stylesheetManager.adoptStyleSheets(
12867
12917
  shadowRoot2.adoptedStyleSheets,
12868
12918
  this.mirror.getId(index$1.host(shadowRoot2))
12869
12919
  );
12870
- this.restoreHandlers.push(
12920
+ this.mappedRestoreHandlers[observeId].push(
12871
12921
  initAdoptedStyleSheetObserver(
12872
12922
  {
12873
12923
  mirror: this.mirror,
@@ -12877,6 +12927,18 @@ class ShadowDomManager {
12877
12927
  )
12878
12928
  );
12879
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
+ });
12880
12942
  }
12881
12943
  /**
12882
12944
  * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms.
@@ -12916,6 +12978,15 @@ class ShadowDomManager {
12916
12978
  } catch (e2) {
12917
12979
  }
12918
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 = {};
12919
12990
  this.restoreHandlers = [];
12920
12991
  this.shadowDoms = /* @__PURE__ */ new WeakSet();
12921
12992
  }
@@ -13294,6 +13365,7 @@ function record(options = {}) {
13294
13365
  };
13295
13366
  try {
13296
13367
  const handlers = [];
13368
+ const registeredHandlers = {};
13297
13369
  const observe = (doc) => {
13298
13370
  var _a2;
13299
13371
  return callbackWrapper(initObservers)(
@@ -13419,7 +13491,13 @@ function record(options = {}) {
13419
13491
  };
13420
13492
  iframeManager.addLoadListener((iframeEl) => {
13421
13493
  try {
13422
- 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
+ });
13423
13501
  } catch (error) {
13424
13502
  if (isDebug()) {
13425
13503
  console.warn("internal error");
@@ -13478,7 +13556,9 @@ function record(options = {}) {
13478
13556
  }
13479
13557
  return () => {
13480
13558
  handlers.forEach((h) => h());
13559
+ Object.values(registeredHandlers).forEach((h) => h());
13481
13560
  processedNodeManager.destroy();
13561
+ observeManager.destroy();
13482
13562
  recording = false;
13483
13563
  unregisterErrorHandler();
13484
13564
  };