-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚 [RUMF-1449] move addEventListener functions to a dedicated module
- Loading branch information
1 parent
f7e38a4
commit ca6b03d
Showing
7 changed files
with
106 additions
and
100 deletions.
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,95 @@ | ||
import { monitor } from '@datadog/browser-core' | ||
|
||
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 | ||
events.forEach((event) => eventTarget.addEventListener(event, wrappedListener, options)) | ||
const stop = () => events.forEach((event) => eventTarget.removeEventListener(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