From 02bf3cf5d7e2cb682fa1d614414175aafe2047f0 Mon Sep 17 00:00:00 2001 From: Jam <42326027+benjammin4dayz@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:33:40 -0400 Subject: [PATCH] Set ready state after Neutralino.init --- .../resources/src/NeutralinoProvider.tsx | 30 +++++++++---------- react-src/src/NeutralinoProvider.jsx | 30 +++++++++---------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/.ts-convert/resources/src/NeutralinoProvider.tsx b/.ts-convert/resources/src/NeutralinoProvider.tsx index 8725aa0..583f978 100644 --- a/.ts-convert/resources/src/NeutralinoProvider.tsx +++ b/.ts-convert/resources/src/NeutralinoProvider.tsx @@ -51,27 +51,25 @@ export const NeutralinoProvider = ({ useEffect(() => { try { Neutralino.init(); - if (errorTimeout.current) { - clearTimeout(errorTimeout.current); - errorTimeout.current = null; - } + // + // If the state is reloaded while Neutralino is already initialized, + // the "ready" event won't fire. This should always be accurate because + // Neutralino's init function either passes or throws an error. + // + setReady(true); } catch (e) { - if (!errorTimeout.current) { - errorTimeout.current = setTimeout(() => { - setError(true); - errorTimeout.current = null; - }, failAfterMs); - } + errorTimeout.current = setTimeout(() => { + setError(true); + errorTimeout.current = null; + }, failAfterMs); console.warn('Neutralino failed to initialize', e); } - const onReady = () => { - setReady(true); - }; - void Neutralino.events.on('ready', onReady); - return () => { - void Neutralino.events.off('ready', onReady); + if (errorTimeout.current) { + clearTimeout(errorTimeout.current); + errorTimeout.current = null; + } }; }, [failAfterMs]); diff --git a/react-src/src/NeutralinoProvider.jsx b/react-src/src/NeutralinoProvider.jsx index d40ec4f..055ac0f 100644 --- a/react-src/src/NeutralinoProvider.jsx +++ b/react-src/src/NeutralinoProvider.jsx @@ -35,27 +35,25 @@ export const NeutralinoProvider = ({ useEffect(() => { try { Neutralino.init(); - if (errorTimeout.current) { - clearTimeout(errorTimeout.current); - errorTimeout.current = null; - } + // + // If the state is reloaded while Neutralino is already initialized, + // the "ready" event won't fire. This should always be accurate because + // Neutralino's init function either passes or throws an error. + // + setReady(true); } catch (e) { - if (!errorTimeout.current) { - errorTimeout.current = setTimeout(() => { - setError(true); - errorTimeout.current = null; - }, failAfterMs); - } + errorTimeout.current = setTimeout(() => { + setError(true); + errorTimeout.current = null; + }, failAfterMs); console.warn('Neutralino failed to initialize', e); } - const onReady = () => { - setReady(true); - }; - void Neutralino.events.on('ready', onReady); - return () => { - void Neutralino.events.off('ready', onReady); + if (errorTimeout.current) { + clearTimeout(errorTimeout.current); + errorTimeout.current = null; + } }; }, [failAfterMs]);