|
1 | 1 | /*!
|
2 |
| - * Vuex v2.0.0-rc.1 |
| 2 | + * Vuex v2.0.0-rc.2 |
3 | 3 | * (c) 2016 Evan You
|
4 | 4 | * Released under the MIT License.
|
5 | 5 | */
|
|
9 | 9 | (global.Vuex = factory());
|
10 | 10 | }(this, function () { 'use strict';
|
11 | 11 |
|
12 |
| - var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 12 | + var devtoolHook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
13 | 13 |
|
14 | 14 | function devtoolPlugin(store) {
|
15 |
| - if (!hook) return; |
| 15 | + if (!devtoolHook) return; |
16 | 16 |
|
17 |
| - hook.emit('vuex:init', store); |
| 17 | + store._devtoolHook = devtoolHook; |
18 | 18 |
|
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) { |
20 | 22 | store.replaceState(targetState);
|
21 | 23 | });
|
22 | 24 |
|
23 | 25 | store.subscribe(function (mutation, state) {
|
24 |
| - hook.emit('vuex:mutation', mutation, state); |
| 26 | + devtoolHook.emit('vuex:mutation', mutation, state); |
25 | 27 | });
|
26 | 28 | }
|
27 | 29 |
|
|
60 | 62 | }
|
61 | 63 | }
|
62 | 64 |
|
63 |
| - function mapGetters(getters) { |
| 65 | + function mapState(map) { |
64 | 66 | 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) { |
66 | 79 | var key = _ref.key;
|
67 | 80 | var val = _ref.val;
|
68 | 81 |
|
| 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 | + |
69 | 101 | res[key] = function mappedGetter() {
|
70 | 102 | if (!(val in this.$store.getters)) {
|
71 | 103 | console.error("[vuex] unknown getter: " + val);
|
|
78 | 110 |
|
79 | 111 | function mapActions(actions) {
|
80 | 112 | 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; |
84 | 116 |
|
85 | 117 | res[key] = function mappedAction() {
|
86 |
| - var _$store; |
| 118 | + var _$store2; |
87 | 119 |
|
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]; |
90 | 122 | }
|
91 | 123 |
|
92 |
| - return (_$store = this.$store).dispatch.apply(_$store, [val].concat(args)); |
| 124 | + return (_$store2 = this.$store).dispatch.apply(_$store2, [val].concat(args)); |
93 | 125 | };
|
94 | 126 | });
|
95 | 127 | return res;
|
|
142 | 174 | var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
143 | 175 | classCallCheck(this, Store);
|
144 | 176 |
|
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.'); |
152 | 179 |
|
153 | 180 | var _options$state = options.state;
|
154 | 181 | var state = _options$state === undefined ? {} : _options$state;
|
|
166 | 193 | this._actions = Object.create(null);
|
167 | 194 | this._mutations = Object.create(null);
|
168 | 195 | this._subscribers = [];
|
| 196 | + this._pendingActions = []; |
169 | 197 |
|
170 | 198 | // bind commit and dispatch to self
|
171 | 199 | var store = this;
|
|
207 | 235 | value: function module(path, _module, hot) {
|
208 | 236 | var _this2 = this;
|
209 | 237 |
|
| 238 | + this._committing = true; |
210 | 239 | 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.'); |
214 | 241 |
|
215 | 242 | var isRoot = !path.length;
|
216 | 243 | var state = _module.state;
|
|
244 | 271 | _this2.module(path.concat(key), modules[key], hot);
|
245 | 272 | });
|
246 | 273 | }
|
| 274 | + this._committing = false; |
247 | 275 | }
|
248 | 276 | }, {
|
249 | 277 | key: 'mutation',
|
|
270 | 298 | var res = handler({
|
271 | 299 | dispatch: dispatch,
|
272 | 300 | commit: commit,
|
273 |
| - state: getNestedState(store.state, path) |
| 301 | + state: getNestedState(store.state, path), |
| 302 | + rootState: store.state |
274 | 303 | }, payload, cb);
|
275 | 304 | if (!isPromise(res)) {
|
276 | 305 | res = Promise.resolve(res);
|
277 | 306 | }
|
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 | + } |
282 | 315 | });
|
283 | 316 | }
|
284 | 317 | }, {
|
|
315 | 348 | value: function dispatch(type, payload) {
|
316 | 349 | var entry = this._actions[type];
|
317 | 350 | if (!entry) {
|
318 |
| - debugger; |
319 | 351 | console.error('[vuex] unknown action type: ' + type);
|
320 | 352 | return;
|
321 | 353 | }
|
322 |
| - return entry.length > 1 ? Promise.all(entry.map(function (handler) { |
| 354 | + var res = entry.length > 1 ? Promise.all(entry.map(function (handler) { |
323 | 355 | return handler(payload);
|
324 | 356 | })) : 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); |
325 | 368 | }
|
326 | 369 | }, {
|
327 | 370 | key: 'subscribe',
|
|
337 | 380 | }
|
338 | 381 | };
|
339 | 382 | }
|
| 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 | + } |
340 | 393 | }, {
|
341 | 394 | key: 'hotUpdate',
|
342 | 395 | value: function hotUpdate(newOptions) {
|
343 |
| - var _this4 = this; |
| 396 | + var _this5 = this; |
344 | 397 |
|
345 | 398 | this._actions = Object.create(null);
|
346 | 399 | this._mutations = Object.create(null);
|
|
362 | 415 | var getters = extractModuleGetters(newOptions.getters, newOptions.modules);
|
363 | 416 | if (Object.keys(getters).length) {
|
364 | 417 | (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); |
369 | 422 | }
|
370 | 423 | // dispatch changes in all subscribed watchers
|
371 | 424 | // to force getter re-evaluation.
|
372 |
| - _this4._committing = true; |
| 425 | + _this5._committing = true; |
373 | 426 | oldVm.state = null;
|
374 |
| - _this4._committing = false; |
| 427 | + _this5._committing = false; |
375 | 428 | Vue.nextTick(function () {
|
376 | 429 | return oldVm.$destroy();
|
377 | 430 | });
|
|
384 | 437 | return this._vm.state;
|
385 | 438 | },
|
386 | 439 | 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.'); |
388 | 441 | }
|
389 | 442 | }]);
|
390 | 443 | return Store;
|
391 | 444 | }();
|
392 | 445 |
|
| 446 | + function assert(condition, msg) { |
| 447 | + if (!condition) throw new Error('[vuex] ' + msg); |
| 448 | + } |
| 449 | + |
393 | 450 | function initStoreState(store, state, getters) {
|
394 | 451 | // bind getters
|
395 | 452 | store.getters = {};
|
|
436 | 493 | return;
|
437 | 494 | }
|
438 | 495 | getters[getterKey] = function wrappedGetter(state) {
|
439 |
| - return rawGetter(getNestedState(state, modulePath)); |
| 496 | + return rawGetter(getNestedState(state, modulePath), state); |
440 | 497 | };
|
441 | 498 | });
|
442 | 499 | }
|
|
447 | 504 |
|
448 | 505 | function enableStrictMode(store) {
|
449 | 506 | 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.'); |
453 | 508 | }, { deep: true, sync: true });
|
454 | 509 | }
|
455 | 510 |
|
|
484 | 539 | var index = {
|
485 | 540 | Store: Store,
|
486 | 541 | install: install,
|
| 542 | + mapState: mapState, |
| 543 | + mapMutations: mapMutations, |
487 | 544 | mapGetters: mapGetters,
|
488 | 545 | mapActions: mapActions
|
489 | 546 | };
|
|
0 commit comments