From 468d3d4d2915057a75fab74cc7be5694b545be1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt?= Date: Thu, 27 May 2021 14:18:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[REPLAY-312]=20use=20unpatched?= =?UTF-8?q?=20MutationObserver=20object=20(#866)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rum-core/src/browser/domMutationCollection.ts | 4 ++-- packages/rum-core/src/index.ts | 1 + .../rum-recorder/src/domain/record/mutationObserver.ts | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/rum-core/src/browser/domMutationCollection.ts b/packages/rum-core/src/browser/domMutationCollection.ts index 6d6f20f0fe..09fb38f842 100644 --- a/packages/rum-core/src/browser/domMutationCollection.ts +++ b/packages/rum-core/src/browser/domMutationCollection.ts @@ -10,11 +10,11 @@ interface BrowserWindow extends Window { } } -function getMutationObserverConstructor(): MutationObserverConstructor | undefined { +export function getMutationObserverConstructor(): MutationObserverConstructor | undefined { let constructor: MutationObserverConstructor | undefined const browserWindow: BrowserWindow = window - // Angular uses Zone.js to provide a context persisting accross async tasks. Zone.js replaces the + // Angular uses Zone.js to provide a context persisting across async tasks. Zone.js replaces the // global MutationObserver constructor with a patched version to support the context propagation. // There is an ongoing issue[1][2] with this setup when using a MutationObserver within a Angular // component: on some occasions, the callback is being called in an infinite loop, causing the diff --git a/packages/rum-core/src/index.ts b/packages/rum-core/src/index.ts index e4a81f2eb2..9ae1a3a54c 100644 --- a/packages/rum-core/src/index.ts +++ b/packages/rum-core/src/index.ts @@ -14,3 +14,4 @@ export { startRum } from './boot/rum' export { LifeCycle, LifeCycleEventType } from './domain/lifeCycle' export { ParentContexts } from './domain/parentContexts' export { RumSession } from './domain/rumSession' +export { getMutationObserverConstructor } from './browser/domMutationCollection' diff --git a/packages/rum-recorder/src/domain/record/mutationObserver.ts b/packages/rum-recorder/src/domain/record/mutationObserver.ts index c074bd6258..02d642cda5 100644 --- a/packages/rum-recorder/src/domain/record/mutationObserver.ts +++ b/packages/rum-recorder/src/domain/record/mutationObserver.ts @@ -1,4 +1,5 @@ -import { monitor } from '@datadog/browser-core' +import { monitor, noop } from '@datadog/browser-core' +import { getMutationObserverConstructor } from '@datadog/browser-rum-core' import { getNodeOrAncestorsInputPrivacyMode, nodeOrAncestorsShouldBeHidden } from './privacy' import { getElementInputValue, @@ -29,6 +30,10 @@ type WithSerializedTarget = T & { target: NodeWithSerializedNode } * Buffers and aggregate mutations generated by a MutationObserver into MutationPayload */ export function startMutationObserver(controller: MutationController, mutationCallback: MutationCallBack) { + const MutationObserver = getMutationObserverConstructor() + if (!MutationObserver) { + return { stop: noop } + } const mutationBatch = createMutationBatch((mutations) => { processMutations(mutations.concat(observer.takeRecords() as RumMutationRecord[]), mutationCallback) })