-
Notifications
You must be signed in to change notification settings - Fork 932
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
[reactive-element] Adds decorators for @compute
, @observe
, and @listen
#1870
base: main
Are you sure you want to change the base?
Conversation
* Adds `@computed` decorator for creating computed properties. Decorator specifies an array of deps which are the names of dependent properties and a compute function which is called with dependency values and should return the computed property value. * Adds `@observe` decorator for observing property changes. Decorator specifies an observe function which receives the current and previous value of the property as well as the name of the property. Note, the observed property must be a reactive property. * Adds `@listen` decorator for declaratively adding event listeners. The decorator should be placed on an element property which is an event listener function. The decorator must provide an object which specifies the event `type`, `options`, and an optional `target`. The `target` defaults to the element itself or may be `root` to add the listener to the element's `renderRoot`, or an instance of `EventTarget`. If `target` is an `EventTarget`, the listener is dynamically added and removed when the element is connected and disconnected.
📊 Tachometer Benchmark ResultsSummarynop-update
render
update
update-reflect
Resultslit-element-list
render
update
update-reflect
lit-html-kitchen-sink
render
update
nop-update
lit-html-repeat
render
update
lit-html-template-heavy
render
update
reactive-element-list
render
update
update-reflect
|
@sorvell could we get this broken up into separate PRs per decorator? |
@compute
, @observe
, and @listen
@compute
, @observe
, and @listen
* private _clickHandler(e: Event) => { | ||
* this.clickedOn = e.target.localName; | ||
* } | ||
* @listen('keydown', window, {capture: true})) |
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 noticed that the docs do not match the function definition.
listen('keydown', window, { capture: true })
vs
listen({ type: 'keydown', options: { capture: true }, target: window })
Pretty excited about these APIs. Betting there was some internal discussions on your end.. was kind of wondering about multi-property observers? |
@sorvell what is the status of this? Thanks! |
Curious if there is still a plan to add these decorators? They would be very handy, and i expect many component libraries end up implementing these themselves. I implemented an @property()
@observe(() => {
this.doSomething() // error `this` is undefined
})
foo: number = 0 I guess maybe the code is no longer compatible with Lit 2? Since I couldn't workout a good approach to allow access to @property()
foo: number = 0
@observe("foo")
doSomething() {
} This also works with |
Also interested on this! |
Sharing interest! |
If anyone is interested in these features (excluding |
Fixes #1873.
Adds
@computed
decorator for creating computed properties. Decorator specifies an array of deps which are the names of dependent properties and a compute function which is called with dependency values and should return the computed property value.Adds
@observe
decorator for observing property changes. Decorator specifies an observe function which receives the current and previous value of the property as well as the name of the property. Note, the observed property must be a reactive property.Adds
@listen
decorator for declaratively adding event listeners. The decorator should be placed on an element property which is an event listener function. The decorator must provide an object which specifies the eventtype
,options
, and an optionaltarget
. Thetarget
defaults to the element itself or may beroot
to add the listener to the element'srenderRoot
, or an instance ofEventTarget
. Iftarget
is anEventTarget
,the listener is dynamically added and removed when the element is connected and disconnected.