Skip to content

Commit c3d2674

Browse files
committed
First draft of new api, not functional
1 parent 2a9c239 commit c3d2674

File tree

6 files changed

+81
-28
lines changed

6 files changed

+81
-28
lines changed

examples/counter/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {bind} from 'angular2/di';
33
import {createStore, applyMiddleware} from 'redux';
44
import thunk from 'redux-thunk';
55
import {App} from './containers/App';
6-
import {ngRedux} from 'ng2-redux';
6+
import ngRedux from 'ng2-redux';
77
import {rootReducer} from './reducers';
88

99
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
1010
const store = createStoreWithMiddleware(rootReducer);
1111

12+
console.log(ngRedux);
13+
1214
bootstrap(
1315
App,
14-
[bind('ngRedux').toFactory(() => {
15-
return new ngRedux(store);
16-
})]
16+
[ngRedux(store)]
1717
);

examples/counter/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path');
22
var webpack = require('webpack');
33

44
module.exports = {
5-
devtool: 'eval',
5+
devtool: 'source-map',
66
entry: [
77
// Angular 2 Deps
88
'zone.js',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"redux": "^1.0.0 || 1.0.0-alpha || 1.0.0-rc"
3434
},
3535
"dependencies": {
36+
"angular2": "^2.0.0-alpha.30",
3637
"invariant": "^2.1.0",
3738
"lodash": "^3.10.1"
3839
}

src/connector.js

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,79 @@
11
import shallowEqual from './utils/shallowEqual';
2+
import wrapActionCreators from './utils/wrapActionCreators';
3+
import invariant from 'invariant';
4+
import _ from 'lodash';
25

36
export default class Connector {
47

58
constructor(store) {
6-
this.store = store;
7-
this.unsubscribe = undefined;
9+
this._store = store;
10+
this._defaultMapStateToTarget = () => ({});
11+
this._defaultMapDispatchToTarget = dispatch => ({dispatch});
812
}
913

10-
disconnect() {
11-
this.unsubscribe();
12-
}
14+
connect(mapStateToTarget, mapDispatchToTarget) {
1315

14-
connect(selectors, callback, disableCaching = false) {
15-
if (!Array.isArray(selectors)) {
16-
selectors = [selectors];
17-
}
16+
const finalMapStateToTarget = mapStateToTarget || this._defaultMapStateToTarget;
17+
18+
const finalMapDispatchToTarget = _.isPlainObject(mapDispatchToTarget) ?
19+
wrapActionCreators(mapDispatchToTarget) :
20+
mapDispatchToTarget || this._defaultMapDispatchToTarget;
21+
22+
invariant(
23+
_.isFunction(finalMapStateToTarget),
24+
'mapStateToTarget must be a Function. Instead received $s.', finalMapStateToTarget
25+
);
26+
27+
invariant(
28+
_.isPlainObject(finalMapDispatchToTarget) || _.isFunction(finalMapDispatchToTarget),
29+
'mapDispatchToTarget must be a plain Object or a Function. Instead received $s.', finalMapDispatchToTarget
30+
);
31+
32+
let slice = this.getStateSlice(this._store.getState(), finalMapStateToTarget);
33+
34+
const boundActionCreators = finalMapDispatchToTarget(this._store.dispatch);
1835

19-
//Initial update
20-
let params = selectors.map(selector => selector(this.store.getState()));
21-
callback(...params);
36+
return (target) => {
2237

23-
this.unsubscribe = this.store.subscribe(() => {
24-
let nextParams = selectors.map(selector => selector(this.store.getState()));
25-
if (disableCaching || !shallowEqual(params, nextParams)) {
26-
callback(...nextParams);
27-
params = nextParams;
28-
}
29-
});
38+
invariant(
39+
_.isFunction(target) || _.isObject(target),
40+
'The target parameter passed to connect must be a Function or a plain object.'
41+
);
42+
43+
//Initial update
44+
this.updateTarget(target, slice, boundActionCreators);
45+
46+
const unsubscribe = this._store.subscribe(() => {
47+
const nextSlice = this.getStateSlice(this._store.getState(), finalMapStateToTarget);
48+
if (!shallowEqual(slice, nextSlice)) {
49+
slice = nextSlice;
50+
this.updateTarget(target, slice, boundActionCreators);
51+
}
52+
});
53+
return unsubscribe;
54+
}
3055

31-
return this;
3256
}
3357

34-
getStore() {
35-
return this.store;
58+
59+
updateTarget(target, StateSlice, dispatch) {
60+
if(_.isFunction(target)) {
61+
target(StateSlice, dispatch);
62+
} else {
63+
_.assign(target, StateSlice, dispatch);
3664
}
65+
}
66+
67+
getStateSlice(state, mapStateToScope) {
68+
const slice = mapStateToScope(state);
69+
70+
invariant(
71+
_.isPlainObject(slice),
72+
'`mapStateToScope` must return an object. Instead received %s.',
73+
slice
74+
);
75+
76+
return slice;
77+
}
3778

3879
}

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
export {default as ngRedux} from './connector';
1+
import Connector from './connector';
2+
import {bind} from 'angular2/di';
3+
4+
export default function createRedux(store) {
5+
const _connector = new Connector(store);
6+
7+
return bind('ngRedux').toFactory(() => {
8+
return {connect: _connector.connect, ...store};
9+
});
10+
}
11+
12+

0 commit comments

Comments
 (0)