Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
fix: timer component, fixes apache#10849, closes apache#11002
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud committed Sep 23, 2020
1 parent 345fe07 commit efa13b2
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions superset-frontend/src/components/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import Label from 'src/components/Label';

import { now, fDuration } from '../modules/dates';
Expand All @@ -35,42 +35,31 @@ export default function Timer({
status = 'success',
}: TimerProps) {
const [clockStr, setClockStr] = useState('');
const [timer, setTimer] = useState<NodeJS.Timeout>();
const timer = useRef<NodeJS.Timeout>();

const stopTimer = () => {
if (timer) {
clearInterval(timer);
setTimer(undefined);
if (timer.current) {
clearInterval(timer.current);
timer.current = undefined;
}
};

const stopwatch = () => {
if (startTime) {
const endDttm = endTime || now();
if (startTime < endDttm) {
setClockStr(fDuration(startTime, endDttm));
}
if (!isRunning) {
stopTimer();
}
}
};

const startTimer = () => {
setTimer(setInterval(stopwatch, 30));
};

useEffect(() => {
if (isRunning) {
startTimer();
timer.current = setInterval(() => {
if (startTime) {
const endDttm = endTime || now();
if (startTime < endDttm) {
setClockStr(fDuration(startTime, endDttm));
}
if (!isRunning) {
stopTimer();
}
}
}, 30);
}
}, [isRunning]);

useEffect(() => {
return () => {
stopTimer();
};
});
return stopTimer;
}, [endTime, isRunning, startTime]);

return (
<Label id="timer" bsStyle={status}>
Expand Down

0 comments on commit efa13b2

Please sign in to comment.