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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NGRX component store selector by parameter does not work when it is created late #2

Open
milosmih opened this issue Sep 30, 2021 · 0 comments

Comments

@milosmih
Copy link

I am using Angular 12 and ngrx store 12.1.0. Whole question is described here:
https://stackoverflow.com/questions/69220483/ngrx-component-store-selector-by-parameter-does-not-always-fire

I will repeat the text:

I have an objects map structure that I keep in the component store:

{
   id1: {
    name: param1,
    value: 45
   },
   id2: {
    name: param2,
    value: 567
   }
}

I have selector:

objectsMap$: Observable<ObjectsMapModel> = this.select(
    (state) => state.objectsMap
);

and this works perfectly, fire every time I changed the structure of the whole map.

readonly setObjectMap = this.updater((state: MainStore, data: ObjectMapModel) => {
    return {
        ...state,
        objectMap: data
    };
});

I also have an updater which change the value of a single object:

 readonly setSingleObjectValue = this.updater((state: MainStore, data: ObjectModel) => {
    const objectMapWithNewValue = state.objectMap[data.id] = {...data}
    return {
        ...state,
        objectMap: objectMapWithNewValue 
    };
});

Now I need a selector per single object in the object map. I have a lot of objects here and it is not possible to create selectors one by one. For this purpose I found 3 ways:

  1. The solution which does not work and I do not understand why.

objectById(id: string): Observable {

     return this.select(state => {
         // method never enter this line
         return state.objectMap[id]
     })
 }
 objectById$ = this.objectById;
  1. This solution works, but my reducer first needs to change the whole objectMap structure to fire objectMap selector first and after that change the reference/update value of my object. But changing the reference of the whole objectMap structure is not ok for me, I do not want that.
objectById2(id: string): Observable<ObjectModel> {
     return this.select(this.objectsMap$,
     (objectsMap) => {
         return objectsMap[id];
     }
 )}
 objectById2$ = this.objectById2;
  1. The third solution I did to test, just for one object. It works fine but I do not need it since I want a dynamic method for object subscription by id (as 1) or 2) above).
objectWithId1$: Observable<ObjectModel> = this.select(
    (state) => {
        return state.parameterDescription['id1']
    }
)

2 and 3 works, but I do not need them. I do not understand why solution 1 does not work?

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

No branches or pull requests

1 participant