Skip to content

Commit

Permalink
fix: initializes yoga without race conditions (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeetiss authored Feb 18, 2023
1 parent 48aea6f commit 35dc719
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/yoga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ export function init(yoga: typeof Yoga) {
Yoga = yoga
}

let initializationPromise = null

export default async function getYoga(): Promise<typeof Yoga> {
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
}

1 comment on commit 35dc719

@vercel
Copy link

@vercel vercel bot commented on 35dc719 Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.