Given Situation:
In my coding practice, I employ a
signalStore(withEntities<Entity>() /*...*/)
.
There are instances where I need to initiate additional updates whenever an entity in the store undergoes addition, modification or deletion.
I am interested in utilizing an effect()
in this scenario, but I am yet to find a straightforward and graceful way to identify what changed since the last invocation of the effect function.
Furthermore, I do not have access to the previous state of the store (similar to the functionality provided by Vue's watch()
) for comparison purposes.
Any suggestions on how to approach this challenge effectively?
My preference would be to implement something along the lines of
watchEntities(store, (updated: EntityId[], added: EntityId[], removed: EntityId[]) => {
// This function is called like an effect each time there is a change in the store, allowing me to synchronize other components with these entities as needed
};
An AI recommended a solution involving:
toObservable(store).pipe(
startWith(null),
pairwise()
).subscribe(([prev, current]) => {
if (prev === null) return; // Skip initial emission
// ... which involves quite a bit of code to identify the changes ...
Therefore, it seems reasonable to enhance my methods of modification in order to trigger the necessary actions automatically.