Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [RUMF-469] fix angular compatibility #376

Merged
merged 3 commits into from
Apr 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions packages/rum/src/domMutationCollection.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
import { monitor } from '@datadog/browser-core'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'

type MutationObserverConstructor = new (callback: MutationCallback) => MutationObserver

interface BrowserWindow extends Window {
MutationObserver?: MutationObserver
MutationObserver?: MutationObserverConstructor
Zone?: {
__symbol__(name: string): string
}
}

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
// 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
// page to freeze (even if the callback is completely empty).
//
// To work around this issue, we are using the Zone __symbol__ API to get the original, unpatched
// MutationObserver constructor.
//
// [1] https://github.com/angular/angular/issues/26948
// [2] https://github.com/angular/angular/issues/31712
if (browserWindow.Zone) {
const symbol = browserWindow.Zone.__symbol__('MutationObserver')
constructor = browserWindow[symbol as any] as any
}

if (!constructor) {
constructor = browserWindow.MutationObserver
}

return constructor
}

export function startDOMMutationCollection(lifeCycle: LifeCycle) {
let observer: MutationObserver | undefined
if ((window as BrowserWindow).MutationObserver) {
const MutationObserver = getMutationObserverConstructor()
if (MutationObserver) {
observer = new MutationObserver(
monitor(() => {
lifeCycle.notify(LifeCycleEventType.DOM_MUTATED)
Expand Down