From 8e26e27b8d017dca5ccf426b7d4a1b756aff7d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Gran=C3=A1t?= Date: Mon, 11 Sep 2023 14:45:17 +0200 Subject: [PATCH 1/2] fix: race condition error in react integration --- packages/react/src/TolgeeProvider.tsx | 20 +++++++++++--------- packages/react/src/useTranslateInternal.ts | 6 ++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/react/src/TolgeeProvider.tsx b/packages/react/src/TolgeeProvider.tsx index 69461f8abb..cec057efce 100644 --- a/packages/react/src/TolgeeProvider.tsx +++ b/packages/react/src/TolgeeProvider.tsx @@ -34,15 +34,17 @@ export const TolgeeProvider: React.FC = ({ const [loading, setLoading] = useState(!tolgee.isLoaded()); useEffect(() => { - tolgee - .run() - .catch((e) => { - // eslint-disable-next-line no-console - console.error(e); - }) - .finally(() => { - setLoading(false); - }); + if (!tolgee.isRunning()) { + tolgee + .run() + .catch((e) => { + // eslint-disable-next-line no-console + console.error(e); + }) + .finally(() => { + setLoading(false); + }); + } }, [tolgee]); const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options }; diff --git a/packages/react/src/useTranslateInternal.ts b/packages/react/src/useTranslateInternal.ts index 3c7433844c..a3070beb61 100644 --- a/packages/react/src/useTranslateInternal.ts +++ b/packages/react/src/useTranslateInternal.ts @@ -42,9 +42,7 @@ export const useTranslateInternal = ( useEffect(() => { const subscription = tolgee.onNsUpdate(rerender); subscriptionRef.current = subscription; - if (!isLoaded) { - subscription.subscribeNs(namespaces); - } + subscription.subscribeNs(namespaces); subscriptionQueue.current.forEach((ns) => { subscription!.subscribeNs(ns); }); @@ -52,7 +50,7 @@ export const useTranslateInternal = ( return () => { subscription.unsubscribe(); }; - }, [isLoaded, namespacesJoined, tolgee]); + }, [namespacesJoined, tolgee]); useEffect(() => { tolgee.addActiveNs(namespaces); From a415c0706c0fb5f485f64bc3f023d80adcc43e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Gran=C3=A1t?= Date: Mon, 11 Sep 2023 15:12:14 +0200 Subject: [PATCH 2/2] fix: race condition error in react integration --- packages/core/src/Controller/Controller.ts | 7 ++++--- packages/react/src/TolgeeProvider.tsx | 20 +++++++++----------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/core/src/Controller/Controller.ts b/packages/core/src/Controller/Controller.ts index 640d6c6c47..8f23c98e4c 100644 --- a/packages/core/src/Controller/Controller.ts +++ b/packages/core/src/Controller/Controller.ts @@ -63,6 +63,8 @@ export function Controller({ options }: StateServiceProps) { init(options); } + let runPromise: Promise | undefined; + events.onUpdate.listen(() => { if (state.isRunning()) { pluginService.retranslate(); @@ -283,14 +285,13 @@ export function Controller({ options }: StateServiceProps) { }, run() { - let result: Promise | undefined = undefined; checkCorrectConfiguration(); if (!state.isRunning()) { state.setRunning(true); pluginService.run(); - result = loadInitial(); + runPromise = loadInitial(); } - return Promise.resolve(result); + return Promise.resolve(runPromise); }, stop() { diff --git a/packages/react/src/TolgeeProvider.tsx b/packages/react/src/TolgeeProvider.tsx index cec057efce..69461f8abb 100644 --- a/packages/react/src/TolgeeProvider.tsx +++ b/packages/react/src/TolgeeProvider.tsx @@ -34,17 +34,15 @@ export const TolgeeProvider: React.FC = ({ const [loading, setLoading] = useState(!tolgee.isLoaded()); useEffect(() => { - if (!tolgee.isRunning()) { - tolgee - .run() - .catch((e) => { - // eslint-disable-next-line no-console - console.error(e); - }) - .finally(() => { - setLoading(false); - }); - } + tolgee + .run() + .catch((e) => { + // eslint-disable-next-line no-console + console.error(e); + }) + .finally(() => { + setLoading(false); + }); }, [tolgee]); const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };