Skip to content

Commit f9494cc

Browse files
authored
Merge pull request #171 from AKolodeev/feat/provide-prev-state-slice
feat(connector): provide prev state slice to target (function)
2 parents 8474cc0 + 564daa9 commit f9494cc

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Diff for: src/components/connector.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export default function Connector(store) {
5252
const unsubscribe = store.subscribe(() => {
5353
const nextSlice = getStateSlice(store.getState(), finalMapStateToTarget);
5454
if (!shallowEqual(slice, nextSlice)) {
55+
updateTarget(target, nextSlice, boundActionCreators, slice);
5556
slice = nextSlice;
56-
updateTarget(target, slice, boundActionCreators);
5757
}
5858
});
5959
return unsubscribe;
@@ -62,9 +62,9 @@ export default function Connector(store) {
6262
}
6363
}
6464

65-
function updateTarget(target, StateSlice, dispatch) {
65+
function updateTarget(target, StateSlice, dispatch, prevStateSlice) {
6666
if(isFunction(target)) {
67-
target(StateSlice, dispatch);
67+
target(StateSlice, dispatch, prevStateSlice);
6868
} else {
6969
assign(target, StateSlice, dispatch);
7070
}

Diff for: test/components/connector.spec.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ describe('Connector', () => {
1818
baz: -1
1919
};
2020
store = createStore((state = defaultState, action) => {
21-
return assign({}, state, { baz: action.payload });
21+
switch (action.type) {
22+
case 'ACTION':
23+
return assign({}, state, { baz: action.payload });
24+
default:
25+
return state;
26+
}
2227
});
2328
targetObj = {};
2429
connect = Connector(store);
@@ -109,4 +114,20 @@ describe('Connector', () => {
109114
expect(receivedDispatch).toBe(store.dispatch);
110115
});
111116

117+
it('Should provide state slice, bound actions and previous state slice to target (function)', () => {
118+
const targetFunc = sinon.spy();
119+
120+
connect(state => state, {})(targetFunc);
121+
122+
expect(targetFunc.calledWith(defaultState, {}, undefined)).toBeTruthy();
123+
124+
store.dispatch({ type: 'ACTION', payload: 2 });
125+
126+
expect(targetFunc.calledWith(
127+
assign({}, defaultState, { baz: 2 }),
128+
{},
129+
defaultState)
130+
).toBeTruthy();
131+
});
132+
112133
});

0 commit comments

Comments
 (0)