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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access array methods on property #66

Closed
ericktucto opened this issue Oct 12, 2019 · 4 comments
Closed

Cannot access array methods on property #66

ericktucto opened this issue Oct 12, 2019 · 4 comments

Comments

@ericktucto
Copy link

There are some fix methods that cannot be used in the properties: push, pop, shift, ...
For example click here

Sample error captures
Is Bad
isbad

Is Okay. 馃
isokay

@ericktucto ericktucto changed the title Not access methods of Array on property Cannot access array methods on property Oct 12, 2019
@smalluban
Copy link
Contributor

Hi @ericktucto!

The hybrids cache mechanism foundation is an equality check between property values over time. It means that you cannot safely modify object instances, because then other properties will not be notified about the change.

To protect library users, for example property() factory for objects uses Object.freeze() to block changes. It is done on purpose.

In the get definition of the property in documentation you can read:

It calculates the current property value. The returned value is cached by default. The cache mechanism works between properties defined by the library (even between different elements). If your get method does not use other properties, it won't be called again (the only way to update the value then is to assert new value or call invalidate from connect method).

Cache mechanism uses equality check to compare values (nextValue !== lastValue), so it enforces using immutable data, which is one of the ground rules of the library.

All methods that you mentioned - pop, push or shift mutates an array instance. Instead of using them, you should always return a new instance - as you do with filter or concat. For simpler syntax you can use spread operator for adding a new item into the array:

host.todos = [...host.todos, newTodo];

@smalluban
Copy link
Contributor

Here you have a few hints about your code example:

host.maxId++;

You can avoid manually updated property - it is strictly related to current todos property value, so you can define it as a getter:

const ListTodos = {
  maxId: ({ todos }) => todos[todos.length - 1].id, // if your ids will increment (so the last is the highest)
  // or 
  maxId: ({ todos }) => todos.length, // if you relay on the count of todos
}

Then in your addTodo function you can use it safely:

function addTodo(host) {
  const newTodo = { text: host.text, id: host.maxId + 1 };
  host.todos = [...host.todos, newTodo];
}

Going even further your maxId is only used in addTodo(), so you can simply replace it with todos.length + 1 ;)

@smalluban
Copy link
Contributor

Feel free to re-open, if you will have other problems with the subject.

@ericktucto
Copy link
Author

ericktucto commented Nov 23, 2019

Thanks for the Hybrids library, I already modified my code. I'm doing a PWA of Notes only with the library (it's not finished yet), in the following link you can see it. click here (sorry for the spanish)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants