Skip to content

Commit 19863a0

Browse files
committed
[build] 2.0.0-rc.2
1 parent 9b3aa50 commit 19863a0

File tree

2 files changed

+103
-46
lines changed

2 files changed

+103
-46
lines changed

dist/vuex.js

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Vuex v2.0.0-rc.1
2+
* Vuex v2.0.0-rc.2
33
* (c) 2016 Evan You
44
* Released under the MIT License.
55
*/
@@ -9,19 +9,21 @@
99
(global.Vuex = factory());
1010
}(this, function () { 'use strict';
1111

12-
var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
12+
var devtoolHook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
1313

1414
function devtoolPlugin(store) {
15-
if (!hook) return;
15+
if (!devtoolHook) return;
1616

17-
hook.emit('vuex:init', store);
17+
store._devtoolHook = devtoolHook;
1818

19-
hook.on('vuex:travel-to-state', function (targetState) {
19+
devtoolHook.emit('vuex:init', store);
20+
21+
devtoolHook.on('vuex:travel-to-state', function (targetState) {
2022
store.replaceState(targetState);
2123
});
2224

2325
store.subscribe(function (mutation, state) {
24-
hook.emit('vuex:mutation', mutation, state);
26+
devtoolHook.emit('vuex:mutation', mutation, state);
2527
});
2628
}
2729

@@ -60,12 +62,42 @@
6062
}
6163
}
6264

63-
function mapGetters(getters) {
65+
function mapState(map) {
6466
var res = {};
65-
normalizeMap(getters).forEach(function (_ref) {
67+
Object.keys(map).forEach(function (key) {
68+
var fn = map[key];
69+
res[key] = function mappedState() {
70+
return fn.call(this, this.$store.state);
71+
};
72+
});
73+
return res;
74+
}
75+
76+
function mapMutations(mutations) {
77+
var res = {};
78+
normalizeMap(mutations).forEach(function (_ref) {
6679
var key = _ref.key;
6780
var val = _ref.val;
6881

82+
res[key] = function mappedMutation() {
83+
var _$store;
84+
85+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
86+
args[_key] = arguments[_key];
87+
}
88+
89+
return (_$store = this.$store).commit.apply(_$store, [val].concat(args));
90+
};
91+
});
92+
return res;
93+
}
94+
95+
function mapGetters(getters) {
96+
var res = {};
97+
normalizeMap(getters).forEach(function (_ref2) {
98+
var key = _ref2.key;
99+
var val = _ref2.val;
100+
69101
res[key] = function mappedGetter() {
70102
if (!(val in this.$store.getters)) {
71103
console.error("[vuex] unknown getter: " + val);
@@ -78,18 +110,18 @@
78110

79111
function mapActions(actions) {
80112
var res = {};
81-
normalizeMap(actions).forEach(function (_ref2) {
82-
var key = _ref2.key;
83-
var val = _ref2.val;
113+
normalizeMap(actions).forEach(function (_ref3) {
114+
var key = _ref3.key;
115+
var val = _ref3.val;
84116

85117
res[key] = function mappedAction() {
86-
var _$store;
118+
var _$store2;
87119

88-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
89-
args[_key] = arguments[_key];
120+
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
121+
args[_key2] = arguments[_key2];
90122
}
91123

92-
return (_$store = this.$store).dispatch.apply(_$store, [val].concat(args));
124+
return (_$store2 = this.$store).dispatch.apply(_$store2, [val].concat(args));
93125
};
94126
});
95127
return res;
@@ -142,13 +174,8 @@
142174
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
143175
classCallCheck(this, Store);
144176

145-
if (!Vue) {
146-
throw new Error('[vuex] must call Vue.use(Vuex) before creating a store instance.');
147-
}
148-
149-
if (typeof Promise === 'undefined') {
150-
throw new Error('[vuex] vuex requires a Promise polyfill in this browser.');
151-
}
177+
assert(Vue, 'must call Vue.use(Vuex) before creating a store instance.');
178+
assert(typeof Promise !== 'undefined', 'vuex requires a Promise polyfill in this browser.');
152179

153180
var _options$state = options.state;
154181
var state = _options$state === undefined ? {} : _options$state;
@@ -166,6 +193,7 @@
166193
this._actions = Object.create(null);
167194
this._mutations = Object.create(null);
168195
this._subscribers = [];
196+
this._pendingActions = [];
169197

170198
// bind commit and dispatch to self
171199
var store = this;
@@ -207,10 +235,9 @@
207235
value: function module(path, _module, hot) {
208236
var _this2 = this;
209237

238+
this._committing = true;
210239
if (typeof path === 'string') path = [path];
211-
if (!Array.isArray(path)) {
212-
throw new Error('[vuex] module path must be a string or an Array.');
213-
}
240+
assert(Array.isArray(path), 'module path must be a string or an Array.');
214241

215242
var isRoot = !path.length;
216243
var state = _module.state;
@@ -244,6 +271,7 @@
244271
_this2.module(path.concat(key), modules[key], hot);
245272
});
246273
}
274+
this._committing = false;
247275
}
248276
}, {
249277
key: 'mutation',
@@ -270,15 +298,20 @@
270298
var res = handler({
271299
dispatch: dispatch,
272300
commit: commit,
273-
state: getNestedState(store.state, path)
301+
state: getNestedState(store.state, path),
302+
rootState: store.state
274303
}, payload, cb);
275304
if (!isPromise(res)) {
276305
res = Promise.resolve(res);
277306
}
278-
return res.catch(function (err) {
279-
console.error('[vuex] error in Promise returned from action "' + type + '":');
280-
console.error(err);
281-
});
307+
if (store._devtoolHook) {
308+
return res.catch(function (err) {
309+
store._devtoolHook.emit('vuex:error', err);
310+
throw err;
311+
});
312+
} else {
313+
return res;
314+
}
282315
});
283316
}
284317
}, {
@@ -315,13 +348,23 @@
315348
value: function dispatch(type, payload) {
316349
var entry = this._actions[type];
317350
if (!entry) {
318-
debugger;
319351
console.error('[vuex] unknown action type: ' + type);
320352
return;
321353
}
322-
return entry.length > 1 ? Promise.all(entry.map(function (handler) {
354+
var res = entry.length > 1 ? Promise.all(entry.map(function (handler) {
323355
return handler(payload);
324356
})) : entry[0](payload);
357+
var pending = this._pendingActions;
358+
pending.push(res);
359+
return res.then(function (value) {
360+
pending.splice(pending.indexOf(res), 1);
361+
return value;
362+
});
363+
}
364+
}, {
365+
key: 'onActionsResolved',
366+
value: function onActionsResolved(cb) {
367+
Promise.all(this._pendingActions).then(cb);
325368
}
326369
}, {
327370
key: 'subscribe',
@@ -337,10 +380,20 @@
337380
}
338381
};
339382
}
383+
}, {
384+
key: 'watch',
385+
value: function watch(getter, cb, options) {
386+
var _this4 = this;
387+
388+
assert(typeof getter === 'function', 'store.watch only accepts a function.');
389+
return this._vm.$watch(function () {
390+
return getter(_this4.state);
391+
}, cb, options);
392+
}
340393
}, {
341394
key: 'hotUpdate',
342395
value: function hotUpdate(newOptions) {
343-
var _this4 = this;
396+
var _this5 = this;
344397

345398
this._actions = Object.create(null);
346399
this._mutations = Object.create(null);
@@ -362,16 +415,16 @@
362415
var getters = extractModuleGetters(newOptions.getters, newOptions.modules);
363416
if (Object.keys(getters).length) {
364417
(function () {
365-
var oldVm = _this4._vm;
366-
initStoreState(_this4, _this4.state, getters);
367-
if (_this4.strict) {
368-
enableStrictMode(_this4);
418+
var oldVm = _this5._vm;
419+
initStoreState(_this5, _this5.state, getters);
420+
if (_this5.strict) {
421+
enableStrictMode(_this5);
369422
}
370423
// dispatch changes in all subscribed watchers
371424
// to force getter re-evaluation.
372-
_this4._committing = true;
425+
_this5._committing = true;
373426
oldVm.state = null;
374-
_this4._committing = false;
427+
_this5._committing = false;
375428
Vue.nextTick(function () {
376429
return oldVm.$destroy();
377430
});
@@ -384,12 +437,16 @@
384437
return this._vm.state;
385438
},
386439
set: function set(v) {
387-
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.');
440+
assert(false, 'Use store.replaceState() to explicit replace store state.');
388441
}
389442
}]);
390443
return Store;
391444
}();
392445

446+
function assert(condition, msg) {
447+
if (!condition) throw new Error('[vuex] ' + msg);
448+
}
449+
393450
function initStoreState(store, state, getters) {
394451
// bind getters
395452
store.getters = {};
@@ -436,7 +493,7 @@
436493
return;
437494
}
438495
getters[getterKey] = function wrappedGetter(state) {
439-
return rawGetter(getNestedState(state, modulePath));
496+
return rawGetter(getNestedState(state, modulePath), state);
440497
};
441498
});
442499
}
@@ -447,9 +504,7 @@
447504

448505
function enableStrictMode(store) {
449506
store._vm.$watch('state', function () {
450-
if (!store._committing) {
451-
throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.');
452-
}
507+
assert(store._committing, 'Do not mutate vuex store state outside mutation handlers.');
453508
}, { deep: true, sync: true });
454509
}
455510

@@ -484,6 +539,8 @@
484539
var index = {
485540
Store: Store,
486541
install: install,
542+
mapState: mapState,
543+
mapMutations: mapMutations,
487544
mapGetters: mapGetters,
488545
mapActions: mapActions
489546
};

0 commit comments

Comments
 (0)