@glimt/record 0.0.78 → 0.0.80

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.js CHANGED
@@ -10291,8 +10291,13 @@ const _ObserveManager = class _ObserveManager2 {
10291
10291
  constructor() {
10292
10292
  __publicField(this, "docsObservers", /* @__PURE__ */ new WeakSet());
10293
10293
  __publicField(this, "shadowRootsObservers", /* @__PURE__ */ new WeakSet());
10294
+ __publicField(this, "docsDebounceTimers", /* @__PURE__ */ new WeakMap());
10295
+ __publicField(this, "shadowRootsDebounceTimers", /* @__PURE__ */ new WeakMap());
10296
+ __publicField(this, "debounceTime", 15);
10294
10297
  __publicField(this, "mutationOptions");
10295
10298
  __publicField(this, "emitter", null);
10299
+ __publicField(this, "isSnapshottingShadowRoots", false);
10300
+ __publicField(this, "isSnapshottingDocs", false);
10296
10301
  if (_ObserveManager2.instance) {
10297
10302
  return _ObserveManager2.instance;
10298
10303
  }
@@ -10366,37 +10371,73 @@ const _ObserveManager = class _ObserveManager2 {
10366
10371
  });
10367
10372
  }
10368
10373
  serializeAndEmitDoc(doc) {
10374
+ debugLog(
10375
+ "onDocObserver: doc already observed, emitting full snapshot for doc",
10376
+ doc
10377
+ );
10378
+ this.isSnapshottingDocs = true;
10369
10379
  const serialized = this.serializeDoc(doc);
10370
10380
  this.emitDoc(serialized);
10381
+ this.isSnapshottingDocs = false;
10371
10382
  }
10372
10383
  serializeAndEmitShadowRoot(shadowRoot2) {
10384
+ debugLog(
10385
+ "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10386
+ shadowRoot2
10387
+ );
10388
+ this.isSnapshottingShadowRoots = true;
10373
10389
  const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10374
10390
  this.emitShadowRoot(serialized, shadowRoot2);
10391
+ this.isSnapshottingShadowRoots = false;
10392
+ }
10393
+ debounceEmitDoc(doc) {
10394
+ const existingTimer = this.docsDebounceTimers.get(doc);
10395
+ if (existingTimer) clearTimeout(existingTimer);
10396
+ this.docsDebounceTimers.set(
10397
+ doc,
10398
+ setTimeout(() => {
10399
+ this.docsDebounceTimers.delete(doc);
10400
+ this.serializeAndEmitDoc(doc);
10401
+ }, this.debounceTime)
10402
+ );
10403
+ }
10404
+ debounceEmitShadowRoot(shadowRoot2) {
10405
+ const existingTimer = this.shadowRootsDebounceTimers.get(shadowRoot2);
10406
+ if (existingTimer) clearTimeout(existingTimer);
10407
+ this.shadowRootsDebounceTimers.set(
10408
+ shadowRoot2,
10409
+ setTimeout(() => {
10410
+ this.shadowRootsDebounceTimers.delete(shadowRoot2);
10411
+ this.serializeAndEmitShadowRoot(shadowRoot2);
10412
+ }, this.debounceTime)
10413
+ );
10375
10414
  }
10376
10415
  onDocObserver(doc) {
10416
+ if (this.isSnapshottingDocs) return false;
10377
10417
  if (!this.usable) return false;
10378
10418
  if (!this.docsObservers.has(doc)) {
10379
10419
  this.docsObservers.add(doc);
10380
10420
  return true;
10381
10421
  }
10382
10422
  debugLog(
10383
- "onDocObserver: doc already observed, emitting full snapshot for doc",
10423
+ "onDocObserver: doc already observed, debouncing full snapshot for doc",
10384
10424
  doc
10385
10425
  );
10386
- this.serializeAndEmitDoc(doc);
10426
+ this.debounceEmitDoc(doc);
10387
10427
  return false;
10388
10428
  }
10389
10429
  onShadowRootObserver(shadowRoot2) {
10430
+ if (this.isSnapshottingShadowRoots) return false;
10390
10431
  if (!this.usable) return false;
10391
10432
  if (!this.shadowRootsObservers.has(shadowRoot2)) {
10392
10433
  this.shadowRootsObservers.add(shadowRoot2);
10393
10434
  return true;
10394
10435
  }
10395
10436
  debugLog(
10396
- "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10437
+ "onShadowRootObserver: shadowRoot already observed, debouncing full snapshot for shadowRoot",
10397
10438
  shadowRoot2
10398
10439
  );
10399
- this.serializeAndEmitShadowRoot(shadowRoot2);
10440
+ this.debounceEmitShadowRoot(shadowRoot2);
10400
10441
  return false;
10401
10442
  }
10402
10443
  // canObserveDoc(doc: Document) {