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

Wrap returned component data with Readonly #11

Closed
Insadem opened this issue Feb 3, 2023 · 5 comments
Closed

Wrap returned component data with Readonly #11

Insadem opened this issue Feb 3, 2023 · 5 comments

Comments

@Insadem
Copy link

Insadem commented Feb 3, 2023

Component data is immutable, but only for first scope. So I can easily edit 2-nd+ scope by accident. Is it by design, or it's impossible to wrap it to Readonly?
Ex:

SomeComponent({
someKey: []
});

const someComponent = world.get(...);
someComponent.someKey.push({});
@Insadem Insadem changed the title Wrap returned component data with Readonly. Wrap returned component data with Readonly Feb 3, 2023
@LastTalon
Copy link
Member

LastTalon commented Feb 5, 2023

Well matter really doesn't care if you use mutable components. It doesn't really break in any way if you give it a mutable component, you just won't get queryChanged updates for that mutable component.

However, here the issue looks like the issue is not that your component is mutable, but your someKey is a regular Array, not a ReadonlyArray. This is the same way matter sans TS handles this as well, only the first level table is frozen, if you want deep freezing you'll have to do that yourself.

@Insadem
Copy link
Author

Insadem commented Feb 5, 2023

Well matter really doesn't care if you use mutable components. It doesn't really break in any way if you give it a mutable component, you just won't get queryChanged updates for that mutable component.

However, here the issue looks like the issue is not that your component is mutable, but your someKey is a regular Array, not a ReadonlyArray.

I understand this. Maybe it's better to close this issue, because it's optional to mutate component data. I wish matter had more explicit documentation, like what are requirements to trigger queryChanged (I figured it out through code, but still..)

@LastTalon
Copy link
Member

I wish matter had more explicit documentation, like what are requirements to trigger queryChanged

If you find a good way to explain this and update the docs, I'm sure a matter PR would be welcome. I think the issue so far has been that there are a few nuances like this that you can technically do but aren't encouraged out of the box. Because of that they can be pretty tricky to explain accurately and concisely for general users and would likely be more confusing.

@Insadem
Copy link
Author

Insadem commented Feb 5, 2023

Yeah, sure. I just thought what is the purpose of component mutation if it doesn't trigger queryChanged (performance wise it's bad idea). But then I realized that it's very handy to be able mutate existing data. I just wanted some safety, but probably I will just get used to it. You anyway need to call world.insert to trigger queryChanged, so it's not smart to prohibit edit component data (reference types under the keys).

@LastTalon
Copy link
Member

I just wanted some safety, but probably I will just get used to it.

If you want that safety, you can still type them yourself to ensure you're using your components correctly in the places you want the safety.

@Insadem Insadem closed this as completed Feb 5, 2023
Ukendio pushed a commit that referenced this issue Feb 1, 2024
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

No branches or pull requests

2 participants