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

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/context': patch
---

Use `target.set` instead of member assignment in `@consume()` decorator.
17 changes: 9 additions & 8 deletions packages/context/src/lib/decorators/consume.ts
Original file line number Diff line number Diff line change
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>
augustjk marked this conversation as resolved.
Show resolved Hide resolved
) => {
if (typeof nameOrContext === 'object') {
// Standard decorators branch
nameOrContext.addInitializer(function (this: ReactiveElement): void {
nameOrContext.addInitializer(function () {
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
24 changes: 13 additions & 11 deletions packages/context/src/lib/decorators/provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ export function provide<ValueType>({
}: {
context: Context<unknown, ValueType>;
}): ProvideDecorator<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>
) => {
// Map of instances to controllers
const controllerMap = new WeakMap<
Expand All @@ -51,18 +53,18 @@ export function provide<ValueType>({
>();
if (typeof nameOrContext === 'object') {
// Standard decorators branch
nameOrContext.addInitializer(function (this: ReactiveElement) {
nameOrContext.addInitializer(function () {
controllerMap.set(this, new ContextProvider(this, {context}));
});
return {
get(this: ReactiveElement) {
return protoOrTarget.get.call(this as unknown as C);
return protoOrTarget.get.call(this);
},
set(this: ReactiveElement, value: V) {
set(this: ReactiveElement, value: ValueType) {
controllerMap.get(this)?.setValue(value);
return protoOrTarget.set.call(this as unknown as C, value);
return protoOrTarget.set.call(this, value);
},
init(this: ReactiveElement, value: V) {
init(this: ReactiveElement, value: ValueType) {
controllerMap.get(this)?.setValue(value);
return value;
},
Expand All @@ -84,10 +86,10 @@ export function provide<ValueType>({
if (descriptor === undefined) {
const valueMap = new WeakMap<ReactiveElement, ValueType>();
newDescriptor = {
get: function (this: ReactiveElement) {
get(this: ReactiveElement) {
return valueMap.get(this);
},
set: function (this: ReactiveElement, value: ValueType) {
set(this: ReactiveElement, value: ValueType) {
controllerMap.get(this)!.setValue(value);
valueMap.set(this, value);
},
Expand All @@ -98,7 +100,7 @@ export function provide<ValueType>({
const oldSetter = descriptor.set;
newDescriptor = {
...descriptor,
set: function (this: ReactiveElement, value: ValueType) {
set(this: ReactiveElement, value: ValueType) {
controllerMap.get(this)!.setValue(value);
oldSetter?.call(this, value);
},
Expand Down