How to watch for changes of entities in a withEntities() ngrx signal store?

Discussão em 'Angular' iniciado por Peter T., Outubro 23, 2024.

  1. Peter T.

    Peter T. Guest

    Scenario:

    I use a signalStore(withEntities<Entity>() /*...*/).

    I have a use case when I want to trigger some other updates whenever an entity in the store is added, updated or removed.

    I'd like to use an effect() - but I miss an simple & elegant solution to find out about what changed since last call of the effect function.

    I even don't get the previous store state (like in Vue's watch()) so I could compare myself.

    Any suggestion?

    Ideally, I'd like to have something like

    watchEntities(store, (updated: EntityId[], added: EntityId[], removed: EntityId[]) => {
    // called as effect whenever things change in the store so I can
    // do whatever I need to keep other things in sync with these entities
    };


    AI suggested to do something like:

    toObservable(store).pipe(
    startWith(null),
    pairwise()
    ).subscribe(([prev, current]) => {
    if (prev === null) return; // Skip initial emission
    // ...and an awful lot of code to detect the changes...


    So I'd say it's simple to augment my modification methods to trigger the actions.

    Continue reading...

Compartilhe esta Página