|
1 | 1 | /*!
|
2 |
| - * Vuex v0.4.2 |
| 2 | + * Vuex v0.5.0 |
3 | 3 | * (c) 2016 Evan You
|
4 | 4 | * Released under the MIT License.
|
5 | 5 | */
|
|
92 | 92 | }
|
93 | 93 | }
|
94 | 94 |
|
| 95 | + var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 96 | + |
95 | 97 | var devtoolMiddleware = {
|
96 |
| - onInit: function onInit(state) { |
97 |
| - // TODO |
| 98 | + onInit: function onInit(state, store) { |
| 99 | + if (!hook) return; |
| 100 | + hook.emit('vuex:init', store); |
| 101 | + hook.on('vuex:travel-to-state', function (targetState) { |
| 102 | + var currentState = store._vm._data; |
| 103 | + Object.keys(targetState).forEach(function (key) { |
| 104 | + currentState[key] = targetState[key]; |
| 105 | + }); |
| 106 | + }); |
98 | 107 | },
|
99 | 108 | onMutation: function onMutation(mutation, state) {
|
100 |
| - // TODO |
| 109 | + if (!hook) return; |
| 110 | + hook.emit('vuex:mutation', mutation, state); |
101 | 111 | }
|
102 | 112 | };
|
103 | 113 |
|
104 |
| - // Credits: borrowed code from fcomb/redux-logger |
105 |
| - |
106 |
| - function createLogger() { |
107 |
| - var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
108 |
| - |
109 |
| - var _ref$collapsed = _ref.collapsed; |
110 |
| - var collapsed = _ref$collapsed === undefined ? true : _ref$collapsed; |
111 |
| - var _ref$transformer = _ref.transformer; |
112 |
| - var transformer = _ref$transformer === undefined ? function (state) { |
113 |
| - return state; |
114 |
| - } : _ref$transformer; |
115 |
| - var _ref$mutationTransfor = _ref.mutationTransformer; |
116 |
| - var mutationTransformer = _ref$mutationTransfor === undefined ? function (mut) { |
117 |
| - return mut; |
118 |
| - } : _ref$mutationTransfor; |
119 |
| - |
120 |
| - return { |
121 |
| - snapshot: true, |
122 |
| - onMutation: function onMutation(mutation, nextState, prevState) { |
123 |
| - if (typeof console === 'undefined') { |
124 |
| - return; |
125 |
| - } |
126 |
| - var time = new Date(); |
127 |
| - var formattedTime = ' @ ' + pad(time.getHours(), 2) + ':' + pad(time.getMinutes(), 2) + ':' + pad(time.getSeconds(), 2) + '.' + pad(time.getMilliseconds(), 3); |
128 |
| - var formattedMutation = mutationTransformer(mutation); |
129 |
| - var message = 'mutation ' + mutation.type + formattedTime; |
130 |
| - var startMessage = collapsed ? console.groupCollapsed : console.group; |
131 |
| - |
132 |
| - // render |
133 |
| - try { |
134 |
| - startMessage.call(console, message); |
135 |
| - } catch (e) { |
136 |
| - console.log(message); |
137 |
| - } |
138 |
| - |
139 |
| - console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', prevState); |
140 |
| - console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation); |
141 |
| - console.log('%c next state', 'color: #4CAF50; font-weight: bold', nextState); |
142 |
| - |
143 |
| - try { |
144 |
| - console.groupEnd(); |
145 |
| - } catch (e) { |
146 |
| - console.log('—— log end ——'); |
147 |
| - } |
148 |
| - } |
149 |
| - }; |
150 |
| - } |
151 |
| - |
152 |
| - function repeat(str, times) { |
153 |
| - return new Array(times + 1).join(str); |
154 |
| - } |
155 |
| - |
156 |
| - function pad(num, maxLength) { |
157 |
| - return repeat('0', maxLength - num.toString().length) + num; |
158 |
| - } |
159 |
| - |
160 | 114 | // export install function
|
161 | 115 | function override (Vue) {
|
162 | 116 | var _init = Vue.prototype._init;
|
|
255 | 209 | dispatch.apply(_this, args);
|
256 | 210 | };
|
257 | 211 | // use a Vue instance to store the state tree
|
| 212 | + // suppress warnings just in case the user has added |
| 213 | + // some funky global mixins |
| 214 | + var silent = Vue.config.silent; |
| 215 | + Vue.config.silent = true; |
258 | 216 | this._vm = new Vue({
|
259 | 217 | data: state
|
260 | 218 | });
|
| 219 | + Vue.config.silent = silent; |
261 | 220 | this._setupModuleState(state, modules);
|
262 | 221 | this._setupModuleMutations(modules);
|
263 | 222 | this._setupMiddlewares(middlewares, state);
|
|
285 | 244 | */
|
286 | 245 |
|
287 | 246 | value: function dispatch(type) {
|
| 247 | + var _this2 = this; |
| 248 | + |
288 | 249 | for (var _len2 = arguments.length, payload = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
289 | 250 | payload[_key2 - 1] = arguments[_key2];
|
290 | 251 | }
|
|
313 | 274 | this._middlewares.forEach(function (m) {
|
314 | 275 | if (m.onMutation) {
|
315 | 276 | if (m.snapshot) {
|
316 |
| - m.onMutation({ type: type, payload: clonedPayload }, snapshot, prevSnapshot); |
| 277 | + m.onMutation({ type: type, payload: clonedPayload }, snapshot, prevSnapshot, _this2); |
317 | 278 | } else {
|
318 |
| - m.onMutation({ type: type, payload: payload }, state); |
| 279 | + m.onMutation({ type: type, payload: payload }, state, _this2); |
319 | 280 | }
|
320 | 281 | }
|
321 | 282 | });
|
|
337 | 298 | }, {
|
338 | 299 | key: 'watch',
|
339 | 300 | value: function watch(expOrFn, cb, options) {
|
340 |
| - var _this2 = this; |
| 301 | + var _this3 = this; |
341 | 302 |
|
342 | 303 | return this._vm.$watch(function () {
|
343 |
| - return typeof expOrFn === 'function' ? expOrFn(_this2.state) : _this2._vm.$get(expOrFn); |
| 304 | + return typeof expOrFn === 'function' ? expOrFn(_this3.state) : _this3._vm.$get(expOrFn); |
344 | 305 | }, cb, options);
|
345 | 306 | }
|
346 | 307 |
|
|
364 | 325 | this._setupModuleMutations(modules || this._modules);
|
365 | 326 | }
|
366 | 327 |
|
367 |
| - /** |
368 |
| - * Replace entire state tree. |
369 |
| - */ |
370 |
| - |
371 |
| - }, { |
372 |
| - key: 'replaceState', |
373 |
| - value: function replaceState(newState) { |
374 |
| - var state = this._vm._data; |
375 |
| - var clone = deepClone(newState); |
376 |
| - Object.keys(clone).forEach(function (key) { |
377 |
| - state[key] = clone[key]; |
378 |
| - }); |
379 |
| - } |
380 |
| - |
381 | 328 | /**
|
382 | 329 | * Attach sub state tree of each module to the root tree.
|
383 | 330 | *
|
|
391 | 338 | var setPath = Vue.parsers.path.setPath;
|
392 | 339 |
|
393 | 340 | Object.keys(modules).forEach(function (key) {
|
394 |
| - setPath(state, key, modules[key].state); |
| 341 | + setPath(state, key, modules[key].state || {}); |
395 | 342 | });
|
396 | 343 | }
|
397 | 344 |
|
|
411 | 358 | var allMutations = [this._rootMutations];
|
412 | 359 | Object.keys(modules).forEach(function (key) {
|
413 | 360 | var module = modules[key];
|
| 361 | + if (!module || !module.mutations) return; |
414 | 362 | // bind mutations to sub state tree
|
415 | 363 | var mutations = {};
|
416 | 364 | Object.keys(module.mutations).forEach(function (name) {
|
|
440 | 388 | }, {
|
441 | 389 | key: '_setupMutationCheck',
|
442 | 390 | value: function _setupMutationCheck() {
|
443 |
| - var _this3 = this; |
| 391 | + var _this4 = this; |
444 | 392 |
|
445 | 393 | // a hack to get the watcher constructor from older versions of Vue
|
446 | 394 | // mainly because the public $watch method does not allow sync
|
|
452 | 400 | unwatch();
|
453 | 401 | /* eslint-disable no-new */
|
454 | 402 | new Watcher(this._vm, '$data', function () {
|
455 |
| - if (!_this3._dispatching) { |
| 403 | + if (!_this4._dispatching) { |
456 | 404 | throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.');
|
457 | 405 | }
|
458 | 406 | }, { deep: true, sync: true });
|
|
473 | 421 | }, {
|
474 | 422 | key: '_setupMiddlewares',
|
475 | 423 | value: function _setupMiddlewares(middlewares, state) {
|
| 424 | + var _this5 = this; |
| 425 | + |
476 | 426 | this._middlewares = [devtoolMiddleware].concat(middlewares);
|
477 | 427 | this._needSnapshots = middlewares.some(function (m) {
|
478 | 428 | return m.snapshot;
|
|
484 | 434 | // call init hooks
|
485 | 435 | this._middlewares.forEach(function (m) {
|
486 | 436 | if (m.onInit) {
|
487 |
| - m.onInit(m.snapshot ? initialSnapshot : state); |
| 437 | + m.onInit(m.snapshot ? initialSnapshot : state, _this5); |
488 | 438 | }
|
489 | 439 | });
|
490 | 440 | }
|
|
507 | 457 |
|
508 | 458 | var index = {
|
509 | 459 | Store: Store,
|
510 |
| - install: install, |
511 |
| - createLogger: createLogger |
| 460 | + install: install |
512 | 461 | };
|
513 | 462 |
|
514 | 463 | return index;
|
|
0 commit comments