Manav Rathi hai 1 ano
pai
achega
816f8cecd6
Modificáronse 1 ficheiros con 20 adicións e 22 borrados
  1. 20 22
      web/apps/cast/src/pages/index.tsx

+ 20 - 22
web/apps/cast/src/pages/index.tsx

@@ -11,30 +11,27 @@ export default function Index() {
     const [publicKeyB64, setPublicKeyB64] = useState<string | undefined>();
     const [publicKeyB64, setPublicKeyB64] = useState<string | undefined>();
     const [privateKeyB64, setPrivateKeyB64] = useState<string | undefined>();
     const [privateKeyB64, setPrivateKeyB64] = useState<string | undefined>();
     const [pairingCode, setPairingCode] = useState<string | undefined>();
     const [pairingCode, setPairingCode] = useState<string | undefined>();
-    // TODO: This needs to change, since there is an interim period when the
-    // code becomes invalid.
+
+    // Keep a boolean flag to ensure that Cast Receiver starts only once even if
+    // pairing codes change.
     const [haveInitializedCast, setHaveInitializedCast] = useState(false);
     const [haveInitializedCast, setHaveInitializedCast] = useState(false);
 
 
     const router = useRouter();
     const router = useRouter();
 
 
     useEffect(() => {
     useEffect(() => {
-        init();
-    }, []);
-
-    const init = () => {
-        register().then((r) => {
-            setPublicKeyB64(r.publicKeyB64);
-            setPrivateKeyB64(r.privateKeyB64);
-            setPairingCode(r.pairingCode);
-        });
-    };
-
-    useEffect(() => {
-        if (pairingCode && !haveInitializedCast) {
-            castReceiverLoadingIfNeeded().then((cast) => {
-                setHaveInitializedCast(true);
-                advertiseCode(cast, () => pairingCode);
+        if (!pairingCode) {
+            register().then((r) => {
+                setPublicKeyB64(r.publicKeyB64);
+                setPrivateKeyB64(r.privateKeyB64);
+                setPairingCode(r.pairingCode);
             });
             });
+        } else {
+            if (!haveInitializedCast) {
+                castReceiverLoadingIfNeeded().then((cast) => {
+                    setHaveInitializedCast(true);
+                    advertiseCode(cast, () => pairingCode);
+                });
+            }
         }
         }
     }, [pairingCode]);
     }, [pairingCode]);
 
 
@@ -58,11 +55,12 @@ export default function Index() {
             storeCastData(data);
             storeCastData(data);
             await router.push("/slideshow");
             await router.push("/slideshow");
         } catch (e) {
         } catch (e) {
-            // Code has become invalid
-            log.error("Failed to get cast data", e);
-            // Start again from the beginning.
+            // The pairing code becomes invalid after an hour, which will cause
+            // `getCastData` to fail. There might be other reasons this might
+            // fail too, but in all such cases, it is a reasonable idea to start
+            // again from the beginning.
+            log.warn("Failed to get cast data", e);
             setPairingCode(undefined);
             setPairingCode(undefined);
-            init();
         }
         }
     };
     };