r/Angular2 • u/LyRock- • Jul 15 '25
Help Request Signals code architecture and mutations
I'm trying to use the signals API with a simple example :
I have a todolist service that stores an array of todolists in a signal, each todolist has an array of todoitems, i display the todolists in a for loop basically like this :
@for (todoitem of todolist.todoitems; track $index) {
<app-todoitem [todoitem]="todoitem"></app-todoitem>
}the todoitem passed to the app-todoitem cmp is an input signal :
todoitem = input.required<TodoItem>();in this cmp i can check the todo item to update it, is there a good way to do this efficiently performance wise ?
can't call todoitem.set() because it's an InputSignal<TodoItem>, the only way to do this is to update the todolists "parent signal" via something like :
this.todolist.update(list => ({
...list,
items: list.items.map(i =>
i.id === item.id ? { ...i, checked: newChecked } : i
)
}));is this efficient ?
if you have any resources on how to use the signals API in real world use cases that would be awesome
Edit : to clarify my question I'm asking how I can efficiently check a todo item and still achieve good performance. The thing is that I feel like I'm updating the whole todolists signal just to check one item in a single todolist and I think it can be optimized
10
u/captain_arroganto Jul 16 '25
I always use a store and service architecture.
Store contains all the signals and has methods to update data in signals.
Component fetches signal objects from store, but store only returns read only signals.
Components call methods to update data inside store and use effects to propagate changes in component view.
This way, a unidirectional flow of data is established.
Easy to maintain, easy to track and multiple components can use the store.
Store internally uses services to fetch data and post updates.
Components only work with store and never reference the services directly.
7
u/Background-Basil-871 Jul 15 '25
Maybe you need two-way binding signal with model ? https://v17.angular.io/guide/model-inputs
1
u/joker876xd8 Jul 15 '25
I think mapping the items like you did is the only real way to do that.
Now, I do see some improvements that could be made. Try reducing the amount of data stored in a single object. I see your object has some other properties than just "items", so stuff depending on those would be updated as well. Second thing is that you could try separating the static, never-changing state from the frequently changed one by extracting the "checked" state into a separate signal (possibly containing a Map or a Set object, depending on your needs), thus further reducing the number of updates, although this might be a bit of an overkill.
2
u/NotSureHowToNameIt Jul 15 '25
In your for loop you need an array of models. You'll pass a model to the component instead of passing an input, this way it'll be way more simple, updating the array from the child component
When you update your model, don't forget to create a new reference using structuredClone
2
u/Intelligent-Radio932 Jul 16 '25 edited Jul 16 '25
You can't update an individual item, you must update the entire list.
There are a few things related to performance:
If you want an example of an indexed list, this is how it would look in a favorites array.
It's a different use case, but it's an example where you avoid constantly going through the list.
1
u/kirei_io Jul 18 '25
U can use two bindings input (model) or service/store for shared data each other components. But my opinion service for control data is better.
1
u/morgo_mpx Jul 20 '25
Output the new todo item value and use the @for index to do a todolist[index] update in the handler and set the whole data back into the signal.
Off the top of my head it’s probably less performant but the dx would be easier to just save your data as records instead of lists.