Handling collections #5
-
Hi, first of all: fantastic project, always great to see solutions that promote declarative designs. Looking forward to updates to UniMob.UI too. Question tho: do I understand correctly that collections (like Todo[] array) have to be replaced entirely for Atom to detect changes? (So basically it should be be treated as immutable collection) As in this would not work as expected, correct?:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi, Yes that's right. Atom doesn't track object modification. Reaction will be triggered only when new value has been set for atom. However, you can write your own collection type that will trigger reactions when new items are added. [Atom] public AtomList<Todo> Todos { get; set; } = new AtomList<Todo>();
incrementButton.onClick.AddListener(() =>
{
Todos.Add(new Todo());
});
Atom.Reaction(() => counterText.text = "Unifinished: " + UnfinishedTodoCount); public class AtomList<T> : IEnumerable<T>
{
private readonly MutableAtom<List<T>> _list = Atom.Value(new List<T>());
public void Add(T value)
{
_list.Value.Add(value);
_list.Invalidate();
}
public IEnumerator<T> GetEnumerator() => _list.Value.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
} Here |
Beta Was this translation helpful? Give feedback.
-
Thanks, I see, Invalidate() is the key to custom atom semantics. |
Beta Was this translation helpful? Give feedback.
-
Hey, sorry for reviving quite old thread but Invalidate() does nto seem to trigger Reactions for me, my AtomList implementation: calling Add() or Remove() does not trigger the Reacton |
Beta Was this translation helpful? Give feedback.
Hi,
Yes that's right. Atom doesn't track object modification. Reaction will be triggered only when new value has been set for atom.
However, you can write your own collection type that will trigger reactions when new items are added.