From f8b33c98136f834b6ccb3d745a9bb5aa1030f57b Mon Sep 17 00:00:00 2001 From: Marc Neuwirth Date: Wed, 13 Jul 2016 14:47:48 -0400 Subject: [PATCH 1/3] Fix undefined state on prop change --- bower.json | 2 +- package.json | 2 +- src/dependencies/DependencyMap.js | 5 +++-- src/dependencies/StoreDependencyMixin.js | 2 +- src/dependencies/__tests__/DependencyMap-test.js | 10 +++++++++- src/dependencies/connect.js | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/bower.json b/bower.json index ec38e55..21ffb4f 100644 --- a/bower.json +++ b/bower.json @@ -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 " diff --git a/package.json b/package.json index 6f79e42..8e2f8fb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/dependencies/DependencyMap.js b/src/dependencies/DependencyMap.js index cbe6333..d73a3e1 100644 --- a/src/dependencies/DependencyMap.js +++ b/src/dependencies/DependencyMap.js @@ -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) ); } diff --git a/src/dependencies/StoreDependencyMixin.js b/src/dependencies/StoreDependencyMixin.js index 9ef8005..354a103 100644 --- a/src/dependencies/StoreDependencyMixin.js +++ b/src/dependencies/StoreDependencyMixin.js @@ -76,7 +76,7 @@ export default function StoreDependencyMixin( componentWillReceiveProps(nextProps): void { this.setState( - calculateForPropsChange(dependencies, nextProps) + calculateForPropsChange(dependencies, nextProps, this.state) ); }, diff --git a/src/dependencies/__tests__/DependencyMap-test.js b/src/dependencies/__tests__/DependencyMap-test.js index a8adcfd..594dc8d 100644 --- a/src/dependencies/__tests__/DependencyMap-test.js +++ b/src/dependencies/__tests__/DependencyMap-test.js @@ -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, + }, }; }); @@ -190,6 +194,7 @@ describe('DependencyMap', () => { count: initialState, negativeCount: -initialState, timesAHundred: initialState * 100, + timesAHundredMinusState: initialState * 100 - mockState.testState, }); }); }); @@ -226,6 +231,7 @@ describe('DependencyMap', () => { expect(result).toEqual({ absCount: initialState, timesAHundred: initialState * 100, + timesAHundredMinusState: initialState * 100 - mockState.testState, }); }); }); @@ -238,7 +244,8 @@ describe('DependencyMap', () => { mockState ); expect(result).toEqual({ - timesAHundred: initialState * 100 + timesAHundred: initialState * 100, + timesAHundredMinusState: initialState * 100 - mockState.testState, }); }); }); @@ -251,6 +258,7 @@ describe('DependencyMap', () => { negativeCount: true, absCount: true, timesAHundred: true, + timesAHundredMinusState: true, }); }); diff --git a/src/dependencies/connect.js b/src/dependencies/connect.js index b4ce268..8b7cc37 100644 --- a/src/dependencies/connect.js +++ b/src/dependencies/connect.js @@ -56,7 +56,7 @@ export default function connect( componentWillReceiveProps(nextProps: Object): void { this.setState( - calculateForPropsChange(dependencies, nextProps) + calculateForPropsChange(dependencies, nextProps, this.state) ); } From 0560b4e353dbf72702bc7655dedd93466e2a02dd Mon Sep 17 00:00:00 2001 From: Marc Neuwirth Date: Wed, 13 Jul 2016 17:48:08 -0400 Subject: [PATCH 2/3] Only pass state through if it is needed --- src/dependencies/DependencyMap.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dependencies/DependencyMap.js b/src/dependencies/DependencyMap.js index d73a3e1..33c6c38 100644 --- a/src/dependencies/DependencyMap.js +++ b/src/dependencies/DependencyMap.js @@ -141,7 +141,12 @@ export function calculateForPropsChange( return oFilterMap( dependencies, (dep) => dep.deref && dep.deref.length > 0, - (dep) => calculate(dep, props, state) + (dep) => { + if (dep.deref.length === 1) { + return calculate(dep, props); + } + return calculate(dep, props, state); + } ); } From 8906900a2138c0a4c2ea657bca487c2d7f6bb1f1 Mon Sep 17 00:00:00 2001 From: Colby Rabideau Date: Thu, 14 Jul 2016 08:55:20 -0400 Subject: [PATCH 3/3] whoops, calculate does the arity check --- src/dependencies/DependencyMap.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/dependencies/DependencyMap.js b/src/dependencies/DependencyMap.js index 33c6c38..d73a3e1 100644 --- a/src/dependencies/DependencyMap.js +++ b/src/dependencies/DependencyMap.js @@ -141,12 +141,7 @@ export function calculateForPropsChange( return oFilterMap( dependencies, (dep) => dep.deref && dep.deref.length > 0, - (dep) => { - if (dep.deref.length === 1) { - return calculate(dep, props); - } - return calculate(dep, props, state); - } + (dep) => calculate(dep, props, state) ); }