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

withDevTools does not show Map type #104

Closed
Diaaz opened this issue Nov 29, 2024 · 11 comments
Closed

withDevTools does not show Map type #104

Diaaz opened this issue Nov 29, 2024 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@Diaaz
Copy link

Diaaz commented Nov 29, 2024

when using 'updateState(store, "setViewEntityConfig", { viewEntityConfig });' where viewEntityConfig is an object with a Map in it, the Redux tab in Chrome shows:
image

This is what console.log shows:
image

Would it be possible to show the entire objects in de Redux-tab?

Kind regards,

Jeroen.

@rainerhahnekamp
Copy link
Collaborator

@Diaaz, good point, let me check and expect an answer once v19 comes out

@rainerhahnekamp rainerhahnekamp self-assigned this Nov 29, 2024
@rainerhahnekamp rainerhahnekamp added the bug Something isn't working label Nov 29, 2024
@rainerhahnekamp
Copy link
Collaborator

@Diaaz, you can define now a mapper where you map to any structure you want. If that doesn't fix your problem let us know: https://ngrx-toolkit.angulararchitects.io/docs/with-devtools#withmapper

@Diaaz
Copy link
Author

Diaaz commented Feb 14, 2025

@rainerhahnekamp Thanks for the update, but this does not really solve the problem. The idea of updateState is to help developers inspect the values of objects in a store (without needing to write mappers for all those instances). When the standard type 'Map' is used, the log shows an empty node and ignores the content.

Image

The record type is logged correctly, but the Map type is not.

Image

@rainerhahnekamp
Copy link
Collaborator

@Diaaz, you would have to write your own mapper function of course, but I think you want that devtools support the Map type out of the box?

@jdegand
Copy link

jdegand commented Feb 15, 2025

@Diaaz @rainerhahnekamp I don't think this is a bug. Redux DevTools does not serialize non-JSON objects like Map, which leads them to appearing as empty in the log. I think you have to set a serialize flag to true to see inside. The syntax may be close to this:

export const TodoStore = signalStore(
  { providedIn: 'root' },
  withDevtools({
    name: 'todo-store',
    serialize: {
      options: true // Enable serialization for non-JSON objects
    }
  }),
  withEntities<Todo>(),
  withState({
    selectedIds: [] as number[],
  })
);

@rainerhahnekamp
Copy link
Collaborator

@jdegand, thanks yes, I also don't think that Redux DevTools serialize these things. Otherwise it would work.

If I'm not mistaken, there is no serialize option, but a withMapper one:

const Store = signalStore(
  withState(initialState),
  withDevtools(
    'user',
    withMapper((state) => // code to serialize Map in any way you want.
  )
);

@jdegand
Copy link

jdegand commented Feb 16, 2025

This seems to be a common problem across many different JS state solutions. The serialize solution I posted is how it is done in Redux. See this Zustand issue. I would try to parse and stringify the state inside withMapper.

Check this redux-devtools issue as well. NGXS also has problems.

@rainerhahnekamp
Copy link
Collaborator

Yeah, I mean one could build common serialization stuff into this extension. That's not a big deal, but on the other side, a Map should not end up in a signal in the first place.

@jdegand
Copy link

jdegand commented Feb 16, 2025

Yes, it will not work with change detection.

@Diaaz
Copy link
Author

Diaaz commented Feb 17, 2025

Thanks for the info, I will remove Map from all my signal stores.

@Diaaz Diaaz closed this as completed Feb 17, 2025
@rainerhahnekamp
Copy link
Collaborator

Yes, @Diaaz, it is important that you also understand that Map shouldn't be there because of the devtools.

If you have that code:

const data = userStore.personMap();
data.set(1, // data for person);

In this case the personMap is mutated, the Signals is not aware of its change and will therefore not notify any computed, effect, etc. In general, class instances and signals don't go together nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants