8000 [build] 1.0.0-rc · poorprogrammer/vuejs.org@2d52750 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d52750

Browse files
committed
[build] 1.0.0-rc
1 parent 0168832 commit 2d52750

File tree

2 files changed

+125
-122
lines changed

2 files changed

+125
-122
lines changed

dist/vuex.js

Lines changed: 123 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Vuex v0.8.2
2+
* Vuex v1.0.0-rc
33
* (c) 2016 Evan You
44
* Released under the MIT License.
55
*/
@@ -64,9 +64,9 @@
6464
// allow multiple mutation objects to contain duplicate
6565
// handlers for the same mutation type
6666
if (Array.isArray(existing)) {
67-
existing.push(obj[key]);
67+
prev[key] = existing.concat(obj[key]);
6868
} else {
69-
prev[key] = [prev[key], obj[key]];
69+
prev[key] = [existing].concat(obj[key]);
7070
}
7171
} else {
7272
prev[key] = obj[key];
@@ -77,26 +77,27 @@
7777
}
7878

7979
/**
80-
* Deep clone an object. Faster than JSON.parse(JSON.stringify()).
80+
* Check whether the given value is Object or not
8181
*
8282
* @param {*} obj
83-
* @return {*}
83+
* @return {Boolean}
8484
*/
8585

86-
function deepClone(obj) {
87-
if (Array.isArray(obj)) {
88-
return obj.map(deepClone);
89-
} else if (obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') {
90-
var cloned = {};
91-
var keys = Object.keys(obj);
92-
for (var i = 0, l = keys.length; i < l; i++) {
93-
var key = keys[i];
94-
cloned[key] = deepClone(obj[key]);
95-
}
96-
return cloned;
97-
} else {
98-
return obj;
99-
}
86+
function isObject(obj) {
87+
return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
88+
}
89+
90+
/**
91+
* Get state sub tree by given keys.
92+
*
93+
* @param {Object} state
94+
* @param {Array<String>} nestedKeys
95+
* @return {Object}
96+
*/
97+
function getNestedState(state, nestedKeys) {
98+
return nestedKeys.reduce(function (state, key) {
99+
return state[key];
100+
}, state);
100101
}
101102

102103
/**
@@ -125,21 +126,19 @@
125126

126127
var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
127128

128-
var devtoolMiddleware = {
129-
onInit: function onInit(state, store) {
130-
if (!hook) return;
131-
hook.emit('vuex:init', store);
132-
hook.on('vuex:travel-to-state', function (targetState) {
133-
store._dispatching = true;
134-
store._vm.state = targetState;
135-
store._dispatching = false;
136-
});
137-
},
138-
onMutation: function onMutation(mutation, state) {
139-
if (!hook) return;
129+
function devtoolPlugin(store) {
130+
if (!hook) return;
131+
132+
hook.emit('vuex:init', store);
133+
134+
hook.on('vuex:travel-to-state', function (targetState) {
135+
store.replaceState(targetState);
136+
});
137+
138+
store.on('mutation', function (mutation, state) {
140139
hook.emit('vuex:mutation', mutation, state);
141-
}
142-
};
140+
});
141+
}
143142

144143
function override (Vue) {
145144
var version = Number(Vue.version.split('.')[0]);
@@ -317,7 +316,7 @@
317316
* - {Object} state
318317
* - {Object} actions
319318
* - {Object} mutations
320-
* - {Array} middlewares
319+
* - {Array} plugins
321320
* - {Boolean} strict
322321
*/
323322

@@ -332,8 +331,8 @@
332331
var mutations = _ref$mutations === undefined ? {} : _ref$mutations;
333332
var _ref$modules = _ref.modules;
334333
var modules = _ref$modules === undefined ? {} : _ref$modules;
335-
var _ref$middlewares = _ref.middlewares;
336-
var middlewares = _ref$middlewares === undefined ? [] : _ref$middlewares;
334+
var _ref$plugins = _ref.plugins;
335+
var plugins = _ref$plugins === undefined ? [] : _ref$plugins;
337336
var _ref$strict = _ref.strict;
338337
var strict = _ref$strict === undefined ? false : _ref$strict;
339338
classCallCheck(this, Store);
@@ -342,6 +341,7 @@
342341
this._dispatching = false;
343342
this._rootMutations = this._mutations = mutations;
344343
this._modules = modules;
344+
this._events = Object.create(null);
345345
// bind dispatch to self
346346
var dispatch = this.dispatch;
347347
this.dispatch = function () {
@@ -367,11 +367,15 @@
367367
Vue.config.silent = silent;
368368
this._setupModuleState(state, modules);
369369
this._setupModuleMutations(modules);
370-
this._setupMiddlewares(middlewares, state);
371370
// add extra warnings in strict mode
372371
if (strict) {
373372
this._setupMutationCheck();
374373
}
374+
// apply plugins
375+
devtoolPlugin(this);
376+
plugins.forEach(function (plugin) {
377+
return plugin(_this);
378+
});
375379
}
376380

377381
/**
@@ -382,41 +386,60 @@
382386
*/
383387

384388
createClass(Store, [{
385-
key: 'dispatch',
389+
key: 'replaceState',
386390

387391

392+
/**
393+
* Replace root state.
394+
*
395+
* @param {Object} state
396+
*/
397+
398+
value: function replaceState(state) {
399+
this._dispatching = true;
400+
this._vm.state = state;
401+
this._dispatching = false;
402+
}
403+
388404
/**
389405
* Dispatch an action.
390406
*
391407
* @param {String} type
392408
*/
393409

410+
}, {
411+
key: 'dispatch',
394412
value: function dispatch(type) {
395413
for (var _len2 = arguments.length, payload = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
396414
payload[_key2 - 1] = arguments[_key2];
397415
}
398416

399417
var silent = false;
418+
var isObjectStyleDispatch = false;
400419
// compatibility for object actions, e.g. FSA
401420
if ((typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type && arguments.length === 1) {
402-
payload = [type.payload];
421+
isObjectStyleDispatch = true;
422+
payload = type;
403423
if (type.silent) silent = true;
404424
type = type.type;
405425
}
406-
var mutation = this._mutations[type];
426+
var handler = this._mutations[type];
407427
var state = this.state;
408-
if (mutation) {
428+
if (handler) {
409429
this._dispatching = true;
410430
// apply the mutation
411-
if (Array.isArray(mutation)) {
412-
mutation.forEach(function (m) {
413-
return m.apply(undefined, [state].concat(toConsumableArray(payload)));
431+
if (Array.isArray(handler)) {
432+
handler.forEach(function (h) {
433+
isObjectStyleDispatch ? h(state, payload) : h.apply(undefined, [state].concat(toConsumableArray(payload)));
414434
});
415435
} else {
416-
mutation.apply(undefined, [state].concat(toConsumableArray(payload)));
436+
isObjectStyleDispatch ? handler(state, payload) : handler.apply(undefined, [state].concat(toConsumableArray(payload)));
417437
}
418438
this._dispatching = false;
419-
if (!silent) this._applyMiddlewares(type, payload);
439+
if (!silent) {
440+
var mutation = isObjectStyleDispatch ? payload : { type: type, payload: payload };
441+
this.emit('mutation', mutation, state);
442+
}
420443
} else {
421444
console.warn('[vuex] Unknown mutation: ' + type);
422445
}
@@ -476,8 +499,18 @@
476499
}, {
477500
key: '_setupModuleState',
478501
value: function _setupModuleState(state, modules) {
502+
var _this3 = this;
503+
504+
if (!isObject(modules)) return;
505+
479506
Object.keys(modules).forEach(function (key) {
480-
Vue.set(state, key, modules[key].state || {});
507+
var module = modules[key];
508+
509+
// set this module's state
510+
Vue.set(state, key, module.state || {});
511+
512+
// retrieve nested modules
513+
_this3._setupModuleState(state[key], module.modules);
481514
});
482515
}
483516

@@ -492,13 +525,41 @@
492525
key: '_setupModuleMutations',
493526
value: function _setupModuleMutations(updatedModules) {
494527
var modules = this._modules;
495-
var allMutations = [this._rootMutations];
496528
Object.keys(updatedModules).forEach(function (key) {
497529
modules[key] = updatedModules[key];
498530
});
499-
Object.keys(modules).forEach(function (key) {
531+
var updatedMutations = this._createModuleMutations(modules, []);
532+
this._mutations = mergeObjects([this._rootMutations].concat(toConsumableArray(updatedMutations)));
533+
}
534+
535+
/**
536+
* Helper method for _setupModuleMutations.
537+
* The method retrieve nested sub modules and
538+
* bind each mutations to its sub tree recursively.
539+
*
540+
* @param {Object} modules
541+
* @param {Array<String>} nestedKeys
542+
* @return {Array<Object>}
543+
*/
544+
545+
}, {
546+
key: '_createModuleMutations',
547+
value: function _createModuleMutations(modules, nestedKeys) {
548+
var _this4 = this;
549+
550+
if (!isObject(modules)) return [];
551+
552+
return Object.keys(modules).map(function (key) {
500553
var module = modules[key];
501-
if (!module || !module.mutations) return;
554+
var newNestedKeys = nestedKeys.concat(key);
555+
556+
// retrieve nested modules
557+
var nestedMutations = _this4._createModuleMutations(module.modules, newNestedKeys);
558+
559+
if (!module || !module.mutations) {
560+
return mergeObjects(nestedMutations);
561+
}
562+
502563
// bind mutations to sub state tree
503564
var mutations = {};
504565
Object.keys(module.mutations).forEach(function (name) {
@@ -508,12 +569,13 @@
508569
args[_key3 - 1] = arguments[_key3];
509570
}
510571

511-
original.apply(undefined, [state[key]].concat(args));
572+
original.apply(undefined, [getNestedState(state, newNestedKeys)].concat(args));
512573
};
513574
});
514-
allMutations.push(mutations);
575+
576+
// merge mutations of this module and nested modules
577+
return mergeObjects([mutations].concat(toConsumableArray(nestedMutations)));
515578
});
516-
this._mutations = mergeObjects(allMutations);
517579
}
518580

519581
/**
@@ -528,87 +590,24 @@
528590
}, {
529591
key: '_setupMutationCheck',
530592
value: function _setupMutationCheck() {
531-
var _this3 = this;
593+
var _this5 = this;
532594

533595
var Watcher = getWatcher(this._vm);
534596
/* eslint-disable no-new */
535597
new Watcher(this._vm, 'state', function () {
536-
if (!_this3._dispatching) {
598+
if (!_this5._dispatching) {
537599
throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.');
538600
}
539601
}, { deep: true, sync: true });
540602
/* eslint-enable no-new */
541603
}
542-
543-
/**
544-
* Setup the middlewares. The devtools middleware is always
545-
* included, since it does nothing if no devtool is detected.
546-
*
547-
* A middleware can demand the state it receives to be
548-
* "snapshots", i.e. deep clones of the actual state tree.
549-
*
550-
* @param {Array} middlewares
551-
* @param {Object} state
552-
*/
553-
554-
}, {
555-
key: '_setupMiddlewares',
556-
value: function _setupMiddlewares(middlewares, state) {
557-
var _this4 = this;
558-
559-
this._middlewares = [devtoolMiddleware].concat(middlewares);
560-
this._needSnapshots = middlewares.some(function (m) {
561-
return m.snapshot;
562-
});
563-
if (this._needSnapshots) {
564-
console.log('[vuex] One or more of your middlewares are taking state snapshots ' + 'for each mutation. Make sure to use them only during development.');
565-
}
566-
var initialSnapshot = this._prevSnapshot = this._needSnapshots ? deepClone(state) : null;
567-
// call init hooks
568-
this._middlewares.forEach(function (m) {
569-
if (m.onInit) {
570-
m.onInit(m.snapshot ? initialSnapshot : state, _this4);
571-
}
572-
});
573-
}
574-
575-
/**
576-
* Apply the middlewares on a given mutation.
577-
*
578-
* @param {String} type
579-
* @param {Array} payload
580-
*/
581-
582-
}, {
583-
key: '_applyMiddlewares',
584-
value: function _applyMiddlewares(type, payload) {
585-
var _this5 = this;
586-
587-
var state = this.state;
588-
var prevSnapshot = this._prevSnapshot;
589-
var snapshot = void 0,
590-
clonedPayload = void 0;
591-
if (this._needSnapshots) {
592-
snapshot = this._prevSnapshot = deepClone(state);
593-
clonedPayload = deepClone(payload);
594-
}
595-
this._middlewares.forEach(function (m) {
596-
if (m.onMutation) {
597-
if (m.snapshot) {
598-
m.onMutation({ type: type, payload: clonedPayload }, snapshot, prevSnapshot, _this5);
599-
} else {
600-
m.onMutation({ type: type, payload: payload }, state, _this5);
601-
}
602-
}
603-
});
604-
}
605604
}, {
606605
key: 'state',
607606
get: function get() {
608607
return this._vm.state;
609608
},
610609
set: function set(v) {
611-
throw new Error('[vuex] Vuex root state is read only.');
610+
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.');
612611
}
613612
}]);
614613
return Store;
@@ -619,7 +618,11 @@
619618
console.warn('[vuex] already installed. Vue.use(Vuex) should be called only once.');
620619
return;
621620
}
622-
Vue = _Vue;
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+
});
623626
override(Vue);
624627
}
625628

0 commit comments

Comments
 (0)
9 0