|
1 | | -import noop from 'lodash/noop'; |
2 | 1 | import normalizeProps from './normalizeProps'; |
3 | 2 |
|
4 | | -export default function connect(mapStateAsProps = noop, mapActionsAsProps = noop) { |
5 | | - return (children) => { |
6 | | - const props = children.props || {}; |
7 | | - const subscriptions = children.collect || {}; |
| 3 | +function noop() { |
| 4 | +} |
8 | 5 |
|
9 | | - const allProps = { |
10 | | - ...normalizeProps(props), |
11 | | - ...normalizeProps(subscriptions) |
12 | | - }; |
| 6 | +function getStore(component) { |
| 7 | + return component.$store; |
| 8 | +} |
| 9 | + |
| 10 | +function getAttrs(component) { |
| 11 | + return component.$parent._vnode.data.attrs; |
| 12 | +} |
| 13 | + |
| 14 | +function getStates(component, mapStateToProps) { |
| 15 | + const store = getStore(component); |
| 16 | + const attrs = getAttrs(component); |
| 17 | + |
| 18 | + return mapStateToProps(store.getState(), attrs) || {}; |
| 19 | +} |
| 20 | + |
| 21 | +function getActions(component, mapActionsToProps) { |
| 22 | + const store = getStore(component); |
| 23 | + |
| 24 | + return mapActionsToProps(store.dispatch, getAttrs.bind(null, component)) || {}; |
| 25 | +} |
| 26 | + |
| 27 | +function getProps(component) { |
| 28 | + let props = {}; |
| 29 | + const attrs = getAttrs(component); |
| 30 | + const stateNames = component.vuaReduxStateNames; |
| 31 | + const actionNames = component.vuaReduxActionNames; |
| 32 | + |
| 33 | + for (let ii = 0; ii < stateNames.length; ii++) { |
| 34 | + props[stateNames[ii]] = component[stateNames[ii]]; |
| 35 | + } |
13 | 36 |
|
14 | | - children.props = allProps; |
15 | | - delete children.collect; |
| 37 | + for (let ii = 0; ii < actionNames.length; ii++) { |
| 38 | + props[actionNames[ii]] = component[actionNames[ii]]; |
| 39 | + } |
| 40 | + |
| 41 | + return { |
| 42 | + ...props, |
| 43 | + ...attrs |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * 1. utilities are moved above because vue stores methods, states and props |
| 49 | + * in the same namespace |
| 50 | + * 2. actions are set while created |
| 51 | + */ |
| 52 | + |
| 53 | +/** |
| 54 | + * @param mapStateToProps |
| 55 | + * @param mapActionsToProps |
| 56 | + * @returns Object |
| 57 | + */ |
| 58 | +export default function connect(mapStateToProps = noop, mapActionsToProps = noop) { |
| 59 | + return (children) => { |
| 60 | + |
| 61 | + /** @namespace children.collect */ |
| 62 | + if (children.collect) { |
| 63 | + console.warn('vua-redux: collect is deprecated, use props'); |
| 64 | + children.props = { |
| 65 | + ...normalizeProps(children.props || {}), |
| 66 | + ...normalizeProps(children.collect || {}) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + name: `ConnectVuaRedux-${children.name || 'children'}`, |
| 72 | + |
| 73 | + render(h) { |
| 74 | + const props = getProps(this); |
| 75 | + |
| 76 | + return h(children, { props }); |
| 77 | + }, |
| 78 | + |
| 79 | + data() { |
| 80 | + const state = getStates(this, mapStateToProps); |
| 81 | + const actions = getActions(this, mapActionsToProps); |
| 82 | + const stateNames = Object.keys(state); |
| 83 | + const actionNames = Object.keys(actions); |
16 | 84 |
|
17 | 85 | return { |
18 | | - name: 'VuaRedux', |
19 | | - |
20 | | - props: props, |
21 | | - |
22 | | - render(h) { |
23 | | - const keys = Object.keys(allProps); |
24 | | - let propsToPass = {}; |
25 | | - for (let i = 0; i < keys.length; i++) { |
26 | | - propsToPass[keys[i]] = this[keys[i]]; |
27 | | - } |
28 | | - |
29 | | - return h(children, { |
30 | | - props: propsToPass |
31 | | - }) |
32 | | - }, |
33 | | - |
34 | | - data() { |
35 | | - const store = this.$store; |
36 | | - const state = mapStateAsProps(store.getState()) || {}; |
37 | | - const actions = mapActionsAsProps(store.dispatch, store.getState) || {}; |
38 | | - |
39 | | - return { |
40 | | - ...state, |
41 | | - ...actions |
42 | | - }; |
43 | | - }, |
44 | | - |
45 | | - created() { |
46 | | - const store = this.$store; |
47 | | - const state = mapStateAsProps(store.getState()) || {}; |
48 | | - const stateNames = Object.keys(state); |
49 | | - |
50 | | - this.unsubscribe = store.subscribe(() => { |
51 | | - const state = mapStateAsProps(store.getState()); |
52 | | - |
53 | | - for (let i = 0; i < stateNames.length; i++) { |
54 | | - this[stateNames[i]] = state[stateNames[i]]; |
55 | | - } |
56 | | - }); |
57 | | - }, |
58 | | - |
59 | | - beforeDestroy() { |
60 | | - this.unsubscribe(); |
61 | | - } |
| 86 | + ...state, |
| 87 | + ...actions, |
| 88 | + vuaReduxStateNames: stateNames, |
| 89 | + vuaReduxActionNames: actionNames |
62 | 90 | }; |
| 91 | + }, |
| 92 | + |
| 93 | + created() { |
| 94 | + const store = getStore(this); |
| 95 | + |
| 96 | + this.vuaReduxUnsubscribe = store.subscribe(() => { |
| 97 | + const state = getStates(this, mapStateToProps); |
| 98 | + const stateNames = Object.keys(state); |
| 99 | + this.vuaReduxStateNames = stateNames; |
| 100 | + |
| 101 | + for (let ii = 0; ii < stateNames.length; ii++) { |
| 102 | + this[stateNames[ii]] = state[stateNames[ii]]; |
| 103 | + } |
| 104 | + }); |
| 105 | + }, |
| 106 | + |
| 107 | + beforeDestroy() { |
| 108 | + this.vuaReduxUnsubscribe(); |
| 109 | + } |
63 | 110 | }; |
| 111 | + }; |
64 | 112 | } |
0 commit comments