-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathevent-listener.ts
More file actions
42 lines (37 loc) · 1009 Bytes
/
Copy pathevent-listener.ts
File metadata and controls
42 lines (37 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
let passiveSupported: boolean = false
let eventListeners: EventDescription[] = []
interface EventDescription {
eventName: string
callback: EventListener
}
/* istanbul ignore next */
try {
const options = Object.defineProperty({}, `passive`, {
get() {
passiveSupported = true
},
})
window.addEventListener(`test`, options, options)
window.removeEventListener(`test`, options, options)
} catch (err) {
passiveSupported = false
}
export function addListener(eventName: string, callback: EventListener) {
eventListeners.push({
eventName,
callback,
})
window.addEventListener(
eventName,
callback,
/* istanbul ignore next */
passiveSupported ? { passive: true } : false
)
}
export function removeAll() {
eventListeners.forEach(config => {
window.removeEventListener(config.eventName, config.callback)
})
eventListeners = []
}