Skip to content
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

Merged
merged 2 commits into from
Mar 5, 2019

Conversation

trevor-bliss
Copy link
Contributor

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?

  • Yes
  • No

@trevor-bliss trevor-bliss requested a review from kevinv11n March 5, 2019 04:07
@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: d8a7aaf | Target commit: 2a917df

lwc-engine-benchmark

table-append-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table/append/1k duration 153.55 (±5.00 ms) 151.20 (±4.95 ms) -2.4ms (1.5%) 👌
table-clear-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table/clear/1k duration 10.65 (±0.65 ms) 10.60 (±0.50 ms) -0.0ms (0.5%) 👌
table-create-10k metric base(d8a7aaf) target(2a917df) trend
benchmark-table/create/10k duration 881.90 (±6.70 ms) 888.15 (±6.65 ms) +6.3ms (0.7%) 👎
table-create-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table/create/1k duration 118.30 (±2.15 ms) 118.60 (±3.45 ms) +0.3ms (0.3%) 👌
table-update-10th-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table/update-10th/1k duration 74.40 (±1.60 ms) 85.05 (±2.60 ms) +10.6ms (14.3%) 👎
tablecmp-append-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-component/append/1k duration 229.30 (±11.50 ms) 224.40 (±9.80 ms) -4.9ms (2.1%) 👌
tablecmp-clear-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-component/clear/1k duration 6.20 (±1.10 ms) 6.45 (±1.15 ms) +0.3ms (4.0%) 👌
tablecmp-create-10k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-component/create/10k duration 1778.95 (±10.55 ms) 1790.30 (±8.25 ms) +11.3ms (0.6%) 👎
tablecmp-create-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-component/create/1k duration 213.60 (±5.50 ms) 212.30 (±5.45 ms) -1.3ms (0.6%) 👌
tablecmp-update-10th-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-component/update-10th/1k duration 68.35 (±5.80 ms) 68.85 (±5.00 ms) +0.5ms (0.7%) 👌
wc-append-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-wc/append/1k duration 228.95 (±19.35 ms) 234.60 (±18.00 ms) +5.7ms (2.5%) 👌
wc-clear-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-wc/clear/1k duration 11.05 (±1.85 ms) 10.80 (±1.90 ms) -0.3ms (2.3%) 👌
wc-create-10k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-wc/create/10k duration 1908.00 (±11.90 ms) 1918.50 (±14.65 ms) +10.5ms (0.6%) 👎
wc-create-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-wc/create/1k duration 222.15 (±6.35 ms) 223.10 (±6.60 ms) +0.9ms (0.4%) 👌
wc-update-10th-1k metric base(d8a7aaf) target(2a917df) trend
benchmark-table-wc/update-10th/1k duration 70.30 (±5.00 ms) 68.35 (±4.80 ms) -2.0ms (2.8%) 👌

Copy link
Contributor

@kevinv11n kevinv11n left a 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') {
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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');

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: d8a7aaf | Target commit: a33728e

lwc-engine-benchmark

table-append-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table/append/1k duration 153.55 (±5.00 ms) 153.75 (±6.30 ms) +0.2ms (0.1%) 👌
table-clear-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table/clear/1k duration 10.65 (±0.65 ms) 10.80 (±0.55 ms) +0.2ms (1.4%) 👌
table-create-10k metric base(d8a7aaf) target(a33728e) trend
benchmark-table/create/10k duration 881.90 (±6.70 ms) 886.10 (±6.90 ms) +4.2ms (0.5%) 👌
table-create-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table/create/1k duration 118.30 (±2.15 ms) 118.80 (±2.75 ms) +0.5ms (0.4%) 👌
table-update-10th-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table/update-10th/1k duration 74.40 (±1.60 ms) 84.95 (±1.15 ms) +10.5ms (14.2%) 👎
tablecmp-append-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-component/append/1k duration 229.30 (±11.50 ms) 227.10 (±7.55 ms) -2.2ms (1.0%) 👌
tablecmp-clear-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-component/clear/1k duration 6.20 (±1.10 ms) 5.90 (±0.95 ms) -0.3ms (4.8%) 👌
tablecmp-create-10k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-component/create/10k duration 1778.95 (±10.55 ms) 1765.65 (±10.95 ms) -13.3ms (0.7%) 👍
tablecmp-create-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-component/create/1k duration 213.60 (±5.50 ms) 218.20 (±5.15 ms) +4.6ms (2.2%) 👎
tablecmp-update-10th-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-component/update-10th/1k duration 68.35 (±5.80 ms) 67.05 (±2.95 ms) -1.3ms (1.9%) 👌
wc-append-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-wc/append/1k duration 228.95 (±19.35 ms) 235.90 (±16.20 ms) +6.9ms (3.0%) 👌
wc-clear-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-wc/clear/1k duration 11.05 (±1.85 ms) 10.70 (±1.45 ms) -0.4ms (3.2%) 👌
wc-create-10k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-wc/create/10k duration 1908.00 (±11.90 ms) 1914.35 (±11.75 ms) +6.3ms (0.3%) 👌
wc-create-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-wc/create/1k duration 222.15 (±6.35 ms) 222.40 (±5.40 ms) +0.2ms (0.1%) 👌
wc-update-10th-1k metric base(d8a7aaf) target(a33728e) trend
benchmark-table-wc/update-10th/1k duration 70.30 (±5.00 ms) 66.95 (±3.65 ms) -3.4ms (4.8%) 👍

@trevor-bliss trevor-bliss merged commit dfa87d1 into master Mar 5, 2019
@trevor-bliss trevor-bliss deleted the tbliss/wire-service-hook-guards branch March 5, 2019 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants