Comparing version 2.0.0-rc.1 to 2.0.0-rc.3
184
dist/vuex.js
/*! | ||
* Vuex v2.0.0-rc.1 | ||
* Vuex v2.0.0-rc.3 | ||
* (c) 2016 Evan You | ||
@@ -12,10 +12,12 @@ * Released under the MIT License. | ||
var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; | ||
var devtoolHook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; | ||
function devtoolPlugin(store) { | ||
if (!hook) return; | ||
if (!devtoolHook) return; | ||
hook.emit('vuex:init', store); | ||
store._devtoolHook = devtoolHook; | ||
hook.on('vuex:travel-to-state', function (targetState) { | ||
devtoolHook.emit('vuex:init', store); | ||
devtoolHook.on('vuex:travel-to-state', function (targetState) { | ||
store.replaceState(targetState); | ||
@@ -25,3 +27,3 @@ }); | ||
store.subscribe(function (mutation, state) { | ||
hook.emit('vuex:mutation', mutation, state); | ||
devtoolHook.emit('vuex:mutation', mutation, state); | ||
}); | ||
@@ -65,8 +67,38 @@ } | ||
function mapGetters(getters) { | ||
function mapState(map) { | ||
var res = {}; | ||
normalizeMap(getters).forEach(function (_ref) { | ||
Object.keys(map).forEach(function (key) { | ||
var fn = map[key]; | ||
res[key] = function mappedState() { | ||
return fn.call(this, this.$store.state, this.$store.getters); | ||
}; | ||
}); | ||
return res; | ||
} | ||
function mapMutations(mutations) { | ||
var res = {}; | ||
normalizeMap(mutations).forEach(function (_ref) { | ||
var key = _ref.key; | ||
var val = _ref.val; | ||
res[key] = function mappedMutation() { | ||
var _$store; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return (_$store = this.$store).commit.apply(_$store, [val].concat(args)); | ||
}; | ||
}); | ||
return res; | ||
} | ||
function mapGetters(getters) { | ||
var res = {}; | ||
normalizeMap(getters).forEach(function (_ref2) { | ||
var key = _ref2.key; | ||
var val = _ref2.val; | ||
res[key] = function mappedGetter() { | ||
@@ -84,14 +116,14 @@ if (!(val in this.$store.getters)) { | ||
var res = {}; | ||
normalizeMap(actions).forEach(function (_ref2) { | ||
var key = _ref2.key; | ||
var val = _ref2.val; | ||
normalizeMap(actions).forEach(function (_ref3) { | ||
var key = _ref3.key; | ||
var val = _ref3.val; | ||
res[key] = function mappedAction() { | ||
var _$store; | ||
var _$store2; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return (_$store = this.$store).dispatch.apply(_$store, [val].concat(args)); | ||
return (_$store2 = this.$store).dispatch.apply(_$store2, [val].concat(args)); | ||
}; | ||
@@ -149,10 +181,5 @@ }); | ||
if (!Vue) { | ||
throw new Error('[vuex] must call Vue.use(Vuex) before creating a store instance.'); | ||
} | ||
assert(Vue, 'must call Vue.use(Vuex) before creating a store instance.'); | ||
assert(typeof Promise !== 'undefined', 'vuex requires a Promise polyfill in this browser.'); | ||
if (typeof Promise === 'undefined') { | ||
throw new Error('[vuex] vuex requires a Promise polyfill in this browser.'); | ||
} | ||
var _options$state = options.state; | ||
@@ -174,2 +201,3 @@ var state = _options$state === undefined ? {} : _options$state; | ||
this._subscribers = []; | ||
this._pendingActions = []; | ||
@@ -216,6 +244,5 @@ // bind commit and dispatch to self | ||
this._committing = true; | ||
if (typeof path === 'string') path = [path]; | ||
if (!Array.isArray(path)) { | ||
throw new Error('[vuex] module path must be a string or an Array.'); | ||
} | ||
assert(Array.isArray(path), 'module path must be a string or an Array.'); | ||
@@ -254,2 +281,3 @@ var isRoot = !path.length; | ||
} | ||
this._committing = false; | ||
} | ||
@@ -281,3 +309,5 @@ }, { | ||
commit: commit, | ||
state: getNestedState(store.state, path) | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb); | ||
@@ -287,6 +317,10 @@ if (!isPromise(res)) { | ||
} | ||
return res.catch(function (err) { | ||
console.error('[vuex] error in Promise returned from action "' + type + '":'); | ||
console.error(err); | ||
}); | ||
if (store._devtoolHook) { | ||
return res.catch(function (err) { | ||
store._devtoolHook.emit('vuex:error', err); | ||
throw err; | ||
}); | ||
} else { | ||
return res; | ||
} | ||
}); | ||
@@ -328,11 +362,21 @@ } | ||
if (!entry) { | ||
debugger; | ||
console.error('[vuex] unknown action type: ' + type); | ||
return; | ||
} | ||
return entry.length > 1 ? Promise.all(entry.map(function (handler) { | ||
var res = entry.length > 1 ? Promise.all(entry.map(function (handler) { | ||
return handler(payload); | ||
})) : entry[0](payload); | ||
var pending = this._pendingActions; | ||
pending.push(res); | ||
return res.then(function (value) { | ||
pending.splice(pending.indexOf(res), 1); | ||
return value; | ||
}); | ||
} | ||
}, { | ||
key: 'onActionsResolved', | ||
value: function onActionsResolved(cb) { | ||
Promise.all(this._pendingActions).then(cb); | ||
} | ||
}, { | ||
key: 'subscribe', | ||
@@ -352,5 +396,15 @@ value: function subscribe(fn) { | ||
}, { | ||
key: 'watch', | ||
value: function watch(getter, cb, options) { | ||
var _this4 = this; | ||
assert(typeof getter === 'function', 'store.watch only accepts a function.'); | ||
return this._vm.$watch(function () { | ||
return getter(_this4.state); | ||
}, cb, options); | ||
} | ||
}, { | ||
key: 'hotUpdate', | ||
value: function hotUpdate(newOptions) { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -377,12 +431,12 @@ this._actions = Object.create(null); | ||
(function () { | ||
var oldVm = _this4._vm; | ||
initStoreState(_this4, _this4.state, getters); | ||
if (_this4.strict) { | ||
enableStrictMode(_this4); | ||
var oldVm = _this5._vm; | ||
initStoreState(_this5, _this5.state, getters); | ||
if (_this5.strict) { | ||
enableStrictMode(_this5); | ||
} | ||
// dispatch changes in all subscribed watchers | ||
// to force getter re-evaluation. | ||
_this4._committing = true; | ||
_this5._committing = true; | ||
oldVm.state = null; | ||
_this4._committing = false; | ||
_this5._committing = false; | ||
Vue.nextTick(function () { | ||
@@ -400,3 +454,3 @@ return oldVm.$destroy(); | ||
set: function set(v) { | ||
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.'); | ||
assert(false, 'Use store.replaceState() to explicit replace store state.'); | ||
} | ||
@@ -407,2 +461,6 @@ }]); | ||
function assert(condition, msg) { | ||
if (!condition) throw new Error('[vuex] ' + msg); | ||
} | ||
function initStoreState(store, state, getters) { | ||
@@ -416,3 +474,3 @@ // bind getters | ||
computed[key] = function () { | ||
return fn(store._vm.state); | ||
return fn(store); | ||
}; | ||
@@ -443,3 +501,8 @@ Object.defineProperty(store.getters, key, { | ||
if (!modules) return getters; | ||
if (!path.length) { | ||
wrapGetters(getters, getters, path, true); | ||
} | ||
if (!modules) { | ||
return getters; | ||
} | ||
Object.keys(modules).forEach(function (key) { | ||
@@ -449,12 +512,3 @@ var module = modules[key]; | ||
if (module.getters) { | ||
Object.keys(module.getters).forEach(function (getterKey) { | ||
var rawGetter = module.getters[getterKey]; | ||
if (getters[getterKey]) { | ||
console.error('[vuex] duplicate getter key: ' + getterKey); | ||
return; | ||
} | ||
getters[getterKey] = function wrappedGetter(state) { | ||
return rawGetter(getNestedState(state, modulePath)); | ||
}; | ||
}); | ||
wrapGetters(getters, module.getters, modulePath); | ||
} | ||
@@ -466,7 +520,21 @@ extractModuleGetters(getters, module.modules, modulePath); | ||
function wrapGetters(getters, moduleGetters, modulePath, force) { | ||
Object.keys(moduleGetters).forEach(function (getterKey) { | ||
var rawGetter = moduleGetters[getterKey]; | ||
if (getters[getterKey] && !force) { | ||
console.error('[vuex] duplicate getter key: ' + getterKey); | ||
return; | ||
} | ||
getters[getterKey] = function wrappedGetter(store) { | ||
return rawGetter(getNestedState(store.state, modulePath), // local state | ||
store.getters, // getters | ||
store.state // root state | ||
); | ||
}; | ||
}); | ||
} | ||
function enableStrictMode(store) { | ||
store._vm.$watch('state', function () { | ||
if (!store._committing) { | ||
throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.'); | ||
} | ||
assert(store._committing, 'Do not mutate vuex store state outside mutation handlers.'); | ||
}, { deep: true, sync: true }); | ||
@@ -484,5 +552,5 @@ } | ||
function getNestedState(state, path) { | ||
return path.reduce(function (state, key) { | ||
return path.length ? path.reduce(function (state, key) { | ||
return state[key]; | ||
}, state); | ||
}, state) : state; | ||
} | ||
@@ -507,2 +575,4 @@ | ||
install: install, | ||
mapState: mapState, | ||
mapMutations: mapMutations, | ||
mapGetters: mapGetters, | ||
@@ -509,0 +579,0 @@ mapActions: mapActions |
/*! | ||
* Vuex v2.0.0-rc.1 | ||
* Vuex v2.0.0-rc.3 | ||
* (c) 2016 Evan You | ||
* Released under the MIT License. | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Vuex=e()}(this,function(){"use strict";function t(t){v&&(v.emit("vuex:init",t),v.on("vuex:travel-to-state",function(e){t.replaceState(e)}),t.subscribe(function(t,e){v.emit("vuex:mutation",t,e)}))}function e(t){function e(){var t=this.$options;t.store?this.$store=t.store:t.parent&&t.parent.$store&&(this.$store=t.parent.$store)}var n=Number(t.version.split(".")[0]);if(n>=2){var o=t.config._lifecycleHooks.indexOf("init")>-1;t.mixin(o?{init:e}:{beforeCreate:e})}else!function(){var n=t.prototype._init;t.prototype._init=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];t.init=t.init?[e].concat(t.init):e,n.call(this,t)}}()}function n(t){var e={};return i(t).forEach(function(t){var n=t.key,o=t.val;e[n]=function(){return o in this.$store.getters||console.error("[vuex] unknown getter: "+o),this.$store.getters[o]}}),e}function o(t){var e={};return i(t).forEach(function(t){var n=t.key,o=t.val;e[n]=function(){for(var t,e=arguments.length,n=Array(e),i=0;i<e;i++)n[i]=arguments[i];return(t=this.$store).dispatch.apply(t,[o].concat(n))}}),e}function i(t){return Array.isArray(t)?t.map(function(t){return{key:t,val:t}}):Object.keys(t).map(function(e){return{key:e,val:t[e]}})}function r(t,e,n){t.getters={};var o={};Object.keys(n).forEach(function(e){var i=n[e];o[e]=function(){return i(t._vm.state)},Object.defineProperty(t.getters,e,{get:function(){return t._vm[e]}})});var i=y.config.silent;y.config.silent=!0,t._vm=new y({data:{state:e},computed:o}),y.config.silent=i}function s(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2];return e?(Object.keys(e).forEach(function(o){var i=e[o],r=n.concat(o);i.getters&&Object.keys(i.getters).forEach(function(e){var n=i.getters[e];return t[e]?void console.error("[vuex] duplicate getter key: "+e):void(t[e]=function(t){return n(f(t,r))})}),s(t,i.modules,r)}),t):t}function u(t){t._vm.$watch("state",function(){if(!t._committing)throw new Error("[vuex] Do not mutate vuex store state outside mutation handlers.")},{deep:!0,sync:!0})}function c(t){return null!==t&&"object"===("undefined"==typeof t?"undefined":h(t))}function a(t){return t&&"function"==typeof t.then}function f(t,e){return e.reduce(function(t,e){return t[e]},t)}function l(t){return y?void console.error("[vuex] already installed. Vue.use(Vuex) should be called only once."):(y=t,void e(y))}var v="undefined"!=typeof window&&window.__VUE_DEVTOOLS_GLOBAL_HOOK__,h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},m=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},d=function(){function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,n,o){return n&&t(e.prototype,n),o&&t(e,o),e}}(),y=void 0,p=function(){function e(){var n=this,o=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];if(m(this,e),!y)throw new Error("[vuex] must call Vue.use(Vuex) before creating a store instance.");if("undefined"==typeof Promise)throw new Error("[vuex] vuex requires a Promise polyfill in this browser.");var i=o.state,c=void 0===i?{}:i,a=o.modules,f=void 0===a?{}:a,l=o.plugins,v=void 0===l?[]:l,h=o.strict,d=void 0!==h&&h;this._options=o,this._committing=!1,this._actions=Object.create(null),this._mutations=Object.create(null),this._subscribers=[];var p=this,g=this.dispatch,b=this.commit;this.dispatch=function(t,e){return g.call(p,t,e)},this.commit=function(t,e){return b.call(p,t,e)};var _=s(o.getters,f);r(this,c,_),this.module([],o),d&&u(this),v.concat(t).forEach(function(t){return t(n)})}return d(e,[{key:"replaceState",value:function(t){this._committing=!0,this._vm.state=t,this._committing=!1}},{key:"module",value:function(t,e,n){var o=this;if("string"==typeof t&&(t=[t]),!Array.isArray(t))throw new Error("[vuex] module path must be a string or an Array.");var i=!t.length,r=e.state,s=e.actions,u=e.mutations,c=e.modules;if(!i&&!n){var a=f(this.state,t.slice(0,-1)),l=t[t.length-1];y.set(a,l,r||{})}u&&Object.keys(u).forEach(function(e){o.mutation(e,u[e],t)}),s&&Object.keys(s).forEach(function(e){o.action(e,s[e],t)}),c&&Object.keys(c).forEach(function(e){o.module(t.concat(e),c[e],n)})}},{key:"mutation",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2],o=this._mutations[t]||(this._mutations[t]=[]),i=this;o.push(function(t){e(f(i.state,n),t)})}},{key:"action",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2],o=this._actions[t]||(this._actions[t]=[]),i=this,r=this.dispatch,s=this.commit;o.push(function(o,u){var c=e({dispatch:r,commit:s,state:f(i.state,n)},o,u);return a(c)||(c=Promise.resolve(c)),c["catch"](function(e){console.error('[vuex] error in Promise returned from action "'+t+'":'),console.error(e)})})}},{key:"commit",value:function(t,e){var n=this,o=void 0;c(t)&&t.type?(e=o=t,t=t.type):o={type:t,payload:e};var i=this._mutations[t];return i?(this._committing=!0,i.forEach(function(t){t(e)}),this._committing=!1,void(e&&e.silent||this._subscribers.forEach(function(t){return t(o,n.state)}))):void console.error("[vuex] unknown mutation type: "+t)}},{key:"dispatch",value:function(t,e){var n=this._actions[t];return n?n.length>1?Promise.all(n.map(function(t){return t(e)})):n[0](e):void console.error("[vuex] unknown action type: "+t)}},{key:"subscribe",value:function(t){var e=this._subscribers;return e.indexOf(t)<0&&e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}},{key:"hotUpdate",value:function(t){var e=this;this._actions=Object.create(null),this._mutations=Object.create(null);var n=this._options;if(t.actions&&(n.actions=t.actions),t.mutations&&(n.mutations=t.mutations),t.modules)for(var o in t.modules)n.modules[o]=t.modules[o];this.module([],n,!0);var i=s(t.getters,t.modules);Object.keys(i).length&&!function(){var t=e._vm;r(e,e.state,i),e.strict&&u(e),e._committing=!0,t.state=null,e._committing=!1,y.nextTick(function(){return t.$destroy()})}()}},{key:"state",get:function(){return this._vm.state},set:function(t){throw new Error("[vuex] Use store.replaceState() to explicit replace store state.")}}]),e}();"undefined"!=typeof window&&window.Vue&&l(window.Vue);var g={Store:p,install:l,mapGetters:n,mapActions:o};return g}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Vuex=e()}(this,function(){"use strict";function t(t){p&&(t._devtoolHook=p,p.emit("vuex:init",t),p.on("vuex:travel-to-state",function(e){t.replaceState(e)}),t.subscribe(function(t,e){p.emit("vuex:mutation",t,e)}))}function e(t){function e(){var t=this.$options;t.store?this.$store=t.store:t.parent&&t.parent.$store&&(this.$store=t.parent.$store)}var n=Number(t.version.split(".")[0]);if(n>=2){var i=t.config._lifecycleHooks.indexOf("init")>-1;t.mixin(i?{init:e}:{beforeCreate:e})}else!function(){var n=t.prototype._init;t.prototype._init=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];t.init=t.init?[e].concat(t.init):e,n.call(this,t)}}()}function n(t){var e={};return Object.keys(t).forEach(function(n){var i=t[n];e[n]=function(){return i.call(this,this.$store.state,this.$store.getters)}}),e}function i(t){var e={};return s(t).forEach(function(t){var n=t.key,i=t.val;e[n]=function(){for(var t,e=arguments.length,n=Array(e),o=0;o<e;o++)n[o]=arguments[o];return(t=this.$store).commit.apply(t,[i].concat(n))}}),e}function o(t){var e={};return s(t).forEach(function(t){var n=t.key,i=t.val;e[n]=function(){return i in this.$store.getters||console.error("[vuex] unknown getter: "+i),this.$store.getters[i]}}),e}function r(t){var e={};return s(t).forEach(function(t){var n=t.key,i=t.val;e[n]=function(){for(var t,e=arguments.length,n=Array(e),o=0;o<e;o++)n[o]=arguments[o];return(t=this.$store).dispatch.apply(t,[i].concat(n))}}),e}function s(t){return Array.isArray(t)?t.map(function(t){return{key:t,val:t}}):Object.keys(t).map(function(e){return{key:e,val:t[e]}})}function u(t,e){if(!t)throw new Error("[vuex] "+e)}function c(t,e,n){t.getters={};var i={};Object.keys(n).forEach(function(e){var o=n[e];i[e]=function(){return o(t)},Object.defineProperty(t.getters,e,{get:function(){return t._vm[e]}})});var o=b.config.silent;b.config.silent=!0,t._vm=new b({data:{state:e},computed:i}),b.config.silent=o}function a(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2];return n.length||f(t,t,n,!0),e?(Object.keys(e).forEach(function(i){var o=e[i],r=n.concat(i);o.getters&&f(t,o.getters,r),a(t,o.modules,r)}),t):t}function f(t,e,n,i){Object.keys(e).forEach(function(o){var r=e[o];return t[o]&&!i?void console.error("[vuex] duplicate getter key: "+o):void(t[o]=function(t){return r(m(t.state,n),t.getters,t.state)})})}function l(t){t._vm.$watch("state",function(){u(t._committing,"Do not mutate vuex store state outside mutation handlers.")},{deep:!0,sync:!0})}function h(t){return null!==t&&"object"===("undefined"==typeof t?"undefined":y(t))}function v(t){return t&&"function"==typeof t.then}function m(t,e){return e.length?e.reduce(function(t,e){return t[e]},t):t}function d(t){return b?void console.error("[vuex] already installed. Vue.use(Vuex) should be called only once."):(b=t,void e(b))}var p="undefined"!=typeof window&&window.__VUE_DEVTOOLS_GLOBAL_HOOK__,y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},g=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},_=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),b=void 0,k=function(){function e(){var n=this,i=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];g(this,e),u(b,"must call Vue.use(Vuex) before creating a store instance."),u("undefined"!=typeof Promise,"vuex requires a Promise polyfill in this browser.");var o=i.state,r=void 0===o?{}:o,s=i.modules,f=void 0===s?{}:s,h=i.plugins,v=void 0===h?[]:h,m=i.strict,d=void 0!==m&&m;this._options=i,this._committing=!1,this._actions=Object.create(null),this._mutations=Object.create(null),this._subscribers=[],this._pendingActions=[];var p=this,y=this.dispatch,_=this.commit;this.dispatch=function(t,e){return y.call(p,t,e)},this.commit=function(t,e){return _.call(p,t,e)};var k=a(i.getters,f);c(this,r,k),this.module([],i),d&&l(this),v.concat(t).forEach(function(t){return t(n)})}return _(e,[{key:"replaceState",value:function(t){this._committing=!0,this._vm.state=t,this._committing=!1}},{key:"module",value:function(t,e,n){var i=this;this._committing=!0,"string"==typeof t&&(t=[t]),u(Array.isArray(t),"module path must be a string or an Array.");var o=!t.length,r=e.state,s=e.actions,c=e.mutations,a=e.modules;if(!o&&!n){var f=m(this.state,t.slice(0,-1)),l=t[t.length-1];b.set(f,l,r||{})}c&&Object.keys(c).forEach(function(e){i.mutation(e,c[e],t)}),s&&Object.keys(s).forEach(function(e){i.action(e,s[e],t)}),a&&Object.keys(a).forEach(function(e){i.module(t.concat(e),a[e],n)}),this._committing=!1}},{key:"mutation",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2],i=this._mutations[t]||(this._mutations[t]=[]),o=this;i.push(function(t){e(m(o.state,n),t)})}},{key:"action",value:function(t,e){var n=arguments.length<=2||void 0===arguments[2]?[]:arguments[2],i=this._actions[t]||(this._actions[t]=[]),o=this,r=this.dispatch,s=this.commit;i.push(function(t,i){var u=e({dispatch:r,commit:s,getters:o.getters,state:m(o.state,n),rootState:o.state},t,i);return v(u)||(u=Promise.resolve(u)),o._devtoolHook?u["catch"](function(t){throw o._devtoolHook.emit("vuex:error",t),t}):u})}},{key:"commit",value:function(t,e){var n=this,i=void 0;h(t)&&t.type?(e=i=t,t=t.type):i={type:t,payload:e};var o=this._mutations[t];return o?(this._committing=!0,o.forEach(function(t){t(e)}),this._committing=!1,void(e&&e.silent||this._subscribers.forEach(function(t){return t(i,n.state)}))):void console.error("[vuex] unknown mutation type: "+t)}},{key:"dispatch",value:function(t,e){var n=this._actions[t];if(!n)return void console.error("[vuex] unknown action type: "+t);var i=n.length>1?Promise.all(n.map(function(t){return t(e)})):n[0](e),o=this._pendingActions;return o.push(i),i.then(function(t){return o.splice(o.indexOf(i),1),t})}},{key:"onActionsResolved",value:function(t){Promise.all(this._pendingActions).then(t)}},{key:"subscribe",value:function(t){var e=this._subscribers;return e.indexOf(t)<0&&e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}},{key:"watch",value:function(t,e,n){var i=this;return u("function"==typeof t,"store.watch only accepts a function."),this._vm.$watch(function(){return t(i.state)},e,n)}},{key:"hotUpdate",value:function(t){var e=this;this._actions=Object.create(null),this._mutations=Object.create(null);var n=this._options;if(t.actions&&(n.actions=t.actions),t.mutations&&(n.mutations=t.mutations),t.modules)for(var i in t.modules)n.modules[i]=t.modules[i];this.module([],n,!0);var o=a(t.getters,t.modules);Object.keys(o).length&&!function(){var t=e._vm;c(e,e.state,o),e.strict&&l(e),e._committing=!0,t.state=null,e._committing=!1,b.nextTick(function(){return t.$destroy()})}()}},{key:"state",get:function(){return this._vm.state},set:function(t){u(!1,"Use store.replaceState() to explicit replace store state.")}}]),e}();"undefined"!=typeof window&&window.Vue&&d(window.Vue);var w={Store:k,install:d,mapState:n,mapMutations:i,mapGetters:o,mapActions:r};return w}); |
{ | ||
"name": "vuex", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.3", | ||
"description": "state management for Vue.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/vuex.js", |
@@ -0,1 +1,22 @@ | ||
export function mapState (map) { | ||
const res = {} | ||
Object.keys(map).forEach(key => { | ||
const fn = map[key] | ||
res[key] = function mappedState () { | ||
return fn.call(this, this.$store.state, this.$store.getters) | ||
} | ||
}) | ||
return res | ||
} | ||
export function mapMutations (mutations) { | ||
const res = {} | ||
normalizeMap(mutations).forEach(({ key, val }) => { | ||
res[key] = function mappedMutation (...args) { | ||
return this.$store.commit(val, ...args) | ||
} | ||
}) | ||
return res | ||
} | ||
export function mapGetters (getters) { | ||
@@ -2,0 +23,0 @@ const res = {} |
111
src/index.js
import devtoolPlugin from './plugins/devtool' | ||
import applyMixin from './mixin' | ||
import { mapGetters, mapActions } from './helpers' | ||
import { mapState, mapMutations, mapGetters, mapActions } from './helpers' | ||
@@ -9,14 +9,5 @@ let Vue // bind on install | ||
constructor (options = {}) { | ||
if (!Vue) { | ||
throw new Error( | ||
'[vuex] must call Vue.use(Vuex) before creating a store instance.' | ||
) | ||
} | ||
assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`) | ||
assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`) | ||
if (typeof Promise === 'undefined') { | ||
throw new Error( | ||
'[vuex] vuex requires a Promise polyfill in this browser.' | ||
) | ||
} | ||
const { | ||
@@ -35,2 +26,3 @@ state = {}, | ||
this._subscribers = [] | ||
this._pendingActions = [] | ||
@@ -66,3 +58,3 @@ // bind commit and dispatch to self | ||
set state (v) { | ||
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.') | ||
assert(false, `Use store.replaceState() to explicit replace store state.`) | ||
} | ||
@@ -77,6 +69,5 @@ | ||
module (path, module, hot) { | ||
this._committing = true | ||
if (typeof path === 'string') path = [path] | ||
if (!Array.isArray(path)) { | ||
throw new Error('[vuex] module path must be a string or an Array.') | ||
} | ||
assert(Array.isArray(path), `module path must be a string or an Array.`) | ||
@@ -116,2 +107,3 @@ const isRoot = !path.length | ||
} | ||
this._committing = false | ||
} | ||
@@ -135,3 +127,5 @@ | ||
commit, | ||
state: getNestedState(store.state, path) | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb) | ||
@@ -141,6 +135,10 @@ if (!isPromise(res)) { | ||
} | ||
return res.catch(err => { | ||
console.error(`[vuex] error in Promise returned from action "${type}":`) | ||
console.error(err) | ||
}) | ||
if (store._devtoolHook) { | ||
return res.catch(err => { | ||
store._devtoolHook.emit('vuex:error', err) | ||
throw err | ||
}) | ||
} else { | ||
return res | ||
} | ||
}) | ||
@@ -176,11 +174,20 @@ } | ||
if (!entry) { | ||
debugger | ||
console.error(`[vuex] unknown action type: ${type}`) | ||
return | ||
} | ||
return entry.length > 1 | ||
const res = entry.length > 1 | ||
? Promise.all(entry.map(handler => handler(payload))) | ||
: entry[0](payload) | ||
const pending = this._pendingActions | ||
pending.push(res) | ||
return res.then(value => { | ||
pending.splice(pending.indexOf(res), 1) | ||
return value | ||
}) | ||
} | ||
onActionsResolved (cb) { | ||
Promise.all(this._pendingActions).then(cb) | ||
} | ||
subscribe (fn) { | ||
@@ -199,2 +206,7 @@ const subs = this._subscribers | ||
watch (getter, cb, options) { | ||
assert(typeof getter === 'function', `store.watch only accepts a function.`) | ||
return this._vm.$watch(() => getter(this.state), cb, options) | ||
} | ||
hotUpdate (newOptions) { | ||
@@ -235,2 +247,6 @@ this._actions = Object.create(null) | ||
function assert (condition, msg) { | ||
if (!condition) throw new Error(`[vuex] ${msg}`) | ||
} | ||
function initStoreState (store, state, getters) { | ||
@@ -243,3 +259,3 @@ // bind getters | ||
// use computed to leverage its lazy-caching mechanism | ||
computed[key] = () => fn(store._vm.state) | ||
computed[key] = () => fn(store) | ||
Object.defineProperty(store.getters, key, { | ||
@@ -263,3 +279,8 @@ get: () => store._vm[key] | ||
function extractModuleGetters (getters = {}, modules = {}, path = []) { | ||
if (!modules) return getters | ||
if (!path.length) { | ||
wrapGetters(getters, getters, path, true) | ||
} | ||
if (!modules) { | ||
return getters | ||
} | ||
Object.keys(modules).forEach(key => { | ||
@@ -269,12 +290,3 @@ const module = modules[key] | ||
if (module.getters) { | ||
Object.keys(module.getters).forEach(getterKey => { | ||
const rawGetter = module.getters[getterKey] | ||
if (getters[getterKey]) { | ||
console.error(`[vuex] duplicate getter key: ${getterKey}`) | ||
return | ||
} | ||
getters[getterKey] = function wrappedGetter (state) { | ||
return rawGetter(getNestedState(state, modulePath)) | ||
} | ||
}) | ||
wrapGetters(getters, module.getters, modulePath) | ||
} | ||
@@ -286,9 +298,22 @@ extractModuleGetters(getters, module.modules, modulePath) | ||
function wrapGetters (getters, moduleGetters, modulePath, force) { | ||
Object.keys(moduleGetters).forEach(getterKey => { | ||
const rawGetter = moduleGetters[getterKey] | ||
if (getters[getterKey] && !force) { | ||
console.error(`[vuex] duplicate getter key: ${getterKey}`) | ||
return | ||
} | ||
getters[getterKey] = function wrappedGetter (store) { | ||
return rawGetter( | ||
getNestedState(store.state, modulePath), // local state | ||
store.getters, // getters | ||
store.state // root state | ||
) | ||
} | ||
}) | ||
} | ||
function enableStrictMode (store) { | ||
store._vm.$watch('state', () => { | ||
if (!store._committing) { | ||
throw new Error( | ||
'[vuex] Do not mutate vuex store state outside mutation handlers.' | ||
) | ||
} | ||
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`) | ||
}, { deep: true, sync: true }) | ||
@@ -306,3 +331,5 @@ } | ||
function getNestedState (state, path) { | ||
return path.reduce((state, key) => state[key], state) | ||
return path.length | ||
? path.reduce((state, key) => state[key], state) | ||
: state | ||
} | ||
@@ -329,4 +356,6 @@ | ||
install, | ||
mapState, | ||
mapMutations, | ||
mapGetters, | ||
mapActions | ||
} |
@@ -1,2 +0,2 @@ | ||
const hook = | ||
const devtoolHook = | ||
typeof window !== 'undefined' && | ||
@@ -6,7 +6,9 @@ window.__VUE_DEVTOOLS_GLOBAL_HOOK__ | ||
export default function devtoolPlugin (store) { | ||
if (!hook) return | ||
if (!devtoolHook) return | ||
hook.emit('vuex:init', store) | ||
store._devtoolHook = devtoolHook | ||
hook.on('vuex:travel-to-state', targetState => { | ||
devtoolHook.emit('vuex:init', store) | ||
devtoolHook.on('vuex:travel-to-state', targetState => { | ||
store.replaceState(targetState) | ||
@@ -16,4 +18,4 @@ }) | ||
store.subscribe((mutation, state) => { | ||
hook.emit('vuex:mutation', mutation, state) | ||
devtoolHook.emit('vuex:mutation', mutation, state) | ||
}) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42803
985
0