Skip to content

Avoid mutating annotation object for Lightning Web Security (LWS) #980

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nwcm
Copy link

@nwcm nwcm commented Aug 4, 2025

I'm looking to ensure the plugin is compatible with the LWS restrictions so it can be implemented in LWC. While I'm sure an uncommon usage here it would be nice if it can be compatible.

#979

The main issue is setting of value.id = key in beforeUpdate

I have simply removed the assignment to the object to avoid mutation, I'm unclear on any downstream impacts of removing this assignment. It looks that the id is used in element.js getContext. However, annotations seem to function without the assignment.

function getContext(chart, element, elements, annotation) {
  return element.$context || (element.$context = Object.assign(Object.create(chart.getContext()), {
    element,
    get elements() {
      return elements.filter((el) => el && el.options);
    },
    id: annotation.id,
    type: 'annotation'
  }));
}

I tried cloning the object however afterUpdate -> updateElements fails on annotationOptions.setContext(getContext(chart, element, elements, annotationOptions)); where setContext is now undefined. So cloning would require a deeper fix

Reading for LWS restrictions.

A common problematic coding practice is mutating objects that the namespace doesn’t own. When you mutate an object, you add or remove properties of the object or change values of properties. You change the "shape" of the object. When you mutate an object in a component that’s running in Lightning Web Security (LWS), that change isn’t propagated outside the namespace sandbox, which leads to errors.

https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-mutation-alternatives.html
https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-proxies.html
https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-proxies.html

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.

1 participant