@glimt/record 0.0.75 → 0.0.76

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.
@@ -10333,40 +10333,142 @@ class IframeManager {
10333
10333
  }
10334
10334
  const _ObserveManager = class _ObserveManager2 {
10335
10335
  constructor() {
10336
- __publicField(this, "docsObservers", /* @__PURE__ */ new WeakMap());
10337
- __publicField(this, "shadowRootsObserved", /* @__PURE__ */ new WeakMap());
10336
+ __publicField(this, "docsObservers", /* @__PURE__ */ new WeakSet());
10337
+ __publicField(this, "shadowRootsObservers", /* @__PURE__ */ new WeakMap());
10338
+ __publicField(this, "mutationOptions");
10339
+ __publicField(this, "emitter", null);
10338
10340
  if (_ObserveManager2.instance) {
10339
10341
  return _ObserveManager2.instance;
10340
10342
  }
10341
10343
  _ObserveManager2.instance = this;
10342
10344
  }
10343
- canObserveDoc(doc) {
10344
- return !this.docsObservers.has(doc);
10345
+ setEmitter(emitter) {
10346
+ this.emitter = emitter;
10345
10347
  }
10346
- canObserveShadowRoot(shadowRoot2) {
10347
- return !this.shadowRootsObserved.has(shadowRoot2);
10348
+ setMutationOptions(options) {
10349
+ this.mutationOptions = options;
10348
10350
  }
10349
- observerAttached(doc, onCleanup) {
10350
- debugLog("[doc] attaching observer to doc", doc);
10351
- if (this.docsObservers.has(doc)) {
10352
- debugLog("[doc] detected existing observer, cleaning up old observer");
10353
- const cleanupFn = this.docsObservers.get(doc);
10354
- cleanupFn == null ? void 0 : cleanupFn();
10355
- }
10356
- this.docsObservers.set(doc, onCleanup);
10351
+ get usable() {
10352
+ const result2 = this.emitter != null;
10353
+ if (!result2) debugLog("observerManager: emitter is null");
10354
+ return result2;
10357
10355
  }
10358
- observerAttachedToShadow(shadowRoot2, onCleanup) {
10359
- debugLog("[shadow] attaching observer to shadowRoot", shadowRoot2);
10360
- if (this.shadowRootsObserved.has(shadowRoot2)) {
10361
- debugLog("[shadow] detected existing observer, cleaning up old observer");
10362
- const cleanupFn = this.shadowRootsObserved.get(shadowRoot2);
10363
- cleanupFn == null ? void 0 : cleanupFn();
10356
+ serializeDoc(doc) {
10357
+ var _a2, _b2, _c;
10358
+ if (!((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return null;
10359
+ const serialized = serializeNodeWithId(doc, __spreadProps(__spreadValues({}, this.mutationOptions), {
10360
+ doc,
10361
+ skipChild: false,
10362
+ maskTextFn: (_b2 = this.mutationOptions) == null ? void 0 : _b2.maskTextFn,
10363
+ maskInputFn: (_c = this.mutationOptions) == null ? void 0 : _c.maskInputFn
10364
+ }));
10365
+ if (!serialized) {
10366
+ debugLog("snapshotDoc: no serialized node");
10367
+ return null;
10364
10368
  }
10365
- this.shadowRootsObserved.set(shadowRoot2, onCleanup);
10369
+ return serialized;
10370
+ }
10371
+ emitDoc(serialized) {
10372
+ if (!this.emitter || !serialized) return;
10373
+ this.emitter({
10374
+ type: EventType.FullSnapshot,
10375
+ data: {
10376
+ node: serialized,
10377
+ initialOffset: {
10378
+ left: 0,
10379
+ top: 0
10380
+ }
10381
+ }
10382
+ });
10366
10383
  }
10384
+ emitShadowRoot(serialized, shadowRoot2) {
10385
+ var _a2;
10386
+ if (!this.emitter || !serialized || !((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return;
10387
+ const hostId = this.mutationOptions.mirror.getId(shadowRoot2.host);
10388
+ const shadowId = this.mutationOptions.mirror.getId(shadowRoot2);
10389
+ this.emitter({
10390
+ type: EventType.IncrementalSnapshot,
10391
+ data: {
10392
+ source: IncrementalSource.Mutation,
10393
+ adds: [
10394
+ {
10395
+ parentId: hostId,
10396
+ nextId: null,
10397
+ node: serialized
10398
+ }
10399
+ ],
10400
+ removes: [
10401
+ {
10402
+ id: shadowId,
10403
+ parentId: hostId
10404
+ }
10405
+ ],
10406
+ attributes: [],
10407
+ texts: []
10408
+ }
10409
+ });
10410
+ }
10411
+ serializeAndEmitDoc(doc) {
10412
+ const serialized = this.serializeDoc(doc);
10413
+ this.emitDoc(serialized);
10414
+ }
10415
+ serializeAndEmitShadowRoot(shadowRoot2) {
10416
+ const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10417
+ this.emitShadowRoot(serialized, shadowRoot2);
10418
+ }
10419
+ onDocObserver(doc) {
10420
+ if (!this.usable) return false;
10421
+ if (!this.docsObservers.has(doc)) return true;
10422
+ debugLog(
10423
+ "onDocObserver: doc already observed, emitting full snapshot for doc",
10424
+ doc
10425
+ );
10426
+ this.serializeAndEmitDoc(doc);
10427
+ return false;
10428
+ }
10429
+ onShadowRootObserver(shadowRoot2) {
10430
+ if (!this.usable) return false;
10431
+ if (!this.shadowRootsObservers.has(shadowRoot2)) return true;
10432
+ debugLog(
10433
+ "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10434
+ shadowRoot2
10435
+ );
10436
+ this.serializeAndEmitShadowRoot(shadowRoot2);
10437
+ return false;
10438
+ }
10439
+ // canObserveDoc(doc: Document) {
10440
+ // if (!this.usable) return false;
10441
+ // const hasObserver = this.docsObservers.has(doc);
10442
+ // if (!hasObserver) return true;
10443
+ // return false;
10444
+ // }
10445
+ // canObserveShadowRoot(shadowRoot: ShadowRoot) {
10446
+ // if (!this.usable) return false;
10447
+ // return !this.shadowRootsObserved.has(shadowRoot);
10448
+ // }
10449
+ // observerAttached(doc: Document, onCleanup: VoidFunction) {
10450
+ // if (!this.usable) return;
10451
+ // debugLog('[doc] attaching observer to doc', doc);
10452
+ // if (this.docsObservers.has(doc)) {
10453
+ // debugLog('[doc] detected existing observer, cleaning up old observer');
10454
+ // const cleanupFn = this.docsObservers.get(doc);
10455
+ // cleanupFn?.();
10456
+ // }
10457
+ // this.docsObservers.set(doc, onCleanup);
10458
+ // }
10459
+ // observerAttachedToShadow(shadowRoot: ShadowRoot, onCleanup: VoidFunction) {
10460
+ // if (!this.usable) return;
10461
+ // debugLog('[shadow] attaching observer to shadowRoot', shadowRoot);
10462
+ // if (this.shadowRootsObserved.has(shadowRoot)) {
10463
+ // debugLog('[shadow] detected existing observer, cleaning up old observer');
10464
+ // const cleanupFn = this.shadowRootsObserved.get(shadowRoot);
10465
+ // cleanupFn?.();
10466
+ // }
10467
+ // this.shadowRootsObserved.set(shadowRoot, onCleanup);
10468
+ // }
10367
10469
  destroy() {
10368
- this.docsObservers = /* @__PURE__ */ new WeakMap();
10369
- this.shadowRootsObserved = /* @__PURE__ */ new WeakMap();
10470
+ this.docsObservers = /* @__PURE__ */ new WeakSet();
10471
+ this.shadowRootsObservers = /* @__PURE__ */ new WeakMap();
10370
10472
  }
10371
10473
  };
10372
10474
  __publicField(_ObserveManager, "instance");
@@ -12921,11 +13023,10 @@ class ShadowDomManager {
12921
13023
  }
12922
13024
  addShadowRoot(shadowRoot2, doc) {
12923
13025
  if (!isNativeShadowDom(shadowRoot2)) return;
12924
- if (!observeManager.canObserveShadowRoot(shadowRoot2)) return;
13026
+ if (!observeManager.onShadowRootObserver(shadowRoot2)) return;
12925
13027
  if (this.shadowDoms.has(shadowRoot2)) return;
12926
13028
  this.shadowDoms.add(shadowRoot2);
12927
13029
  debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12928
- const observeId = makeid();
12929
13030
  const observer = initMutationObserver(
12930
13031
  __spreadProps(__spreadValues({}, this.bypassOptions), {
12931
13032
  doc,
@@ -12935,8 +13036,8 @@ class ShadowDomManager {
12935
13036
  }),
12936
13037
  shadowRoot2
12937
13038
  );
12938
- this.mappedRestoreHandlers[observeId] = [
12939
- () => observer.disconnect(),
13039
+ this.restoreHandlers.push(() => observer.disconnect());
13040
+ this.restoreHandlers.push(
12940
13041
  initScrollObserver(__spreadProps(__spreadValues({}, this.bypassOptions), {
12941
13042
  scrollCb: this.scrollCb,
12942
13043
  // https://gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813
@@ -12944,14 +13045,14 @@ class ShadowDomManager {
12944
13045
  doc: shadowRoot2,
12945
13046
  mirror: this.mirror
12946
13047
  }))
12947
- ];
13048
+ );
12948
13049
  setTimeout(() => {
12949
13050
  if (shadowRoot2.adoptedStyleSheets && shadowRoot2.adoptedStyleSheets.length > 0)
12950
13051
  this.bypassOptions.stylesheetManager.adoptStyleSheets(
12951
13052
  shadowRoot2.adoptedStyleSheets,
12952
13053
  this.mirror.getId(index$1.host(shadowRoot2))
12953
13054
  );
12954
- this.mappedRestoreHandlers[observeId].push(
13055
+ this.restoreHandlers.push(
12955
13056
  initAdoptedStyleSheetObserver(
12956
13057
  {
12957
13058
  mirror: this.mirror,
@@ -12961,18 +13062,6 @@ class ShadowDomManager {
12961
13062
  )
12962
13063
  );
12963
13064
  }, 0);
12964
- observeManager.observerAttachedToShadow(shadowRoot2, () => {
12965
- const handlers = this.mappedRestoreHandlers[observeId];
12966
- if (handlers) {
12967
- for (const handler of handlers) {
12968
- try {
12969
- handler();
12970
- } catch (e2) {
12971
- }
12972
- }
12973
- }
12974
- delete this.mappedRestoreHandlers[observeId];
12975
- });
12976
13065
  }
12977
13066
  /**
12978
13067
  * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms.
@@ -13320,6 +13409,29 @@ function record(options = {}) {
13320
13409
  },
13321
13410
  mirror
13322
13411
  });
13412
+ observeManager.setEmitter(wrappedEmit);
13413
+ observeManager.setMutationOptions({
13414
+ blockClass,
13415
+ blockSelector,
13416
+ maskTextClass,
13417
+ maskTextSelector,
13418
+ inlineStylesheet,
13419
+ maskInputOptions,
13420
+ maskTextFn,
13421
+ maskInputFn,
13422
+ slimDOMOptions,
13423
+ dataURLOptions,
13424
+ canvasManager,
13425
+ stylesheetManager,
13426
+ shadowDomManager,
13427
+ recordCanvas,
13428
+ inlineImages,
13429
+ mirror,
13430
+ iframeManager,
13431
+ keepIframeSrcFn,
13432
+ mutationCb: wrappedMutationEmit,
13433
+ processedNodeManager
13434
+ });
13323
13435
  takeFullSnapshot$1 = (isCheckout = false) => {
13324
13436
  if (!recordDOM) {
13325
13437
  return;
@@ -13512,14 +13624,8 @@ function record(options = {}) {
13512
13624
  };
13513
13625
  iframeManager.addLoadListener((iframeEl) => {
13514
13626
  try {
13515
- if (!observeManager.canObserveDoc(iframeEl.contentDocument)) return;
13516
- const stopObserve = observe(iframeEl.contentDocument);
13517
- const id = makeid();
13518
- registeredHandlers[id] = stopObserve;
13519
- observeManager.observerAttached(iframeEl.contentDocument, () => {
13520
- stopObserve();
13521
- delete registeredHandlers[id];
13522
- });
13627
+ if (!observeManager.onDocObserver(iframeEl.contentDocument)) return;
13628
+ handlers.push(observe(iframeEl.contentDocument));
13523
13629
  } catch (error) {
13524
13630
  if (isDebug()) {
13525
13631
  console.warn("internal error");