Skip to content

Commit

Permalink
app: clear timer interval when unmounting App container
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed May 2, 2016
1 parent ca7d044 commit 8c9a82b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/actions/TickerActionCreators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { get } from './RequestActionCreators';

import { SET_TIMER, OFFSET } from '../constants/actionTypes/time';
import { timerSelector } from '../selectors/timeSelectors';

export function syncTimestamps(clientTimeBefore, serverTime) {
const clientTimeAfter = Date.now();
Expand Down Expand Up @@ -30,3 +31,14 @@ export function createTimer() {
return callbacks;
};
}

export function stopTimer() {
return (dispatch, getState) => {
const timer = timerSelector(getState());
clearInterval(timer);
dispatch({
type: SET_TIMER,
payload: null
});
};
}
11 changes: 9 additions & 2 deletions src/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

import { inputMessage } from '../actions/ChatActionCreators';
import { closeAll } from '../actions/OverlayActionCreators';
import { createTimer } from '../actions/TickerActionCreators';
import { createTimer, stopTimer } from '../actions/TickerActionCreators';

import { currentUserSelector } from '../selectors/userSelectors';
import { settingsSelector, muiThemeSelector } from '../selectors/settingSelectors';
Expand All @@ -22,6 +22,7 @@ const mapStateToProps = createStructuredSelector({
function mapDispatchToProps(dispatch) {
return bindActionCreators({
createTimer,
stopTimer,
sendChatMessage: inputMessage,
onCloseOverlay: closeAll
}, dispatch);
Expand All @@ -31,7 +32,8 @@ function mapDispatchToProps(dispatch) {
export default class AppContainer extends Component {
static propTypes = {
muiTheme: PropTypes.object,
createTimer: PropTypes.func.isRequired
createTimer: PropTypes.func.isRequired,
stopTimer: PropTypes.func.isRequired
};

static childContextTypes = {
Expand All @@ -50,6 +52,11 @@ export default class AppContainer extends Component {
this.timerCallbacks = this.props.createTimer();
}

componentWillUnmount() {
this.timerCallbacks = [];
this.props.stopTimer();
}

render() {
return (
<MuiThemeProvider muiTheme={this.props.muiTheme}>
Expand Down
1 change: 1 addition & 0 deletions src/selectors/timeSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const baseSelector = state => state.time;

const timeSelector = () => Date.now();
const offsetSelector = createSelector(baseSelector, time => time.offset);
export const timerSelector = createSelector(baseSelector, time => time.timer);

export const currentTimeSelector = createSelector(
timeSelector,
Expand Down

0 comments on commit 8c9a82b

Please sign in to comment.