Skip to content

Commit

Permalink
perf(Visibility): use RAF (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and Alexander Fedyashov committed Sep 11, 2017
1 parent 1364976 commit 64a4f31
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
13 changes: 11 additions & 2 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ export default class Visibility extends Component {

const { context, fireOnMount } = this.props

context.addEventListener('scroll', this.handleUpdate)
context.addEventListener('scroll', this.handleScroll)
if (fireOnMount) this.handleUpdate()
}

componentWillUnmount() {
if (!isBrowser) return

const { context } = this.props
context.removeEventListener('scroll', this.handleUpdate)
context.removeEventListener('scroll', this.handleScroll)
}

execute = (callback, name) => {
Expand Down Expand Up @@ -292,9 +292,18 @@ export default class Visibility extends Component {
})
}

handleScroll = () => {
if (this.ticking) return

this.ticking = true
requestAnimationFrame(this.handleUpdate)
}

handleRef = c => (this.ref = c)

handleUpdate = () => {
this.ticking = false

const { offset } = this.props
const { bottom, height, top, width } = this.ref.getBoundingClientRect()
const [topOffset, bottomOffset] = normalizeOffset(offset)
Expand Down
50 changes: 33 additions & 17 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,23 @@ const expectations = [{
describe('Visibility', () => {
common.isConformant(Visibility)

beforeEach(() => {
wrapper = undefined
let requestAnimationFrame

before(() => {
requestAnimationFrame = window.requestAnimationFrame
window.requestAnimationFrame = fn => fn()
})

afterEach(() => {
if (wrapper && wrapper.unmount) wrapper.unmount()
after(() => {
window.requestAnimationFrame = requestAnimationFrame
})

it('should use window as default scroll context', () => {
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} />)
window.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
beforeEach(() => {
wrapper = undefined
})

it('should set a scroll context', () => {
const div = document.createElement('div')
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} context={div} />)
window.dispatchEvent(new Event('scroll'))
onUpdate.should.not.have.been.called()
div.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
afterEach(() => {
if (wrapper && wrapper.unmount) wrapper.unmount()
})

describe('calculations', () => {
Expand Down Expand Up @@ -186,6 +180,28 @@ describe('Visibility', () => {
})
})

describe('context', () => {
it('should use window as default scroll context', () => {
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} />)

window.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
})

it('should set a scroll context', () => {
const div = document.createElement('div')
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} context={div} />)

window.dispatchEvent(new Event('scroll'))
onUpdate.should.not.have.been.called()

div.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
})
})

describe('fireOnMount', () => {
it('fires callbacks after mount', () => {
const onUpdate = sandbox.spy()
Expand Down

0 comments on commit 64a4f31

Please sign in to comment.