Simplifying "pre-reflow" logic

This commit is contained in:
Jeff Avallone 2018-02-17 16:28:46 -05:00
parent 1f9ba28099
commit 44e6dae289
6 changed files with 11 additions and 43 deletions

View File

@ -46,28 +46,20 @@ class Base extends React.PureComponent {
}; };
} }
async doPreReflow() { async reflowChildren() {
const components = this.preReflow();
// No child components // No child components
if (components === undefined) { if (this.children === undefined) {
return true; return true;
} }
// List of child components const reflowed = await Promise.all(this.children.map(c => c.doReflow()));
if (components.map) {
const componentsReflowed = await Promise.all(components.map(c => c.doReflow()));
return componentsReflowed.reduce((memo, value) => memo || value, false); return reflowed.reduce((memo, value) => memo || value, false);
}
// One child component
return components.doReflow();
} }
async doReflow() { async doReflow() {
const oldBBox = this._currentBBox(); const oldBBox = this._currentBBox();
const shouldReflow = await this.doPreReflow(); const shouldReflow = await this.reflowChildren();
if (shouldReflow) { if (shouldReflow) {
this.reflow(); this.reflow();
@ -76,10 +68,6 @@ class Base extends React.PureComponent {
return this._currentBBox() !== oldBBox; return this._currentBBox() !== oldBBox;
} }
preReflow() {
// Implemented in subclass, return array of components to reflow before this
}
reflow() { reflow() {
// Implemented in subclasses // Implemented in subclasses
} }

View File

@ -11,13 +11,9 @@ class Box extends Base {
radius: 3 radius: 3
} }
preReflow() {
return this.contained;
}
reflow() { reflow() {
const { padding, useAnchors } = this.props; const { padding, useAnchors } = this.props;
const box = this.contained.getBBox(); const box = this.children[0].getBBox();
const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0}; const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0};
this.setBBox({ this.setBBox({
@ -37,7 +33,7 @@ class Box extends Base {
}); });
} }
containedRef = contained => this.contained = contained containedRef = contained => this.children = [contained]
labelRef = label => this.label = label labelRef = label => this.label = label

View File

@ -40,10 +40,6 @@ class HorizontalLayout extends Base {
}, new Path()).toString(); }, new Path()).toString();
} }
preReflow() {
return this.children;
}
reflow() { reflow() {
const { spacing, withConnectors } = this.props; const { spacing, withConnectors } = this.props;

View File

@ -30,13 +30,9 @@ class Image extends Base {
height: 0 height: 0
} }
preReflow() {
return this.contained;
}
reflow() { reflow() {
const { padding } = this.props; const { padding } = this.props;
const box = this.contained.getBBox(); const box = this.children[0].getBBox();
return this.setStateAsync({ return this.setStateAsync({
width: Math.round(box.width + 2 * padding), width: Math.round(box.width + 2 * padding),
@ -44,7 +40,7 @@ class Image extends Base {
}); });
} }
containedRef = contained => this.contained = contained containedRef = contained => this.children = [contained]
svgRef = svg => this.svg = svg svgRef = svg => this.svg = svg

View File

@ -68,13 +68,9 @@ class Loop extends Base {
} }
} }
preReflow() {
return this.contained;
}
reflow() { reflow() {
const { skip, repeat, greedy } = this.props; const { skip, repeat, greedy } = this.props;
const box = this.contained.getBBox(); const box = this.children[0].getBBox();
const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0 }; const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0 };
let height = box.height + labelBox.height; let height = box.height + labelBox.height;
@ -105,7 +101,7 @@ class Loop extends Base {
}); });
} }
containedRef = contained => this.contained = contained containedRef = contained => this.children = [contained]
labelRef = label => this.label = label labelRef = label => this.label = label

View File

@ -78,10 +78,6 @@ class VerticalLayout extends Base {
} }
} }
preReflow() {
return this.children;
}
reflow() { reflow() {
const { spacing, withConnectors } = this.props; const { spacing, withConnectors } = this.props;