-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(wire-service): add lifecycle hook guards #1092
Conversation
Benchmark resultsBase commit: lwc-engine-benchmark
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor changes, please
@@ -110,6 +110,9 @@ const wireService = { | |||
|
|||
connected: (cmp: LightningElement, data: object, def: ElementDef, context: Context) => { | |||
let listeners: NoArgumentListener[]; | |||
if (process.env.NODE_ENV !== 'production') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need this check because wiring
-> connected
transition is sync so it's not possible for this invariant to be violated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can reproduce this in a test if I register the wire adapter after calling createElement
and before pushing to the DOM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, keep in mind that the error boundary protocol allows a sync removal of the elements when the error occur, which means that if a child element throws during construction, the parent shadow will be cleaned up before the connect is invoked all together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a child element throws during construction, is the wiring
hook called for that component? I assume connect
and disconnect
are not called at all.
the parent shadow will be cleaned up before the connect is invoked all together.
What does cleaned up
entail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleaned up means that the offender VM/CustomElement will get its shadow root cleaned (empty of any element due to a broken state).
throws could occur at many levels, including constructor of the parent, constructor of a child, connected of parent, render of parent, connected of child, render of child, renderedCallback of child, renderedCallback of parent. Plus, the wiring in the middle, which we don't protect today (eventually will). The consequence of a failure will depend on when and where it happens though.
@@ -118,6 +121,9 @@ const wireService = { | |||
|
|||
disconnected: (cmp: LightningElement, data: object, def: ElementDef, context: Context) => { | |||
let listeners: NoArgumentListener[]; | |||
if (process.env.NODE_ENV !== 'production') { | |||
assert.isTrue(context[CONTEXT_ID], 'wire adapter "disconnected" hook called without an established context'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.isTrue(context[CONTEXT_ID], 'wire adapter "disconnected" hook called without an established context'); | |
assert.isTrue(context[CONTEXT_ID], 'wire service was not initialized prior to component creation: "disconnected" service hook invoked without necessary context'); |
Benchmark resultsBase commit: lwc-engine-benchmark
|
Details
In tests we provide an API that allows users to register a test wire adapter and emit data through the wire. If a user registers the adapter after the element is created they get a cryptic error when that element is disconnected from the DOM.
Users should always register the adapter before creating the element, but we should also have a better error message than a NPE when they don't.
Does this PR introduce a breaking change?