Skip to content

Commit

Permalink
fix(Visibility): cancel RAF on component unmount (Semantic-Org#2635)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and Brandon Lawrence committed Mar 14, 2018
1 parent 3d26c86 commit 6b20157
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export default class Visibility extends Component {
const { context } = this.props

this.unattachHandlers(context)
if (this.frameId) cancelAnimationFrame(this.frameId)
}

attachHandlers(context) {
Expand Down Expand Up @@ -284,7 +285,7 @@ export default class Visibility extends Component {
if (this.ticking) return

this.ticking = true
requestAnimationFrame(this.update)
this.frameId = requestAnimationFrame(this.update)
}

update = () => {
Expand Down
17 changes: 16 additions & 1 deletion test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ describe('Visibility', () => {

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

after(() => {
Expand Down Expand Up @@ -334,6 +337,18 @@ describe('Visibility', () => {
})
})

describe('componentWillUnmount', () => {
it('will cancel requestAnimationFrame', () => {
const cancelAnimationFrame = sandbox.spy(window, 'cancelAnimationFrame')
wrapperMount(<Visibility />)

mockScroll(0, 0)
wrapper.unmount()

cancelAnimationFrame.should.have.been.calledOnce()
})
})

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

0 comments on commit 6b20157

Please sign in to comment.