nytai commented on a change in pull request #10455:
URL: 
https://github.com/apache/incubator-superset/pull/10455#discussion_r492997908



##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+}
+
+export default function Timer({
+  endTime,
+  isRunning,
+  startTime,
+  status = 'success',
+}: TimerProps) {
+  const [clockStr, setClockStr] = useState('');
+  const [timer, setTimer] = useState<NodeJS.Timeout>();
+
+  const stopTimer = () => {
+    if (timer) {
+      clearInterval(timer);
+      setTimer(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();
+    }
+  }, [isRunning]);
+
+  useEffect(() => {
+    return () => {
+      stopTimer();
+    };
+  });

Review comment:
       From the react docs
   > When exactly does React clean up an effect? React performs the cleanup 
when the component unmounts. However, as we learned earlier, effects run for 
every render and not just once. This is why React also cleans up effects from 
the previous render before running the effects next time. We’ll discuss why 
this helps avoid bugs and how to opt out of this behavior in case it creates 
performance issues later below.
   
   Looks like the issue here is that this is running on every render, hence it 
appears to call stopTimer after a single update. A simple way to handle this is 
to add a dependency on the prop that signals the query has started/stopped 
running, namely `endTime`.
   
   Suggested change
   ```js
     useEffect(() => {
       return () => {
         stopTimer();
       };
     }, [endTime]);
   ```
   
   A test case to ensure this doesn't break in future refactors would great. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to