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

Cleaned up intersection observer when the component unmounts #1583

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
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ describe('ViewerApp', () => {
expect(window.IntersectionObserver).toHaveBeenCalledWith(thisValue.onIntersectionChange, {
root: null,
rootMargin: '0px',
threshhold: 0
threshold: 0
})
expect(thisValue.observer.observe).toHaveBeenCalledWith(
FocusUtil.getVisuallyFocussedModel().getDomEl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export default class ViewerApp extends React.Component {
componentWillUnmount() {
this.unRegisterStores()
document.removeEventListener('visibilitychange', this.onVisibilityChange)

// Removing the intersection observer.
this.stopObservingForIntersectionChanges()
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -387,6 +390,10 @@ export default class ViewerApp extends React.Component {
this.stopObservingForIntersectionChanges()
const focusState = this.state.focusState

// Only creates an Intersection Observer object when the 'visualFocusTarget'
// property of 'focusState' is set - this is when all of the elements of the
// page are faded except for the visualFocusTarget (for example, when answering
// a practice question)
if (!focusState.visualFocusTarget) {
return
}
Expand All @@ -401,10 +408,12 @@ export default class ViewerApp extends React.Component {
return
}

// If an intersection observer is created below, we're 100% sure that
// 'focusState.visualFocusTarget' is set.
this.observer = new IntersectionObserver(this.onIntersectionChange, {
root: null,
rootMargin: '0px',
threshhold: 0
threshold: 0
})

this.observer.observe(el)
Expand Down