|
1 | 1 | /*!
|
2 |
| - * Vuex v1.0.0-rc |
| 2 | + * Vuex v1.0.0-rc.2 |
3 | 3 | * (c) 2016 Evan You
|
4 | 4 | * Released under the MIT License.
|
5 | 5 | */
|
|
135 | 135 | store.replaceState(targetState);
|
136 | 136 | });
|
137 | 137 |
|
138 |
| - store.on('mutation', function (mutation, state) { |
| 138 | + store.subscribe(function (mutation, state) { |
139 | 139 | hook.emit('vuex:mutation', mutation, state);
|
140 | 140 | });
|
141 | 141 | }
|
|
341 | 341 | this._dispatching = false;
|
342 | 342 | this._rootMutations = this._mutations = mutations;
|
343 | 343 | this._modules = modules;
|
344 |
| - this._events = Object.create(null); |
| 344 | + this._subscribers = []; |
345 | 345 | // bind dispatch to self
|
346 | 346 | var dispatch = this.dispatch;
|
347 | 347 | this.dispatch = function () {
|
|
410 | 410 | }, {
|
411 | 411 | key: 'dispatch',
|
412 | 412 | value: function dispatch(type) {
|
| 413 | + var _this2 = this; |
| 414 | + |
413 | 415 | for (var _len2 = arguments.length, payload = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
414 | 416 | payload[_key2 - 1] = arguments[_key2];
|
415 | 417 | }
|
|
437 | 439 | }
|
438 | 440 | this._dispatching = false;
|
439 | 441 | if (!silent) {
|
440 |
| - var mutation = isObjectStyleDispatch ? payload : { type: type, payload: payload }; |
441 |
| - this.emit('mutation', mutation, state); |
| 442 | + (function () { |
| 443 | + var mutation = isObjectStyleDispatch ? payload : { type: type, payload: payload }; |
| 444 | + _this2._subscribers.forEach(function (sub) { |
| 445 | + return sub(mutation, state); |
| 446 | + }); |
| 447 | + })(); |
442 | 448 | }
|
443 | 449 | } else {
|
444 | 450 | console.warn('[vuex] Unknown mutation: ' + type);
|
|
458 | 464 | }, {
|
459 | 465 | key: 'watch',
|
460 | 466 | value: function watch(fn, cb, options) {
|
461 |
| - var _this2 = this; |
| 467 | + var _this3 = this; |
462 | 468 |
|
463 | 469 | if (typeof fn !== 'function') {
|
464 | 470 | console.error('Vuex store.watch only accepts function.');
|
465 | 471 | return;
|
466 | 472 | }
|
467 | 473 | return this._vm.$watch(function () {
|
468 |
| - return fn(_this2.state); |
| 474 | + return fn(_this3.state); |
469 | 475 | }, cb, options);
|
470 | 476 | }
|
471 | 477 |
|
| 478 | + /** |
| 479 | + * Subscribe to state changes. Fires after every mutation. |
| 480 | + */ |
| 481 | + |
| 482 | + }, { |
| 483 | + key: 'subscribe', |
| 484 | + value: function subscribe(fn) { |
| 485 | + var subs = this._subscribers; |
| 486 | + if (subs.indexOf(fn) < 0) { |
| 487 | + subs.push(fn); |
| 488 | + } |
| 489 | + return function () { |
| 490 | + var i = subs.indexOf(fn); |
| 491 | + if (i > -1) { |
| 492 | + subs.splice(i, 1); |
| 493 | + } |
| 494 | + }; |
| 495 | + } |
| 496 | + |
472 | 497 | /**
|
473 | 498 | * Hot update mutations & modules.
|
474 | 499 | *
|
|
499 | 524 | }, {
|
500 | 525 | key: '_setupModuleState',
|
501 | 526 | value: function _setupModuleState(state, modules) {
|
502 |
| - var _this3 = this; |
| 527 | + var _this4 = this; |
503 | 528 |
|
504 | 529 | if (!isObject(modules)) return;
|
505 | 530 |
|
|
510 | 535 | Vue.set(state, key, module.state || {});
|
511 | 536 |
|
512 | 537 | // retrieve nested modules
|
513 |
| - _this3._setupModuleState(state[key], module.modules); |
| 538 | + _this4._setupModuleState(state[key], module.modules); |
514 | 539 | });
|
515 | 540 | }
|
516 | 541 |
|
|
545 | 570 | }, {
|
546 | 571 | key: '_createModuleMutations',
|
547 | 572 | value: function _createModuleMutations(modules, nestedKeys) {
|
548 |
| - var _this4 = this; |
| 573 | + var _this5 = this; |
549 | 574 |
|
550 | 575 | if (!isObject(modules)) return [];
|
551 | 576 |
|
|
554 | 579 | var newNestedKeys = nestedKeys.concat(key);
|
555 | 580 |
|
556 | 581 | // retrieve nested modules
|
557 |
| - var nestedMutations = _this4._createModuleMutations(module.modules, newNestedKeys); |
| 582 | + var nestedMutations = _this5._createModuleMutations(module.modules, newNestedKeys); |
558 | 583 |
|
559 | 584 | if (!module || !module.mutations) {
|
560 | 585 | return mergeObjects(nestedMutations);
|
|
590 | 615 | }, {
|
591 | 616 | key: '_setupMutationCheck',
|
592 | 617 | value: function _setupMutationCheck() {
|
593 |
| - var _this5 = this; |
| 618 | + var _this6 = this; |
594 | 619 |
|
595 | 620 | var Watcher = getWatcher(this._vm);
|
596 | 621 | /* eslint-disable no-new */
|
597 | 622 | new Watcher(this._vm, 'state', function () {
|
598 |
| - if (!_this5._dispatching) { |
| 623 | + if (!_this6._dispatching) { |
599 | 624 | throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.');
|
600 | 625 | }
|
601 | 626 | }, { deep: true, sync: true });
|
|
618 | 643 | console.warn('[vuex] already installed. Vue.use(Vuex) should be called only once.');
|
619 | 644 | return;
|
620 | 645 | }
|
621 |
| - Vue = _Vue |
622 |
| - // reuse Vue's event system |
623 |
| - ;['on', 'off', 'once', 'emit'].forEach(function (e) { |
624 |
| - Store.prototype[e] = Store.prototype['$' + e] = Vue.prototype['$' + e]; |
625 |
| - }); |
| 646 | + Vue = _Vue; |
626 | 647 | override(Vue);
|
627 | 648 | }
|
628 | 649 |
|
|
0 commit comments