From 35dc7191f335b569bc672eac4838274cf1124b0a Mon Sep 17 00:00:00 2001 From: Dmitry Ivakhnenko Date: Sat, 18 Feb 2023 12:17:58 +0300 Subject: [PATCH] fix: initializes yoga without race conditions (#409) --- src/yoga/index.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/yoga/index.ts b/src/yoga/index.ts index 1dc3bf91..89c55b0a 100644 --- a/src/yoga/index.ts +++ b/src/yoga/index.ts @@ -4,10 +4,22 @@ export function init(yoga: typeof Yoga) { Yoga = yoga } +let initializationPromise = null + export default async function getYoga(): Promise { - if (!Yoga) { - const mod = await import('@yoga') - Yoga = await mod.getYogaModule() + if (Yoga) return Yoga + + if (initializationPromise) { + await initializationPromise + return Yoga } + + initializationPromise = import('@yoga') + .then((mod) => mod.getYogaModule()) + .then((yogaInstance) => (Yoga = yogaInstance)) + + await initializationPromise + initializationPromise = null + return Yoga }