-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Support @provide with experimental decorators on a property It might feel like this is working at cross purposes to our standard decorators work, because those all require accessors, but I don't think so. With standard decorators we can use the type system to error out if they're used on a property instead of an accessor. With experimental decorators, they sorta work with properties, in that they do provide values and update them, but reading the value back off the field would always give undefined because we were defining it without a getter. * Address review comments * Fix import.
- Loading branch information
Showing
3 changed files
with
83 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lit-labs/context': patch | ||
--- | ||
|
||
Support @provide with experimental decorators on a property. Previously we only supported it on an accessor, which happened automatically if used with @state or @property. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {Context, consume, provide} from '@lit-labs/context'; | ||
import {assert} from '@esm-bundle/chai'; | ||
import {LitElement, TemplateResult, html} from 'lit'; | ||
|
||
const simpleContext = 'simple-context' as Context<'simple-context', number>; | ||
|
||
test(`@provide on a property, not an accessor`, async () => { | ||
class ProviderWithoutAccessorElement extends LitElement { | ||
// Note that value doesn't use `accessor`, or `@property()`, or `@state` | ||
@provide({context: simpleContext}) | ||
value = 0; | ||
|
||
override render(): TemplateResult { | ||
return html`<simple-consumer></simple-consumer>`; | ||
} | ||
} | ||
customElements.define( | ||
'provider-without-accessor', | ||
ProviderWithoutAccessorElement | ||
); | ||
class SimpleConsumer extends LitElement { | ||
@consume({context: simpleContext, subscribe: true}) | ||
value = -1; | ||
|
||
override render(): TemplateResult { | ||
return html``; | ||
} | ||
} | ||
customElements.define('simple-consumer', SimpleConsumer); | ||
|
||
const provider = document.createElement( | ||
'provider-without-accessor' | ||
) as ProviderWithoutAccessorElement; | ||
document.body.appendChild(provider); | ||
// The field's value is written with its initial value. | ||
assert.equal(provider.value, 0); | ||
await provider.updateComplete; | ||
const consumer = provider.shadowRoot?.querySelector( | ||
'simple-consumer' | ||
) as SimpleConsumer; | ||
// The consumer's value is written with the provider's initial value. | ||
assert.equal(provider.value, 0); | ||
assert.equal(consumer.value, 0); | ||
|
||
// Updating the provider also updates the subscribing consumer. | ||
provider.value = 1; | ||
await provider.updateComplete; | ||
assert.equal(provider.value, 1); | ||
assert.equal(consumer.value, 1); | ||
}); |