Store Update - merges current and previous state values. #661
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[x] Support request
[ ] Other... Please describe:
Current behavior
When calling store.update only the specified properties are being replaced.
If I have two possible states I want to represent in the Todos store
export type TodosState = TodosBasicState | TodosAltState;
interface TodosBasicState {
_tag: "todos-basic-state";
uniqueToBasicObject: {
info: string;
};
basicInfo: string;
}
interface TodosAltState {
_tag: "todos-alt-state";
altInfo: string;
}
I intialize my todos state to a TodosBasicState
const initialState: TodosState = {
_tag: "todos-basic-state",
uniqueToBasicObject: { info: "test" },
basicInfo: "Basic Info"
};
When I update the state using the following method I get a polluted state object that is a combination of the old state and the new values.
this.todosStore.update((state) => ({
_tag: "todos-alt-state",
altInfo: "This is some alt info.",
}));
State object as json before update:
{
"_tag": "todos-basic-state",
"uniqueToBasicObject": {
"info": "test"
},
"basicInfo": "Basic Info"
}
State object as json after update:
{
"_tag": "todos-alt-state",
"uniqueToBasicObject": {
"info": "test"
},
"basicInfo": "Basic Info",
"altInfo": "This is some alt info."
}
As you can see the the _tag and altInfo properties have been updated as expected but the rest of the state has not been removed from the previously.
The following line is the cause in store.ts. The current and new state are both being combined in a spread operation. I'm sure this is being done for a reason I'm ignorant of but is this side effect intentional?
Expected behavior
I would expect that when calling the update method I get only the specified properties in the state and all old properites are dropped.
Expect this:
this.todosStore.update((state) => ({
_tag: "todos-alt-state",
altInfo: "This is some alt info.",
}));
to turn state from:
{
"_tag": "todos-basic-state",
"uniqueToBasicObject": {
"info": "test"
},
"basicInfo": "Basic Info"
}
to this:
{
"_tag": "todos-alt-state",
"altInfo": "This is some alt info."
}
If I wanted to keep values from the previous state I would expect to have to include them in the update callback using a spread or explicitly.
e.g.
this.todosStore.update(state => ({
...state,
_tag: "todos-alt-state",
altInfo: "This is some alt info."
}));
Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/akita-todos-app-pyf2xv?file=src/app/todos/state/todos.query.ts
Click update to perform the above update method and click reset to reset the store back to the initial method.
What is the motivation / use case for changing the behavior?
I want to be able to update the state without previous state values polluting the new state. If there is a better way to achieve this any advise is welcome?
Environment
Angular version: X.Y.Z
Browser:
- [ x] Chrome (desktop) version XX
- [ x] Chrome (Android) version XX
- [ x] Chrome (iOS) version XX
- [ x] Firefox version XX
- [ x] Safari (desktop) version XX
- [ x] Safari (iOS) version XX
- [ x] IE version XX
- [ x] Edge version XX