Skip to content
Prev Previous commit
Next Next commit
Streamlined existence checks
  • Loading branch information
ericsoderberghp committed Jun 5, 2018
commit 037c4d5f61674d11a6cf1ce4e78f37cf5c1341e6
20 changes: 9 additions & 11 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ class ReactDOMServerRenderer {
previousWasTextNode: boolean;
makeStaticMarkup: boolean;

providerStack: Array<?ReactProvider<any>>;
providerStack: Array<ReactProvider<any>>;
providerIndex: number;

constructor(children: mixed, makeStaticMarkup: boolean) {
Expand Down Expand Up @@ -686,21 +686,19 @@ class ReactDOMServerRenderer {
'Unexpected pop.',
);
}
this.providerStack[this.providerIndex] = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does .length assignment do in V8? I'd like to make sure it doesn't think the array length change is likely to "stay" because it changes all the time. I think just using null is more straightforward and less surprising to the engine (even though the typing isn't as nice for us).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. But, going back to using null seems to require testing for null when accessing array items by index, since Flow has been told that null and undefined are expected array item values. Or, is there an alternate Flow syntax that allows us to remove the explicit checks?

this.providerStack.length = this.providerIndex;
this.providerIndex -= 1;
const context: ReactContext<any> = provider.type._context;
// find the correct previous provider based on type
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is redundant, IMO the code speaks for itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

let previousProvider;
if (this.providerIndex > -1) {
for (let i = this.providerIndex; i >= 0; i--) {
if (this.providerStack[i] !== null && this.providerStack[i] !== undefined &&
(this.providerStack[i]: ReactProvider<any>).type === provider.type) {
previousProvider = this.providerStack[i];
break;
}
let previousProvider = null;
for (let i = this.providerIndex; i >= 0; i--) {
// We assume this Flow type is correct because of the index check above.
if ((this.providerStack[i]: ReactProvider<any>).type === provider.type) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please read this.providerStack[i] once. (Move it to a constant.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep.

previousProvider = this.providerStack[i];
break;
}
}
if (previousProvider) {
if (previousProvider !== null) {
context._currentValue = previousProvider.props.value;
} else {
context._currentValue = context._defaultValue;
Expand Down