Skip to content

Commit

Permalink
⚡️ start mutation observer only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed May 19, 2021
1 parent 8ce7325 commit 0841bdd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/rum-core/src/boot/rum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function startRum(userConfiguration: RumUserConfiguration, getCommonConte
startRequestCollection(lifeCycle, configuration)
startPerformanceCollection(lifeCycle, configuration)
startDOMMutationCollection(lifeCycle)

const internalContext = startInternalContext(userConfiguration.applicationId, session, parentContexts)

return {
Expand Down
6 changes: 1 addition & 5 deletions packages/rum-core/src/browser/domMutationCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export function startDOMMutationCollection(lifeCycle: LifeCycle) {
let observer: MutationObserver | undefined
const MutationObserver = getMutationObserverConstructor()
if (MutationObserver) {
observer = new MutationObserver(
monitor(() => {
lifeCycle.notify(LifeCycleEventType.DOM_MUTATED)
})
)
observer = new MutationObserver(monitor(() => lifeCycle.notify(LifeCycleEventType.DOM_MUTATED)))

observer.observe(document.documentElement, {
attributes: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/rum-core/src/domain/trackPageActivities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { monitor, Observable, TimeStamp, timeStampNow } from '@datadog/browser-core'
import { startDOMMutationCollection } from '../browser/domMutationCollection'
import { LifeCycle, LifeCycleEventType, Subscription } from './lifeCycle'

// Delay to wait for a page activity to validate the tracking process
Expand Down Expand Up @@ -66,6 +67,8 @@ export function trackPageActivities(
let firstRequestIndex: undefined | number
let pendingRequestsCount = 0

const DOMMutationCollection = startDOMMutationCollection(lifeCycle)

subscriptions.push(lifeCycle.subscribe(LifeCycleEventType.DOM_MUTATED, () => notifyPageActivity()))

subscriptions.push(
Expand Down Expand Up @@ -108,6 +111,7 @@ export function trackPageActivities(
observable,
stop: () => {
subscriptions.forEach((s) => s.unsubscribe())
DOMMutationCollection.stop()
},
}
}
Expand Down

0 comments on commit 0841bdd

Please sign in to comment.