Skip to content

Commit

Permalink
Set ready state after Neutralino.init
Browse files Browse the repository at this point in the history
  • Loading branch information
benjammin4dayz committed Oct 28, 2024
1 parent dec7df0 commit 02bf3cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
30 changes: 14 additions & 16 deletions .ts-convert/resources/src/NeutralinoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
30 changes: 14 additions & 16 deletions react-src/src/NeutralinoProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down

0 comments on commit 02bf3cf

Please sign in to comment.