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

[lit/context] Use target.set for setting context values #4598

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-shirts-peel.md
@@ -0,0 +1,5 @@
---
'@lit/context': patch
---

Use `target.set` instead of member assignment
17 changes: 9 additions & 8 deletions packages/context/src/lib/decorators/consume.ts
Expand Up @@ -47,18 +47,19 @@ export function consume<ValueType>({
context: Context<unknown, ValueType>;
subscribe?: boolean;
}): ConsumeDecorator<ValueType> {
return (<C extends ReactiveElement, V extends ValueType>(
protoOrTarget: ClassAccessorDecoratorTarget<C, V>,
nameOrContext: PropertyKey | ClassAccessorDecoratorContext<C, V>
return ((
protoOrTarget: ClassAccessorDecoratorTarget<ReactiveElement, ValueType>,
nameOrContext:
| PropertyKey
| ClassAccessorDecoratorContext<ReactiveElement, ValueType>
) => {
if (typeof nameOrContext === 'object') {
// Standard decorators branch
nameOrContext.addInitializer(function (this: ReactiveElement): void {
nameOrContext.addInitializer(function (): void {
new ContextConsumer(this, {
context,
callback: (value: ValueType) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this as any)[nameOrContext.name] = value;
callback: (value) => {
protoOrTarget.set.call(this, value);
},
subscribe,
});
Expand All @@ -69,7 +70,7 @@ export function consume<ValueType>({
(element: ReactiveElement): void => {
new ContextConsumer(element, {
context,
callback: (value: ValueType) => {
callback: (value) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(element as any)[nameOrContext] = value;
},
Expand Down