UNPKG

4.06 kBJavaScriptView Raw
1'use strict';
2
3var babelHelpers = {};
4
5babelHelpers.classCallCheck = function (instance, Constructor) {
6 if (!(instance instanceof Constructor)) {
7 throw new TypeError("Cannot call a class as a function");
8 }
9};
10
11babelHelpers.createClass = function () {
12 function defineProperties(target, props) {
13 for (var i = 0; i < props.length; i++) {
14 var descriptor = props[i];
15 descriptor.enumerable = descriptor.enumerable || false;
16 descriptor.configurable = true;
17 if ("value" in descriptor) descriptor.writable = true;
18 Object.defineProperty(target, descriptor.key, descriptor);
19 }
20 }
21
22 return function (Constructor, protoProps, staticProps) {
23 if (protoProps) defineProperties(Constructor.prototype, protoProps);
24 if (staticProps) defineProperties(Constructor, staticProps);
25 return Constructor;
26 };
27}();
28
29babelHelpers.slicedToArray = function () {
30 function sliceIterator(arr, i) {
31 var _arr = [];
32 var _n = true;
33 var _d = false;
34 var _e = undefined;
35
36 try {
37 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
38 _arr.push(_s.value);
39
40 if (i && _arr.length === i) break;
41 }
42 } catch (err) {
43 _d = true;
44 _e = err;
45 } finally {
46 try {
47 if (!_n && _i["return"]) _i["return"]();
48 } finally {
49 if (_d) throw _e;
50 }
51 }
52
53 return _arr;
54 }
55
56 return function (arr, i) {
57 if (Array.isArray(arr)) {
58 return arr;
59 } else if (Symbol.iterator in Object(arr)) {
60 return sliceIterator(arr, i);
61 } else {
62 throw new TypeError("Invalid attempt to destructure non-iterable instance");
63 }
64 };
65}();
66
67babelHelpers;
68
69// to valid and match like `a.b.c as x.y.z`
70var re = /^([a-zA-Z0-9_-]+)\s{1,2}as\s{1,2}([a-zA-Z0-9\._-]+)$/i;
71
72var isDev = process.env.NODE_ENV !== 'production';
73
74/**
75 * Bind reduxStore to Vue instance
76 *
77 * @param {Vue} Vue
78 * @param {object} store - redux store
79 */
80function bindVue(Vue, store) {
81 Vue.prototype.$subscribe = function () {
82 var _this = this;
83
84 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
85 args[_key] = arguments[_key];
86 }
87
88 if (this._calledOnce) {
89 if (isDev) {
90 throw new Error('[Revue] You can only subscribe once, pass multi args to subscribe more than one state.');
91 }
92 return false;
93 }
94 this._calledOnce = true;
95 var handleChange = function handleChange() {
96 args.forEach(function (prop) {
97 // realProp: property name/path in your instance
98 // storeProp: property name/path in Redux store
99 var realProp = prop;
100 var storeProp = prop;
101 if (re.test(prop)) {
102 var _prop$match = prop.match(re);
103
104 var _prop$match2 = babelHelpers.slicedToArray(_prop$match, 3);
105
106 storeProp = _prop$match2[1];
107 realProp = _prop$match2[2];
108 }
109 if (realProp && storeProp) {
110 var currentValue = store.getState()[storeProp];
111 _this.$set(realProp, currentValue);
112 }
113 });
114 };
115 this._unsubscribe = store.subscribe(handleChange);
116 Vue.mixin({
117 beforeDestroy: function beforeDestroy() {
118 if (this._unsubscribe) {
119 this._calledOnce = false;
120 this.unsubscribe();
121 }
122 }
123 });
124 };
125}
126
127var Revue = function () {
128 function Revue(Vue, reduxStore, reduxActions) {
129 babelHelpers.classCallCheck(this, Revue);
130
131 this.store = reduxStore;
132 bindVue(Vue, this.store);
133 if (reduxActions) {
134 this.reduxActions = reduxActions;
135 }
136 }
137
138 babelHelpers.createClass(Revue, [{
139 key: 'dispatch',
140 value: function dispatch() {
141 for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
142 args[_key2] = arguments[_key2];
143 }
144
145 this.store.dispatch.apply(null, args);
146 return this;
147 }
148 }, {
149 key: 'state',
150 get: function get() {
151 return this.store.getState();
152 }
153 }, {
154 key: 'actions',
155 get: function get() {
156 if (isDev && !this.reduxActions) {
157 throw new Error('[Revue] Binding actions to Revue before calling them!');
158 }
159 return this.reduxActions;
160 }
161 }]);
162 return Revue;
163}();
164
165module.exports = Revue;
\No newline at end of file