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

Tidy up Event listener when unmounting #1178

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions src/components/framework/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import { twoColumnBreakpoint, controlsHiddenWidth} from "../../util/globals";
class Monitor extends React.Component {
constructor(props) {
super(props);
this.throttleHandleByResize =
_throttle(this.handleResizeByDispatching.bind(this), 500, {
leading: true,
trailing: true
});
}
static propTypes = {
dispatch: PropTypes.func.isRequired
Expand All @@ -24,10 +29,7 @@ class Monitor extends React.Component {
"resize",
/* lodash throttle invokes resize event at most twice per second
to let redraws catch up. Could also use debounce for 'wait until resize stops' */
_throttle(this.handleResizeByDispatching.bind(this), 500, {
leading: true,
trailing: true
})
this.throttleHandleByResize
);

/* Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event.
Expand All @@ -36,6 +38,11 @@ class Monitor extends React.Component {
window.addEventListener('popstate', this.onURLChanged);
// this.onURLChanged();
}
componentWillUnmount() {
window.removeEventListener("resize", this.throttleHandleByResize); // need to actually remove the _throttle
this.throttleHandleByResize.cancel;
window.removeEventListener('popstate', this.onURLChanged);
}

onURLChanged = () => this.props.dispatch(changePage());

Expand Down