ng-turkey / ngxs-reset-plugin

Reset plugin for NGXS: Effortlessly clears, resets, or overwrites NGXS states respecting the state tree.
https://stackblitz.com/edit/ngxs-pizza
MIT License
108 stars 8 forks source link

reset state does not reset Map() filed in defaults #85

Closed ubaidazad closed 10 months ago

ubaidazad commented 1 year ago

StateResetAll reset state to default values but Map objects are not reset Whenever you dispatch StateResetAll it perfectly resets the state but what happens if default state field is Map it is not getting reset to new Map() and old value is returned to listeners leading to unintentional results.

@State<ApplicationSettingsStateModel>({
  name: 'applicationSettings',
  defaults: {
    settings: new Map(),
    settingsDict: {},
  },
})
@Injectable()
export class ApplicationSettingsState {
}

here settings in defaults is not reset to new Map() but settingsDict is set to {}

armanozak commented 1 year ago

NGXS recommends avoiding class instances in your state because your state should be immutable and serializable/deserializable. Nevertheless, if you don't worry about serialization and still want to use a Map, you must make sure your state changes are immutable. In other words, you should create a new Map instance any time you want to update your state. I forked our pizza example to show you how it works.

https://stackblitz.com/edit/ngxs-pizza-with-map?file=src%2Fapp%2Fpizza%2Fpizza.state.ts,src%2Fapp%2Forder%2Forder.handler.ts

If you check line 55 in the pizza.state.ts file, you'll see a new Map instance is created with the current state and the state is updated with it. The order.handler.ts file proves this works with StateResetAll.

Please let us know if your question is answered.

gokhanipek commented 10 months ago

Condisering the issue is open for 5 months and no follow up, maybe we can close the issue?

armanozak commented 10 months ago

I guess we can. Thanks for reminding @gokhanipek. 👍