Skip to content

Commit

Permalink
Fixes nested computed properties issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Jun 24, 2019
1 parent b6778b1 commit 29ec92a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/__tests__/issue217.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { action, computed, createStore } from '../index';

test('issue217', () => {
const model = {
items: {},
items: {
1: 'foo',
},

nested: {
numbers: [1, 2, 3],
Expand All @@ -16,18 +18,17 @@ test('issue217', () => {

// actions
fetched: action((state, payload) => {
state.items = payload.reduce((acc, todo) => {
acc[todo.id] = todo;
return acc;
}, {});
state.nested.numbers = payload;
state.items['1'] = 'bar';
}),
};

const store = createStore(model);

// act
store.getActions().fetched([{ id: 1, text: 'foo' }]);
store.getActions().fetched([4, 5, 6]);

// assert
expect(store.getState().nested.filteredNumbers).toEqual([2, 3]);
expect(store.getState().nested.filteredNumbers).toEqual([4, 5, 6]);
expect(store.getState().list).toEqual(['bar']);
});
20 changes: 14 additions & 6 deletions src/create-store-internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,20 @@ export default function createStoreInternals({
if (updatedState !== state) {
const computedPropertyCreators = get(path, computedProperties);
if (computedPropertyCreators) {
const updatedCurrent = get(path, updatedState);
Object.keys(computedPropertyCreators).forEach(key => {
if (typeof computedPropertyCreators[key] === 'function') {
computedPropertyCreators[key](updatedCurrent);
}
});
const recursiveRebindComputedProperties = (currentPath, obj) => {
const updatedCurrent = get(currentPath, updatedState);
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'function') {
obj[key](updatedCurrent);
} else {
recursiveRebindComputedProperties(
[...currentPath, key],
obj[key],
);
}
});
};
recursiveRebindComputedProperties(path, computedPropertyCreators);
}
}

Expand Down

0 comments on commit 29ec92a

Please sign in to comment.