Skip to content

Commit

Permalink
feat: add supportPassiveEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 11, 2018
1 parent a14b321 commit 60bd73f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as isUndefined } from './isUndefined'
export { default as noop } from './noop'
export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as supportPassiveEventListener } from './supportPassiveEventListener'
21 changes: 21 additions & 0 deletions src/supportPassiveEventListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let isSupportPassiveEventListener: boolean | undefined

/**
* 检测是否支持 passive 模式的事件监听。
*
* @returns 是(true)或否(false)
*/
export default function supportPassiveEventListener(): boolean {
if (isSupportPassiveEventListener === undefined) {
isSupportPassiveEventListener = false
try {
const options = Object.defineProperty({}, 'passive', {
get() {
isSupportPassiveEventListener = true
}
})
window.addEventListener('test', null, options)
} catch (err) {}
}
return isSupportPassiveEventListener
}
6 changes: 6 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,9 @@ describe('forOwn', () => {
expect(arr).toEqual([])
})
})

describe('supportPassiveEventListener', () => {
test('不支持', () => {
expect(vtils.supportPassiveEventListener()).toBeFalsy()
})
})

0 comments on commit 60bd73f

Please sign in to comment.