redux-vue 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc CHANGED
@@ -1,3 +1,4 @@
1
1
  {
2
- "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
2
+ "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]],
3
+ "plugins": ["@babel/plugin-proposal-export-default-from"]
3
4
  }
package/lib/connect.js ADDED
@@ -0,0 +1,128 @@
1
+ import normalizeProps from './normalizeProps';
2
+ function noop() {}
3
+ function getStore(component) {
4
+ return component.$store;
5
+ }
6
+ function getAttrs(component) {
7
+ // issue #6: guard against missing attrs (root component has no _parentVnode attrs)
8
+ const vnode = component._self.$options._parentVnode;
9
+ return vnode && vnode.data && vnode.data.attrs || {};
10
+ }
11
+ function getStates(component, mapStateToProps) {
12
+ const store = getStore(component);
13
+ const attrs = getAttrs(component);
14
+ return mapStateToProps(store.getState(), attrs) || {};
15
+ }
16
+ function getActions(component, mapActionsToProps) {
17
+ const store = getStore(component);
18
+ return mapActionsToProps(store.dispatch, getAttrs.bind(null, component)) || {};
19
+ }
20
+ function getProps(component) {
21
+ const attrs = getAttrs(component);
22
+ const stateNames = component.vuaReduxStateNames;
23
+ const actionNames = component.vuaReduxActionNames;
24
+ const mergedNames = component.vuaReduxMergedNames;
25
+
26
+ // mergeProps path: only expose merged keys
27
+ if (mergedNames) {
28
+ const props = {};
29
+ for (let ii = 0; ii < mergedNames.length; ii++) {
30
+ props[mergedNames[ii]] = component[mergedNames[ii]];
31
+ }
32
+ return {
33
+ ...props,
34
+ ...attrs
35
+ };
36
+ }
37
+ const props = {};
38
+ for (let ii = 0; ii < stateNames.length; ii++) {
39
+ props[stateNames[ii]] = component[stateNames[ii]];
40
+ }
41
+ for (let ii = 0; ii < actionNames.length; ii++) {
42
+ props[actionNames[ii]] = component[actionNames[ii]];
43
+ }
44
+ return {
45
+ ...props,
46
+ ...attrs
47
+ };
48
+ }
49
+
50
+ /**
51
+ * @param mapStateToProps
52
+ * @param mapActionsToProps
53
+ * @param mergeProps - optional; receives (stateProps, dispatchProps) -> props
54
+ * @returns Function (children) => VueComponentDescriptor
55
+ */
56
+ export default function connect(mapStateToProps, mapActionsToProps, mergeProps) {
57
+ mapStateToProps = mapStateToProps || noop;
58
+ mapActionsToProps = mapActionsToProps || noop;
59
+ return children => {
60
+ /** @namespace children.collect */
61
+ if (children.collect) {
62
+ children.props = {
63
+ ...normalizeProps(children.props || {}),
64
+ ...normalizeProps(children.collect || {})
65
+ };
66
+ const msg = `vua-redux: collect is deprecated, use props ` + `in ${children.name || 'anonymous'} component`;
67
+ console.warn(msg);
68
+ }
69
+ return {
70
+ name: `ConnectVuaRedux-${children.name || 'children'}`,
71
+ render(h) {
72
+ const props = getProps(this);
73
+ // issue #3: forward $slots to child
74
+ return h(children, {
75
+ props,
76
+ slots: this.$slots
77
+ });
78
+ },
79
+ data() {
80
+ const state = getStates(this, mapStateToProps);
81
+ const actions = getActions(this, mapActionsToProps);
82
+ if (mergeProps) {
83
+ // PR #5: mergeProps support
84
+ const merged = mergeProps(state, actions) || {};
85
+ const mergedNames = Object.keys(merged);
86
+ return {
87
+ ...merged,
88
+ vuaReduxMergedNames: mergedNames,
89
+ vuaReduxStateNames: [],
90
+ vuaReduxActionNames: []
91
+ };
92
+ }
93
+ const stateNames = Object.keys(state);
94
+ const actionNames = Object.keys(actions);
95
+ return {
96
+ ...state,
97
+ ...actions,
98
+ vuaReduxStateNames: stateNames,
99
+ vuaReduxActionNames: actionNames
100
+ };
101
+ },
102
+ created() {
103
+ const store = getStore(this);
104
+ this.vuaReduxUnsubscribe = store.subscribe(() => {
105
+ const state = getStates(this, mapStateToProps);
106
+ const actions = getActions(this, mapActionsToProps);
107
+ if (mergeProps) {
108
+ const merged = mergeProps(state, actions) || {};
109
+ const mergedNames = Object.keys(merged);
110
+ this.vuaReduxMergedNames = mergedNames;
111
+ for (let ii = 0; ii < mergedNames.length; ii++) {
112
+ this[mergedNames[ii]] = merged[mergedNames[ii]];
113
+ }
114
+ return;
115
+ }
116
+ const stateNames = Object.keys(state);
117
+ this.vuaReduxStateNames = stateNames;
118
+ for (let ii = 0; ii < stateNames.length; ii++) {
119
+ this[stateNames[ii]] = state[stateNames[ii]];
120
+ }
121
+ });
122
+ },
123
+ beforeDestroy() {
124
+ this.vuaReduxUnsubscribe();
125
+ }
126
+ };
127
+ };
128
+ }
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { default as connect } from './connect';
2
+ export { default as reduxStorePlugin } from './reduxStorePlugin';
@@ -0,0 +1,34 @@
1
+ const isArray = Array.isArray;
2
+ const isPlainObject = v => v !== null && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;
3
+
4
+ // https://github.com/vuejs/vue/blob/dev/src/util/options.js
5
+ export default function normalizeProps(props) {
6
+ var i,
7
+ val,
8
+ normalizedProps = {};
9
+ if (isArray(props)) {
10
+ i = props.length;
11
+ while (i--) {
12
+ val = props[i];
13
+ if (typeof val === 'string') {
14
+ normalizedProps[val] = null;
15
+ } else if (val.name) {
16
+ normalizedProps[val.name] = val;
17
+ }
18
+ }
19
+ } else if (isPlainObject(props)) {
20
+ var keys = Object.keys(props);
21
+ i = keys.length;
22
+ while (i--) {
23
+ let key = keys[i];
24
+ val = props[key];
25
+ normalizedProps[key] = props[key];
26
+ if (typeof val === 'function') {
27
+ normalizedProps[key] = {
28
+ type: val
29
+ };
30
+ }
31
+ }
32
+ }
33
+ return normalizedProps;
34
+ }
@@ -0,0 +1,13 @@
1
+ export default function reduxStorePlugin(Vue, storeId = 'store') {
2
+ Vue.mixin({
3
+ beforeCreate() {
4
+ const options = this.$options;
5
+ // store injection
6
+ if (options[storeId]) {
7
+ this.$store = options.store;
8
+ } else if (options.parent && options.parent.$store) {
9
+ this.$store = options.parent.$store;
10
+ }
11
+ }
12
+ });
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redux-vue",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Vue Redux binding higher order component",
5
5
  "author": "Nadim Tuhin",
6
6
  "repository": {
@@ -26,7 +26,9 @@
26
26
  "immutable"
27
27
  ],
28
28
  "devDependencies": {
29
+ "@babel/cli": "^8.0.1",
29
30
  "@babel/core": "^8.0.1",
31
+ "@babel/plugin-proposal-export-default-from": "^8.0.1",
30
32
  "@babel/preset-env": "^8.0.2",
31
33
  "@babel/register": "^8.0.1",
32
34
  "expect": "^1.20.2",