Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Ensure react lazy initializer is in the top generator body #2026

Closed

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented May 25, 2018

Release notes: none

We need to ensure we emit the ReactElement lazy initializer is in the top generator body (the function body) of the React component render. If we emit it in a nested generator body, it may not get called in the right place after we've nested many components. This also renames a bunch of functions to optimized functions rather than additional functions.

I've got an issue tracking the progress on a follow up PR to add regression tests for this PR: #2027

while (topBody.parentBody !== undefined) {
topBody = topBody.parentBody;
}
// temproarily set the body to be the top parent body
Copy link
Contributor

Choose a reason for hiding this comment

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

spelling

@@ -504,4 +504,17 @@ export class Emitter {
invariant(!this._finalized);
return new BodyReference(this._body, this._body.entries.length);
}
emitLazyReactElementInitializer(statement: BabelNodeStatement): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer it if we don't add such special cases to the Emitter. The Emitter is already confusing enough...

@@ -96,7 +96,7 @@ export class ResidualReactElementSerializer {
t.callExpression(funcId, originalCreateElementIdentifier ? [originalCreateElementIdentifier] : [])
)
);
this.residualHeapSerializer.emitter.emit(statement);
this.residualHeapSerializer.emitter.emitLazyReactElementInitializer(statement);
Copy link
Contributor

Choose a reason for hiding this comment

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

If this intends to do what I think it does, then you want to do the following:

  1. Pass the result of this.residualHeapSerializer.isReferencedOnlyByAdditionalFunction(value) (currently evaluated by caller) into this function; let's call it additionalFunction. It should be defined by construction.
  2. Then replace this line with the following:
this.residualHeapSerializer._getPrelude(additionalFunction).push(statement);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's exactly what I want to do!

@trueadm
Copy link
Contributor Author

trueadm commented May 25, 2018

@NTillmann are you happy with the requested changes?

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants