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

Fix undefined state on prop change #46

Merged
merged 3 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "general-store",
"version": "1.2.1",
"version": "1.2.2",
"homepage": "https://github.com/HubSpot/general-store",
"authors": [
"Colby Rabideau <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "general-store",
"version": "1.2.1",
"version": "1.2.2",
"description": "Simple, flexible store implementation for Flux.",
"main": "lib/GeneralStore.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/dependencies/DependencyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ export function calculateForDispatch(

export function calculateForPropsChange(
dependencies: DependencyMap,
props: Object
props: Object,
state: ?Object
): Object {
return oFilterMap(
dependencies,
(dep) => dep.deref && dep.deref.length > 0,
(dep) => calculate(dep, props)
(dep) => calculate(dep, props, state)
Copy link
Contributor

@colbyr colbyr Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm so I think the only thing I would change is that, if the the deref doesn't declare a state argument (i.e. the argument at index 1) it shouldn't receive it.

return oFilterMap(
  ...,
  (dep) => {
    if (dep.deref.length === 1) {
      return caluclate(dep, props);
    }
    return calculate(dep, props, state);
  }
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcneuwirth sorry I was thinking too hard... caclulate does the arity check, it was good the way you had it. Changed it back 8906900.

);
}

Expand Down
2 changes: 1 addition & 1 deletion src/dependencies/StoreDependencyMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function StoreDependencyMixin(

componentWillReceiveProps(nextProps): void {
this.setState(
calculateForPropsChange(dependencies, nextProps)
calculateForPropsChange(dependencies, nextProps, this.state)
);
},

Expand Down
10 changes: 9 additions & 1 deletion src/dependencies/__tests__/DependencyMap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('DependencyMap', () => {
stores: [CountStore],
deref: (props, state) => CountStore.get() * 100,
},
timesAHundredMinusState: {
stores: [CountStore],
deref: (props, state) => CountStore.get() * 100 - state.testState,
},
};
});

Expand Down Expand Up @@ -190,6 +194,7 @@ describe('DependencyMap', () => {
count: initialState,
negativeCount: -initialState,
timesAHundred: initialState * 100,
timesAHundredMinusState: initialState * 100 - mockState.testState,
});
});
});
Expand Down Expand Up @@ -226,6 +231,7 @@ describe('DependencyMap', () => {
expect(result).toEqual({
absCount: initialState,
timesAHundred: initialState * 100,
timesAHundredMinusState: initialState * 100 - mockState.testState,
});
});
});
Expand All @@ -238,7 +244,8 @@ describe('DependencyMap', () => {
mockState
);
expect(result).toEqual({
timesAHundred: initialState * 100
timesAHundred: initialState * 100,
timesAHundredMinusState: initialState * 100 - mockState.testState,
});
});
});
Expand All @@ -251,6 +258,7 @@ describe('DependencyMap', () => {
negativeCount: true,
absCount: true,
timesAHundred: true,
timesAHundredMinusState: true,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/dependencies/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function connect(

componentWillReceiveProps(nextProps: Object): void {
this.setState(
calculateForPropsChange(dependencies, nextProps)
calculateForPropsChange(dependencies, nextProps, this.state)
);
}

Expand Down