Skip to content
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

perf(Visibility): use RAF #2019

Merged
merged 2 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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