Make all times be in ms.

This commit is contained in:
picklesrus 2020-07-17 14:44:36 -04:00
parent 7eeb63cb3f
commit fac3ccad57
2 changed files with 14 additions and 12 deletions

View file

@ -37,14 +37,16 @@ class Navigation extends React.Component {
componentDidMount () { componentDidMount () {
if (this.props.user) { if (this.props.user) {
// Setup polling for messages to start in 2 minutes. // Setup polling for messages to start in 2 minutes.
setTimeout(this.pollForMessages.bind(this, 2), 2 * 60 * 1000); const twoMinInMs = 2 * 60 * 1000;
setTimeout(this.pollForMessages.bind(this, twoMinInMs), twoMinInMs);
} }
} }
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
if (prevProps.user !== this.props.user) { if (prevProps.user !== this.props.user) {
this.props.handleCloseAccountNav(); this.props.handleCloseAccountNav();
if (this.props.user) { if (this.props.user) {
setTimeout(this.pollForMessages.bind(this, 2), 2 * 60 * 1000); const twoMinInMs = 2 * 60 * 1000;
setTimeout(this.pollForMessages.bind(this, twoMinInMs), twoMinInMs);
} else { } else {
// clear message count check, and set to default id. // clear message count check, and set to default id.
clearTimeout(this.state.messageCountIntervalId); clearTimeout(this.state.messageCountIntervalId);
@ -70,16 +72,16 @@ class Navigation extends React.Component {
return `/users/${this.props.user.username}/`; return `/users/${this.props.user.username}/`;
} }
pollForMessages (minutes) { pollForMessages (ms) {
this.props.getMessageCount(this.props.user.username); this.props.getMessageCount(this.props.user.username);
// We only poll if it has been less than 30 minutes. // We only poll if it has been less than 30 minutes.
// Chances of someone actively using the page for that long without // Chances of someone actively using the page for that long without
// a navigation is low. // a navigation is low.
if (minutes < 32) { if (ms < 32 * 60 * 1000) { // 32 minutes
const nextFetch = minutes * 2; const nextFetch = ms * 2; // exponentially back off next fetch time.
const timeoutId = setTimeout(() => { const timeoutId = setTimeout(() => {
this.pollForMessages(nextFetch); this.pollForMessages(nextFetch);
}, nextFetch * 60000); }, nextFetch);
this.setState({ // eslint-disable-line react/no-did-mount-set-state this.setState({ // eslint-disable-line react/no-did-mount-set-state
messageCountIntervalId: timeoutId messageCountIntervalId: timeoutId
}); });

View file

@ -167,16 +167,16 @@ describe('Navigation', () => {
// has already called pollForMessages. // has already called pollForMessages.
jest.clearAllTimers(); jest.clearAllTimers();
jest.clearAllMocks(); jest.clearAllMocks();
let twoMinInMs = 2 * 60 * 1000; // 2 minutes in ms.
navInstance.pollForMessages(2); navInstance.pollForMessages(twoMinInMs);
// Check that we set the timeout to backoff exponentially // Check that we set the timeout to backoff exponentially
let minutes = 2 * 60 * 1000;
for (let count = 1; count < 5; ++count) { for (let count = 1; count < 5; ++count) {
jest.advanceTimersByTime(minutes + 1); jest.advanceTimersByTime(twoMinInMs + 1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), minutes * 2); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), twoMinInMs * 2);
expect(props.getMessageCount).toHaveBeenCalledTimes(count); expect(props.getMessageCount).toHaveBeenCalledTimes(count);
minutes = minutes * 2; twoMinInMs = twoMinInMs * 2;
} }
// Exhaust all timers (there shouldn't be any left) // Exhaust all timers (there shouldn't be any left)