-
Notifications
You must be signed in to change notification settings - Fork 141
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-1449] workaround for Firefox memory leak when using Zone.js #1860
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0b2564e
♻️ [RUMF-1449] rename EventEmitter to EventTarget
BenoitZugmeyer b059179
🚚 [RUMF-1449] move addEventListener functions to a dedicated module
BenoitZugmeyer 669b1ec
🐛 [RUMF-1449] implement a workaround for Firefox memory leak
BenoitZugmeyer 25b3365
👌🚚 move stubZoneJs in its own module
BenoitZugmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { stubZoneJs } from '../../test/specHelper' | ||
import { noop } from '../tools/utils' | ||
|
||
import { addEventListener, DOM_EVENT } from './addEventListener' | ||
|
||
describe('addEventListener', () => { | ||
describe('Zone.js support', () => { | ||
let zoneJsStub: ReturnType<typeof stubZoneJs> | ||
|
||
beforeEach(() => { | ||
zoneJsStub = stubZoneJs() | ||
}) | ||
|
||
afterEach(() => { | ||
zoneJsStub.restore() | ||
}) | ||
|
||
it('uses the original addEventListener method instead of the method patched by Zone.js', () => { | ||
const zoneJsPatchedAddEventListener = jasmine.createSpy() | ||
const eventTarget = document.createElement('div') | ||
zoneJsStub.replaceProperty(eventTarget, 'addEventListener', zoneJsPatchedAddEventListener) | ||
|
||
addEventListener(eventTarget, DOM_EVENT.CLICK, noop) | ||
expect(zoneJsPatchedAddEventListener).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('uses the original removeEventListener method instead of the method patched by Zone.js', () => { | ||
const zoneJsPatchedRemoveEventListener = jasmine.createSpy() | ||
const eventTarget = document.createElement('div') | ||
zoneJsStub.replaceProperty(eventTarget, 'removeEventListener', zoneJsPatchedRemoveEventListener) | ||
|
||
const { stop } = addEventListener(eventTarget, DOM_EVENT.CLICK, noop) | ||
stop() | ||
expect(zoneJsPatchedRemoveEventListener).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { monitor } from '../tools/monitor' | ||
import { getZoneJsOriginalValue } from '../tools/getZoneJsOriginalValue' | ||
|
||
export const enum DOM_EVENT { | ||
BEFORE_UNLOAD = 'beforeunload', | ||
CLICK = 'click', | ||
DBL_CLICK = 'dblclick', | ||
KEY_DOWN = 'keydown', | ||
LOAD = 'load', | ||
POP_STATE = 'popstate', | ||
SCROLL = 'scroll', | ||
TOUCH_START = 'touchstart', | ||
TOUCH_END = 'touchend', | ||
TOUCH_MOVE = 'touchmove', | ||
VISIBILITY_CHANGE = 'visibilitychange', | ||
DOM_CONTENT_LOADED = 'DOMContentLoaded', | ||
POINTER_DOWN = 'pointerdown', | ||
POINTER_UP = 'pointerup', | ||
POINTER_CANCEL = 'pointercancel', | ||
HASH_CHANGE = 'hashchange', | ||
PAGE_HIDE = 'pagehide', | ||
MOUSE_DOWN = 'mousedown', | ||
MOUSE_UP = 'mouseup', | ||
MOUSE_MOVE = 'mousemove', | ||
FOCUS = 'focus', | ||
BLUR = 'blur', | ||
CONTEXT_MENU = 'contextmenu', | ||
RESIZE = 'resize', | ||
CHANGE = 'change', | ||
INPUT = 'input', | ||
PLAY = 'play', | ||
PAUSE = 'pause', | ||
SECURITY_POLICY_VIOLATION = 'securitypolicyviolation', | ||
SELECTION_CHANGE = 'selectionchange', | ||
} | ||
|
||
interface AddEventListenerOptions { | ||
once?: boolean | ||
capture?: boolean | ||
passive?: boolean | ||
} | ||
|
||
/** | ||
* Add an event listener to an event target object (Window, Element, mock object...). This provides | ||
* a few conveniences compared to using `element.addEventListener` directly: | ||
* | ||
* * supports IE11 by: using an option object only if needed and emulating the `once` option | ||
* | ||
* * wraps the listener with a `monitor` function | ||
* | ||
* * returns a `stop` function to remove the listener | ||
*/ | ||
export function addEventListener<E extends Event>( | ||
eventTarget: EventTarget, | ||
event: DOM_EVENT, | ||
listener: (event: E) => void, | ||
options?: AddEventListenerOptions | ||
) { | ||
return addEventListeners(eventTarget, [event], listener, options) | ||
} | ||
|
||
/** | ||
* Add event listeners to an event target object (Window, Element, mock object...). This provides | ||
* a few conveniences compared to using `element.addEventListener` directly: | ||
* | ||
* * supports IE11 by: using an option object only if needed and emulating the `once` option | ||
* | ||
* * wraps the listener with a `monitor` function | ||
* | ||
* * returns a `stop` function to remove the listener | ||
* | ||
* * with `once: true`, the listener will be called at most once, even if different events are listened | ||
*/ | ||
export function addEventListeners<E extends Event>( | ||
eventTarget: EventTarget, | ||
events: DOM_EVENT[], | ||
listener: (event: E) => void, | ||
{ once, capture, passive }: AddEventListenerOptions = {} | ||
) { | ||
const wrappedListener = monitor( | ||
once | ||
? (event: Event) => { | ||
stop() | ||
listener(event as E) | ||
} | ||
: (listener as (event: Event) => void) | ||
) | ||
|
||
const options = passive ? { capture, passive } : capture | ||
|
||
const add = getZoneJsOriginalValue(eventTarget, 'addEventListener') | ||
events.forEach((event) => add.call(eventTarget, event, wrappedListener, options)) | ||
|
||
function stop() { | ||
const remove = getZoneJsOriginalValue(eventTarget, 'removeEventListener') | ||
events.forEach((event) => remove.call(eventTarget, event, wrappedListener, options)) | ||
} | ||
|
||
return { | ||
stop, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { stubZoneJs } from '../../test/specHelper' | ||
|
||
import { getZoneJsOriginalValue } from './getZoneJsOriginalValue' | ||
import { noop } from './utils' | ||
|
||
describe('getZoneJsOriginalValue', () => { | ||
let zoneJsStub: ReturnType<typeof stubZoneJs> | undefined | ||
|
||
function originalValue() { | ||
// just a function that does nothing but different than 'noop' as we'll want to differentiate | ||
// them. | ||
} | ||
const object = { | ||
name: originalValue, | ||
} | ||
|
||
afterEach(() => { | ||
zoneJsStub?.restore() | ||
}) | ||
|
||
it('returns the original value directly if Zone is not not defined', () => { | ||
expect(getZoneJsOriginalValue(object, 'name')).toBe(originalValue) | ||
}) | ||
|
||
it("returns undefined if Zone is defined but didn't patch that method", () => { | ||
zoneJsStub = stubZoneJs() | ||
expect(getZoneJsOriginalValue(object, 'name')).toBe(originalValue) | ||
}) | ||
|
||
it('returns the original value if Zone did patch the method', () => { | ||
zoneJsStub = stubZoneJs() | ||
zoneJsStub.replaceProperty(object, 'name', noop) | ||
expect(getZoneJsOriginalValue(object, 'name')).toBe(originalValue) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export interface BrowserWindowWithZoneJs extends Window { | ||
Zone?: { | ||
__symbol__: (name: string) => string | ||
} | ||
} | ||
|
||
/** | ||
* Gets the original value for a DOM API that was potentially patched by Zone.js. | ||
* | ||
* Zone.js[1] is a library that patches a bunch of JS and DOM APIs. It usually stores the original | ||
* value of the patched functions/constructors/methods in a hidden property prefixed by | ||
* __zone_symbol__. | ||
* | ||
* In multiple occasions, we observed that Zone.js is the culprit of important issues leading to | ||
* browser resource exhaustion (memory leak, high CPU usage). This method is used as a workaround to | ||
* use the original DOM API instead of the one patched by Zone.js. | ||
* | ||
* [1]: https://github.com/angular/angular/tree/main/packages/zone.js | ||
*/ | ||
export function getZoneJsOriginalValue<Target, Name extends keyof Target & string>( | ||
target: Target, | ||
name: Name | ||
): Target[Name] { | ||
const browserWindow = window as BrowserWindowWithZoneJs | ||
let original: Target[Name] | undefined | ||
if (browserWindow.Zone) { | ||
original = (target as any)[browserWindow.Zone.__symbol__(name)] | ||
} | ||
if (!original) { | ||
original = target[name] | ||
} | ||
return original | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ question: Are we sure it works for all version of Angular/zone.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is something we used before:
browser-sdk/packages/rum-core/src/browser/domMutationObservable.ts
Line 47 in e4f2bad