Skip to content

Commit

Permalink
[build] 3.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Nov 10, 2019
1 parent d4d0430 commit 1ed9a03
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 96 deletions.
85 changes: 62 additions & 23 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vuex v3.1.1
* vuex v3.1.2
* (c) 2019 Evan You
* @license MIT
*/
Expand Down Expand Up @@ -323,6 +323,7 @@ var Store = function Store (options) {
this._modulesNamespaceMap = Object.create(null);
this._subscribers = [];
this._watcherVM = new Vue();
this._makeLocalGettersCache = Object.create(null);

// bind commit and dispatch to self
var store = this;
Expand Down Expand Up @@ -555,12 +556,14 @@ function resetStoreVM (store, state, hot) {

// bind store public getters
store.getters = {};
// reset local getters cache
store._makeLocalGettersCache = Object.create(null);
var wrappedGetters = store._wrappedGetters;
var computed = {};
forEachValue(wrappedGetters, function (fn, key) {
// use computed to leverage its lazy-caching mechanism
// direct inline function use will lead to closure preserving oldVm.
// using partial to return function with only arguments preserved in closure enviroment.
// using partial to return function with only arguments preserved in closure environment.
computed[key] = partial(fn, store);
Object.defineProperty(store.getters, key, {
get: function () { return store._vm[key]; },
Expand Down Expand Up @@ -604,6 +607,9 @@ function installModule (store, rootState, path, module, hot) {

// register in namespace map
if (module.namespaced) {
if (store._modulesNamespaceMap[namespace] && process.env.NODE_ENV !== 'production') {
console.error(("[vuex] duplicate namespace " + namespace + " for the namespaced module " + (path.join('/'))));
}
store._modulesNamespaceMap[namespace] = module;
}

Expand All @@ -612,6 +618,13 @@ function installModule (store, rootState, path, module, hot) {
var parentState = getNestedState(rootState, path.slice(0, -1));
var moduleName = path[path.length - 1];
store._withCommit(function () {
if (process.env.NODE_ENV !== 'production') {
if (moduleName in parentState) {
console.warn(
("[vuex] state field \"" + moduleName + "\" was overridden by a module with the same name at \"" + (path.join('.')) + "\"")
);
}
}
Vue.set(parentState, moduleName, module.state);
});
}
Expand Down Expand Up @@ -699,26 +712,28 @@ function makeLocalContext (store, namespace, path) {
}

function makeLocalGetters (store, namespace) {
var gettersProxy = {};

var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }

// extract local getter type
var localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: function () { return store.getters[type]; },
enumerable: true
if (!store._makeLocalGettersCache[namespace]) {
var gettersProxy = {};
var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }

// extract local getter type
var localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: function () { return store.getters[type]; },
enumerable: true
});
});
});
store._makeLocalGettersCache[namespace] = gettersProxy;
}

return gettersProxy
return store._makeLocalGettersCache[namespace]
}

function registerMutation (store, type, handler, local) {
Expand All @@ -730,15 +745,15 @@ function registerMutation (store, type, handler, local) {

function registerAction (store, type, handler, local) {
var entry = store._actions[type] || (store._actions[type] = []);
entry.push(function wrappedActionHandler (payload, cb) {
entry.push(function wrappedActionHandler (payload) {
var res = handler.call(store, {
dispatch: local.dispatch,
commit: local.commit,
getters: local.getters,
state: local.state,
rootGetters: store.getters,
rootState: store.state
}, payload, cb);
}, payload);
if (!isPromise(res)) {
res = Promise.resolve(res);
}
Expand Down Expand Up @@ -819,6 +834,9 @@ function install (_Vue) {
*/
var mapState = normalizeNamespace(function (namespace, states) {
var res = {};
if (process.env.NODE_ENV !== 'production' && !isValidMap(states)) {
console.error('[vuex] mapState: mapper parameter must be either an Array or an Object');
}
normalizeMap(states).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
Expand Down Expand Up @@ -852,6 +870,9 @@ var mapState = normalizeNamespace(function (namespace, states) {
*/
var mapMutations = normalizeNamespace(function (namespace, mutations) {
var res = {};
if (process.env.NODE_ENV !== 'production' && !isValidMap(mutations)) {
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object');
}
normalizeMap(mutations).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
Expand Down Expand Up @@ -885,6 +906,9 @@ var mapMutations = normalizeNamespace(function (namespace, mutations) {
*/
var mapGetters = normalizeNamespace(function (namespace, getters) {
var res = {};
if (process.env.NODE_ENV !== 'production' && !isValidMap(getters)) {
console.error('[vuex] mapGetters: mapper parameter must be either an Array or an Object');
}
normalizeMap(getters).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
Expand Down Expand Up @@ -915,6 +939,9 @@ var mapGetters = normalizeNamespace(function (namespace, getters) {
*/
var mapActions = normalizeNamespace(function (namespace, actions) {
var res = {};
if (process.env.NODE_ENV !== 'production' && !isValidMap(actions)) {
console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object');
}
normalizeMap(actions).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
Expand Down Expand Up @@ -960,11 +987,23 @@ var createNamespacedHelpers = function (namespace) { return ({
* @return {Object}
*/
function normalizeMap (map) {
if (!isValidMap(map)) {
return []
}
return Array.isArray(map)
? map.map(function (key) { return ({ key: key, val: key }); })
: Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
}

/**
* Validate whether given map is valid or not
* @param {*} map
* @return {Boolean}
*/
function isValidMap (map) {
return Array.isArray(map) || isObject(map)
}

/**
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
* @param {Function} fn
Expand Down Expand Up @@ -1000,7 +1039,7 @@ function getModuleByNamespace (store, helper, namespace) {
var index = {
Store: Store,
install: install,
version: '3.1.1',
version: '3.1.2',
mapState: mapState,
mapMutations: mapMutations,
mapGetters: mapGetters,
Expand Down
85 changes: 62 additions & 23 deletions dist/vuex.esm.browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vuex v3.1.1
* vuex v3.1.2
* (c) 2019 Evan You
* @license MIT
*/
Expand Down Expand Up @@ -316,6 +316,7 @@ class Store {
this._modulesNamespaceMap = Object.create(null);
this._subscribers = [];
this._watcherVM = new Vue();
this._makeLocalGettersCache = Object.create(null);

// bind commit and dispatch to self
const store = this;
Expand Down Expand Up @@ -532,12 +533,14 @@ function resetStoreVM (store, state, hot) {

// bind store public getters
store.getters = {};
// reset local getters cache
store._makeLocalGettersCache = Object.create(null);
const wrappedGetters = store._wrappedGetters;
const computed = {};
forEachValue(wrappedGetters, (fn, key) => {
// use computed to leverage its lazy-caching mechanism
// direct inline function use will lead to closure preserving oldVm.
// using partial to return function with only arguments preserved in closure enviroment.
// using partial to return function with only arguments preserved in closure environment.
computed[key] = partial(fn, store);
Object.defineProperty(store.getters, key, {
get: () => store._vm[key],
Expand Down Expand Up @@ -581,6 +584,9 @@ function installModule (store, rootState, path, module, hot) {

// register in namespace map
if (module.namespaced) {
if (store._modulesNamespaceMap[namespace] && "development" !== 'production') {
console.error(`[vuex] duplicate namespace ${namespace} for the namespaced module ${path.join('/')}`);
}
store._modulesNamespaceMap[namespace] = module;
}

Expand All @@ -589,6 +595,13 @@ function installModule (store, rootState, path, module, hot) {
const parentState = getNestedState(rootState, path.slice(0, -1));
const moduleName = path[path.length - 1];
store._withCommit(() => {
{
if (moduleName in parentState) {
console.warn(
`[vuex] state field "${moduleName}" was overridden by a module with the same name at "${path.join('.')}"`
);
}
}
Vue.set(parentState, moduleName, module.state);
});
}
Expand Down Expand Up @@ -674,26 +687,28 @@ function makeLocalContext (store, namespace, path) {
}

function makeLocalGetters (store, namespace) {
const gettersProxy = {};

const splitPos = namespace.length;
Object.keys(store.getters).forEach(type => {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) return

// extract local getter type
const localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: () => store.getters[type],
enumerable: true
if (!store._makeLocalGettersCache[namespace]) {
const gettersProxy = {};
const splitPos = namespace.length;
Object.keys(store.getters).forEach(type => {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) return

// extract local getter type
const localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: () => store.getters[type],
enumerable: true
});
});
});
store._makeLocalGettersCache[namespace] = gettersProxy;
}

return gettersProxy
return store._makeLocalGettersCache[namespace]
}

function registerMutation (store, type, handler, local) {
Expand All @@ -705,15 +720,15 @@ function registerMutation (store, type, handler, local) {

function registerAction (store, type, handler, local) {
const entry = store._actions[type] || (store._actions[type] = []);
entry.push(function wrappedActionHandler (payload, cb) {
entry.push(function wrappedActionHandler (payload) {
let res = handler.call(store, {
dispatch: local.dispatch,
commit: local.commit,
getters: local.getters,
state: local.state,
rootGetters: store.getters,
rootState: store.state
}, payload, cb);
}, payload);
if (!isPromise(res)) {
res = Promise.resolve(res);
}
Expand Down Expand Up @@ -794,6 +809,9 @@ function install (_Vue) {
*/
const mapState = normalizeNamespace((namespace, states) => {
const res = {};
if (!isValidMap(states)) {
console.error('[vuex] mapState: mapper parameter must be either an Array or an Object');
}
normalizeMap(states).forEach(({ key, val }) => {
res[key] = function mappedState () {
let state = this.$store.state;
Expand Down Expand Up @@ -824,6 +842,9 @@ const mapState = normalizeNamespace((namespace, states) => {
*/
const mapMutations = normalizeNamespace((namespace, mutations) => {
const res = {};
if (!isValidMap(mutations)) {
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object');
}
normalizeMap(mutations).forEach(({ key, val }) => {
res[key] = function mappedMutation (...args) {
// Get the commit method from store
Expand Down Expand Up @@ -851,6 +872,9 @@ const mapMutations = normalizeNamespace((namespace, mutations) => {
*/
const mapGetters = normalizeNamespace((namespace, getters) => {
const res = {};
if (!isValidMap(getters)) {
console.error('[vuex] mapGetters: mapper parameter must be either an Array or an Object');
}
normalizeMap(getters).forEach(({ key, val }) => {
// The namespace has been mutated by normalizeNamespace
val = namespace + val;
Expand Down Expand Up @@ -878,6 +902,9 @@ const mapGetters = normalizeNamespace((namespace, getters) => {
*/
const mapActions = normalizeNamespace((namespace, actions) => {
const res = {};
if (!isValidMap(actions)) {
console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object');
}
normalizeMap(actions).forEach(({ key, val }) => {
res[key] = function mappedAction (...args) {
// get dispatch function from store
Expand Down Expand Up @@ -917,11 +944,23 @@ const createNamespacedHelpers = (namespace) => ({
* @return {Object}
*/
function normalizeMap (map) {
if (!isValidMap(map)) {
return []
}
return Array.isArray(map)
? map.map(key => ({ key, val: key }))
: Object.keys(map).map(key => ({ key, val: map[key] }))
}

/**
* Validate whether given map is valid or not
* @param {*} map
* @return {Boolean}
*/
function isValidMap (map) {
return Array.isArray(map) || isObject(map)
}

/**
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
* @param {Function} fn
Expand Down Expand Up @@ -957,7 +996,7 @@ function getModuleByNamespace (store, helper, namespace) {
var index_esm = {
Store,
install,
version: '3.1.1',
version: '3.1.2',
mapState,
mapMutations,
mapGetters,
Expand Down
Loading

0 comments on commit 1ed9a03

Please sign in to comment.