Skip to content

Commit f1065ef

Browse files
committed
Enforced $scope + use lodash
1 parent 91b6223 commit f1065ef

File tree

6 files changed

+28
-90
lines changed

6 files changed

+28
-90
lines changed

src/components/connector.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
1-
import isFunction from '../utils/isFunction';
2-
import isPlainObject from '../utils/isPlainObject';
31
import shallowEqual from '../utils/shallowEqual';
42
import invariant from 'invariant';
5-
import _ from 'lodash'
3+
import _ from 'lodash';
64

75
export default function Connector(store, $injector) {
8-
return (selector, target) => {
6+
return (selector, scope) => {
97

10-
invariant(
11-
isPlainObject(target),
12-
'The target parameter passed to connect must be a plain object. Instead received %s.',
13-
typeof target
14-
);
8+
invariant(scope && _.isFunction(scope.$on) && _.isFunction(scope.$destroy), 'The scope parameter passed to connect must be an instance of $scope.');
159

1610
//Initial update
1711
let slice = getStateSlice(store.getState(), selector);
18-
target = _.assign(target, slice);
12+
_.assign(scope, slice);
1913

2014
let unsubscribe = store.subscribe(() => {
2115
let nextSlice = getStateSlice(store.getState(), selector);
2216
if (!shallowEqual(slice, nextSlice)) {
23-
target = _.assign(target, nextSlice);
2417
slice = nextSlice;
18+
_.assign(scope, slice);
2519
}
2620
});
2721

28-
if(isFunction(target.$destroy)) {
29-
target.$on('$destroy', () => {
30-
unsubscribe();
31-
});
32-
}
33-
34-
return unsubscribe;
22+
scope.$on('$destroy', () => {
23+
unsubscribe();
24+
});
3525
}
3626
}
3727

3828
function getStateSlice(state, selector) {
3929
let slice = selector(state);
4030
invariant(
41-
isPlainObject(slice),
31+
_.isPlainObject(slice),
4232
'`selector` must return an object. Instead received %s.',
4333
slice
4434
);

src/components/ngRedux.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Connector from './connector';
22
import invariant from 'invariant';
3-
import isFunction from '../utils/isFunction';
43
import {createStore, applyMiddleware, compose} from 'redux';
54
import digestMiddleware from './digestMiddleware';
5+
import _ from 'lodash';
66

77
export default function ngReduxProvider() {
88
let _reducer = undefined;
@@ -11,13 +11,13 @@ export default function ngReduxProvider() {
1111

1212
this.createStoreWith = (reducer, middlewares, storeEnhancers) => {
1313
invariant(
14-
isFunction(reducer),
14+
_.isFunction(reducer),
1515
'The reducer parameter passed to createStoreWith must be a Function. Instead received %s.',
1616
typeof reducer
1717
);
1818

1919
invariant(
20-
!storeEnhancers || Array.isArray(storeEnhancers),
20+
!storeEnhancers || _.isArray(storeEnhancers),
2121
'The storeEnhancers parameter passed to createStoreWith must be an Array. Instead received %s.',
2222
typeof storeEnhancers
2323
);

src/utils/isFunction.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils/isPlainObject.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/components/connector.spec.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,43 @@ import Connector from '../../src/components/connector';
55
describe('Connector', () => {
66
let store;
77
let connect;
8+
let scopeStub;
89

910
beforeEach(() => {
1011
store = createStore((state, action) => ({
11-
foo: 'bar',
12-
baz: action.payload,
13-
anotherState: 12,
14-
childObject: {child: true}
12+
foo: 'bar',
13+
baz: action.payload,
14+
anotherState: 12,
15+
childObject: {child: true}
1516
}));
17+
scopeStub = { $on: () => {}, $destroy: () => {}};
1618
connect = Connector(store);
1719
});
1820

19-
it('Should throw when not passed a plain object as target', () => {
21+
it('Should throw when not passed a $scope object as target', () => {
2022
expect(connect.bind(connect, () => ({}), () => {})).toThrow();
2123
expect(connect.bind(connect, () => ({}), 15)).toThrow();
2224
expect(connect.bind(connect, () => ({}), undefined)).toThrow();
23-
expect(connect.bind(connect, () => ({}), {})).toNotThrow();
25+
expect(connect.bind(connect, () => ({}), {})).toThrow();
26+
27+
expect(connect.bind(connect, () => ({}), scopeStub)).toNotThrow();
2428
});
2529

2630
it('Should throw when selector does not return a plain object as target', () => {
27-
expect(connect.bind(connect, state => state.foo, {})).toThrow();
31+
expect(connect.bind(connect, state => state.foo, scopeStub)).toThrow();
2832
});
2933

30-
3134
it('target should be extended with state once directly after creation', () => {
32-
let target = {};
33-
connect(() => ({test: 1}), target);
34-
expect(target).toEqual({test: 1});
35+
connect(() => ({vm : {test: 1}}), scopeStub);
36+
expect(scopeStub.vm).toEqual({test: 1});
3537
});
3638

3739
it('Should update the target passed to connect when the store updates', () => {
38-
let target = {};
39-
connect(state => state, target);
40+
connect(state => state, scopeStub);
4041
store.dispatch({type: 'ACTION', payload: 0});
41-
expect(target.baz).toBe(0);
42+
expect(scopeStub.baz).toBe(0);
4243
store.dispatch({type: 'ACTION', payload: 1});
43-
expect(target.baz).toBe(1);
44+
expect(scopeStub.baz).toBe(1);
4445
});
4546

4647
//does that still makes sense?
@@ -53,15 +54,4 @@ describe('Connector', () => {
5354
store.dispatch({type: 'ACTION', payload: 1});
5455
expect(counter).toBe(3);
5556
});*/
56-
57-
58-
it('Should return an unsubscribing function', () => {
59-
let target = {};
60-
let unsubscribe = connect(state => state, target);
61-
store.dispatch({type: 'ACTION', payload: 1});
62-
expect(target.baz).toBe(1);
63-
unsubscribe();
64-
store.dispatch({type: 'ACTION', payload: 2});
65-
expect(target.baz).toBe(1);
66-
});
6757
});

test/utils/isFunction.spec.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)