Skip to content

Commit

Permalink
Async load Yoga after all
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Jun 28, 2024
1 parent 77b12f3 commit 8d52c69
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/layout/src/steps/resolveYoga.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { loadYoga } from '../yoga/index';

const resolveYoga = (root) => {
const yoga = loadYoga();
const resolveYoga = async (root) => {
const yoga = await loadYoga();

return Object.assign({}, root, { yoga });
};
Expand Down
9 changes: 5 additions & 4 deletions packages/layout/src/yoga/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable import/prefer-default-export */

import Yoga from 'yoga-layout';
import { loadYoga as yogaLoadYoga } from 'yoga-layout/load';

export const loadYoga = () => {
const config = Yoga.Config.create();
export const loadYoga = async () => {
const instance = await yogaLoadYoga();
const config = instance.Config.create();

config.setPointScaleFactor(0);

const node = { create: () => Yoga.Node.createWithConfig(config) };
const node = { create: () => instance.Node.createWithConfig(config) };

return { node };
};
10 changes: 5 additions & 5 deletions packages/layout/tests/steps/resolvePagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const calcLayout = (node) => resolvePagination(resolveDimensions(node));

describe('pagination step', () => {
test('should stretch absolute block to full page size', async () => {
const yoga = loadYoga();
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('pagination step', () => {
});

test('should force new height for split nodes', async () => {
const yoga = loadYoga();
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('pagination step', () => {
});

test('should force new height for split nodes with fixed height', async () => {
const yoga = loadYoga();
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('pagination step', () => {
});

test('should not wrap page with false wrap prop', async () => {
const yoga = loadYoga();
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('pagination step', () => {
});

test('should break on a container whose children can not fit on a page', async () => {
const yoga = loadYoga();
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
Expand Down
10 changes: 5 additions & 5 deletions packages/layout/tests/steps/resolveTextLayout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { loadYoga } from '../../src/yoga';
import resolveTextLayout from '../../src/steps/resolveTextLayout';
import resolveDimensions from '../../src/steps/resolveDimensions';

const getRoot = (text = 'hello world', styles = {}) => ({
const getRoot = async (text = 'hello world', styles = {}) => ({
type: 'DOCUMENT',
yoga: loadYoga(),
yoga: await loadYoga(),
children: [
{
type: 'PAGE',
Expand Down Expand Up @@ -38,14 +38,14 @@ describe('text layout step', () => {
const getText = (root) => root.children[0].children[0];

test('should calculate lines for text while resolve dimensions', async () => {
const root = getRoot('text text text');
const root = await getRoot('text text text');
const dimensions = resolveDimensions(root);

expect(getText(dimensions).lines).toBeDefined();
});

test('should calculate lines for text width defined height', async () => {
const root = getRoot('text text text', { height: 50 });
const root = await getRoot('text text text', { height: 50 });
const dimensions = resolveDimensions(root);

expect(getText(dimensions).lines).not.toBeDefined();
Expand All @@ -56,7 +56,7 @@ describe('text layout step', () => {
});

test('should calculate lines for empty text', async () => {
const root = getRoot('');
const root = await getRoot('');
const dimensions = resolveDimensions(root);

expect(getText(dimensions).lines).toBeDefined();
Expand Down

0 comments on commit 8d52c69

Please sign in to comment.