Allow passing a custom loader such as from yoga-wasm-web - #699
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the Yoga layout engine initialization to allow for custom loaders, enabling flexibility for different Yoga implementations like yoga-wasm-web.
- Replaces automatic Yoga preloading with lazy initialization
- Adds optional
yogaLoaderparameter togetYoga()function with default fallback - Caches the yoga promise to prevent multiple initializations
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| let yogaPromise: Promise<Yoga> | ||
| export function getYoga(yogaLoader: () => Promise<Yoga> = loadYoga) { | ||
| if (!yogaPromise) { | ||
| yogaPromise = yogaLoader() |
There was a problem hiding this comment.
The API design could lead to inconsistent behavior. If different callers pass different yogaLoader functions, only the first one will be used due to the caching mechanism. Consider either preventing this scenario or documenting this behavior clearly.
| yogaPromise = yogaLoader() | |
| let yogaPromise: Promise<Yoga> | |
| let yogaLoaderUsed: (() => Promise<Yoga>) | undefined | |
| /** | |
| * Returns a promise for the Yoga instance. | |
| * Only the first loader function passed will be used; subsequent calls with a different loader will throw an error. | |
| */ | |
| export function getYoga(yogaLoader: () => Promise<Yoga> = loadYoga) { | |
| if (!yogaPromise) { | |
| yogaPromise = yogaLoader() | |
| yogaLoaderUsed = yogaLoader | |
| } else if (yogaLoaderUsed && yogaLoaderUsed !== yogaLoader) { | |
| throw new Error( | |
| 'getYoga: yogaLoader function can only be set once. Subsequent calls must use the same loader.' | |
| ) |
| export function getYoga(yogaLoader: () => Promise<Yoga> = loadYoga) { | ||
| if (!yogaPromise) { | ||
| yogaPromise = yogaLoader() | ||
| } |
There was a problem hiding this comment.
This implementation has a potential race condition. If multiple calls to getYoga() happen simultaneously before yogaPromise is set, multiple yoga instances could be created. Consider using a more robust initialization pattern or synchronization mechanism.
| } | |
| // Use nullish coalescing assignment to ensure only one promise is created | |
| yogaPromise ??= yogaLoader() |
|
Covered by #705 |
No description provided.