Skip to content

Commit 5a1d94e

Browse files
committed
Fix state after returning new state object from reduce.
1 parent e1bb8f6 commit 5a1d94e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/store/AltStore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class AltStore {
7272
if (model.reduce) {
7373
handleDispatch(() => {
7474
const value = model.reduce(this.state, payload)
75-
if (value !== undefined) this.state = value
75+
if (value !== undefined) this.state = model.state = value
7676
}, payload)
7777
if (!this.preventDefault) this.emitChange()
7878
}

test/functional-test.js

+19
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,24 @@ export default {
171171
assert(store.reduce(store.state).x === 0)
172172
assert(store2.reduce(store2.state).x === 1)
173173
},
174+
175+
'state returned from reduce is set correctly'() {
176+
const alt = new Alt()
177+
const actions = alt.generateActions('test');
178+
179+
const store = alt.createStore({
180+
displayName: 'store',
181+
182+
state: { x: 0 },
183+
184+
reduce(state) {
185+
assert(state === this.state, 'state matches this.state');
186+
return { ...state };
187+
}
188+
})
189+
190+
actions.test();
191+
actions.test();
192+
},
174193
}
175194
}

0 commit comments

Comments
 (0)