Skip to content

Commit 3014e94

Browse files
committed
[css-layout-api] Change examples from generators to promises.
1 parent d678f1f commit 3014e94

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

css-layout-api/EXPLAINER.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ below. You should read the code below with its explanatory section.
6666

6767
```js
6868
registerLayout('centering', class {
69-
layout(children, edges, constraints, styleMap) {
69+
async layout(children, edges, constraints, styleMap) {
7070
// (1) Determine our (inner) available size.
7171
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
7272
const availableBlockSize = constraints.fixedBlockSize ?
@@ -229,7 +229,7 @@ registerLayout('style-read', class {
229229
static get inputProperties() { return ['--a-number']; }
230230
static get childInputProperties() { return ['--a-string']; }
231231

232-
layout(children, edges, constraints, styleMap) {
232+
async layout(children, edges, constraints, styleMap) {
233233
// We can read our own style:
234234
styleMap.get('--a-number').value === 42;
235235

@@ -307,7 +307,7 @@ We pass the `BreakToken` to add back into the `layout()` call in order to produc
307307
registerLayout('basic-inline', class {
308308
static get layoutOptions() { return {childDisplay: 'normal'}; }
309309

310-
layout(children, edges, constraints, styleMap) {
310+
async layout(children, edges, constraints, styleMap) {
311311
// Determine our (inner) available size.
312312
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
313313
const availableBlockSize = constraints.fixedBlockSize !== null ?
@@ -415,7 +415,7 @@ We can make our children fragment by passing them a constraint space with a frag
415415

416416
```js
417417
registerLayout('special-multi-col', class {
418-
layout(children, edges, constraints, styleMap, breakToken) {
418+
async layout(children, edges, constraints, styleMap, breakToken) {
419419
for (let child of children) {
420420
// Create a constraint space with a fragmentation line.
421421
const childConstraints = {
@@ -441,7 +441,7 @@ We can also allow our own layout to be fragmented by respecting the fragmentatio
441441

442442
```js
443443
registerLayout('basic-inline', class {
444-
layout(children, edges, constraints, styleMap, breakToken) {
444+
async layout(children, edges, constraints, styleMap, breakToken) {
445445

446446
// We can check if we need to fragment in the block direction.
447447
if (constraints.blockFragmentationType != 'none') {

css-layout-api/Overview.bs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,10 @@ direction), while centering its children in the inline direction.
446446

447447
<pre class="lang-javascript">
448448
registerLayout('block-like', class {
449-
*intrinsicSizes(children, edges, styleMap) {
450-
const childrenSizes = yield children.map((child) => {
449+
async intrinsicSizes(children, edges, styleMap) {
450+
const childrenSizes = await Promise.all(children.map((child) => {
451451
return child.intrinsicSizes();
452-
});
452+
}));
453453

454454
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
455455
return Math.max(max, childSizes.maxContentSize);
@@ -462,16 +462,18 @@ registerLayout('block-like', class {
462462
return {maxContentSize, minContentSize};
463463
}
464464

465-
*layout(children, edges, constraints, styleMap) {
465+
async layout(children, edges, constraints, styleMap) {
466+
// Determine our (inner) available size.
466467
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
467-
const availableBlockSize = (constraints.fixedBlockSize || Infinity) - edges.all.block;
468+
const availableBlockSize = constraints.fixedBlockSize ?
469+
constraints.fixedBlockSize - edges.all.block : null;
468470

469471
const childFragments = [];
470472
const childConstraints = { availableInlineSize, availableBlockSize };
471473

472-
const childFragments = yield children.map((child) => {
474+
const childFragments = await Promise.all(children.map((child) => {
473475
return child.layoutNextFragment(childConstraints);
474-
});
476+
}));
475477

476478
let blockOffset = edges.all.blockStart;
477479
for (let fragment of childFragments) {
@@ -594,10 +596,10 @@ The example below shows the border-box intrinsic sizes of two children.
594596

595597
<pre class="lang-javascript">
596598
registerLayout('intrinsic-sizes-example', class {
597-
*intrinsicSizes(children, edges, styleMap) {
598-
const childrenSizes = yield children.map((child) => {
599+
async intrinsicSizes(children, edges, styleMap) {
600+
const childrenSizes = await Promise.all(children.map((child) => {
599601
return child.intrinsicSizes();
600-
});
602+
}));
601603

602604
childrenSizes[0].minContentSize; // 400, (380+10+10) child has a fixed size.
603605
childrenSizes[0].maxContentSize; // 400, (380+10+10) child has a fixed size.
@@ -606,7 +608,7 @@ registerLayout('intrinsic-sizes-example', class {
606608
childrenSizes[1].maxContentSize; // 200, size of "XXX XXXX".
607609
}
608610

609-
*layout() {}
611+
layout() {}
610612
});
611613
</pre>
612614
</div>
@@ -691,8 +693,8 @@ creates child layout constraints which specify that a child should be a fixed in
691693

692694
<pre class="lang-javascript">
693695
registerLayout('flex-distribution-like', class {
694-
*intrinsicSizes(children, edges, styleMap) {
695-
const childrenSizes = yield children.map((child) => {
696+
async intrinsicSizes(children, edges, styleMap) {
697+
const childrenSizes = children.map((child) => {
696698
return child.intrinsicSizes();
697699
});
698700

@@ -707,18 +709,18 @@ registerLayout('flex-distribution-like', class {
707709
return {maxContentSize, minContentSize};
708710
}
709711

710-
*layout(children, edges, constraints, styleMap) {
711-
712+
async layout(children, edges, constraints, styleMap) {
713+
// Determine our (inner) available size.
712714
const availableInlineSize =
713715
constraints.fixedInlineSize - edges.all.inline;
714-
const availableBlockSize =
715-
(constraints.fixedInlineSize || Infinity) - edges.all.block;
716+
const availableBlockSize = constraints.fixedBlockSize ?
717+
constraints.fixedBlockSize - edges.all.block : null;
716718

717719
const childConstraints = { availableInlineSize, availableBlockSize };
718720

719-
const unconstrainedChildFragments = yield children.map((child) => {
721+
const unconstrainedChildFragments = await Promise.all(children.map((child) => {
720722
return child.layoutNextFragment(childConstraints);
721-
});
723+
}));
722724

723725
const unconstrainedSizes = [];
724726
const totalSize = unconstrainedChildFragments.reduce((sum, fragment, i) => {
@@ -730,12 +732,12 @@ registerLayout('flex-distribution-like', class {
730732
const remainingSpace = Math.max(0, inlineSize - totalSize);
731733
const extraSpace = remainingSpace / children.length;
732734

733-
const childFragments = yield children.map((child, i) => {
735+
const childFragments = await Promise.all(children.map((child, i) => {
734736
return child.layoutNextFragment({
735737
fixedInlineSize: unconstrainedSizes[i] + extraSpace,
736738
availableBlockSize
737739
});
738-
});
740+
}));
739741

740742
// Position the fragments.
741743
let inlineOffset = 0;
@@ -866,13 +868,12 @@ registerLayout('indent-lines', class {
866868
static layoutOptions = {childDisplay: 'normal'};
867869
static inputProperties = ['--indent', '--indent-lines'];
868870

869-
*layout(children, edges, constraints, styleMap, breakToken) {
870-
871+
async layout(children, edges, constraints, styleMap, breakToken) {
871872
// Determine our (inner) available size.
872873
const availableInlineSize =
873874
constraints.fixedInlineSize - edges.all.inline;
874-
const availableBlockSize =
875-
(constraints.fixedBlockSize || Infinity) - edges.all.block;
875+
const availableBlockSize = constraints.fixedBlockSize ?
876+
constraints.fixedBlockSize - edges.all.block : null;
876877

877878
// Detrermine the number of lines to indent, and the indent amount.
878879
const indent = resolveLength(constraints, styleMap.get('--indent'));
@@ -904,7 +905,7 @@ registerLayout('indent-lines', class {
904905
blockFragmentationType: constraints.blockFragmentationType,
905906
};
906907

907-
const fragment = yield child.layoutNextFragment(childConstraints,
908+
const fragment = await child.layoutNextFragment(childConstraints,
908909
childBreakToken);
909910
childFragments.push(fragment);
910911

@@ -1020,7 +1021,7 @@ This example shows an node styled by CSS, and what its respective {{LayoutEdges}
10201021

10211022
<pre class="lang-javascript">
10221023
registerLayout('box-edges', class {
1023-
*layout(children, edges, constraints, styleMap, breakToken) {
1024+
async layout(children, edges, constraints, styleMap, breakToken) {
10241025
edges.padding.inlineStart; // 5 (as 10% * 50px = 5px).
10251026
edges.border.blockEnd; // 2
10261027
edges.scrollbar.inlineEnd; // UA-dependent, may be 0 or >0 (e.g. 16).
@@ -1219,7 +1220,7 @@ baselines themselves. This will be of the form:
12191220

12201221
To <em>query</em> baseline information from a {{LayoutChild}}.
12211222
<pre class="lang-javascript">
1222-
const fragment = yield child.layoutNextFragment({
1223+
const fragment = await child.layoutNextFragment({
12231224
fixedInlineSize: availableInlineSize,
12241225
baselineRequests: ['alphabetic', 'middle'],
12251226
});
@@ -1229,7 +1230,7 @@ fragment.baselines.get('alphabetic') === 25 /* or something */;
12291230
To <em>produce</em> baseline information for a <a>parent layout</a>:
12301231
<pre class="lang-javascript">
12311232
registerLayout('baseline-producing', class {
1232-
*layout(children, edges, constraints, styleMap) {
1233+
async layout(children, edges, constraints, styleMap) {
12331234
const result = {baselines: {}};
12341235

12351236
for (let baselineRequest of constraints.baselineRequests) {
@@ -1549,11 +1550,11 @@ is called, the user agent <em>must</em> run the following steps:
15491550
return {childDisplay: 'normal', sizing: 'block-like'}
15501551
}
15511552

1552-
*intrinsicSizes(children, edges, styleMap) {
1553+
async intrinsicSizes(children, edges, styleMap) {
15531554
// Intrinsic sizes code goes here.
15541555
}
15551556

1556-
*layout(children, edges, constraints, styleMap, breakToken) {
1557+
async layout(children, edges, constraints, styleMap, breakToken) {
15571558
// Layout code goes here.
15581559
}
15591560
}

0 commit comments

Comments
 (0)