Comparing version 2.0.0-rc.3 to 2.0.0-rc.4
815
dist/vuex.js
@@ -1,5 +0,5 @@ | ||
/*! | ||
* Vuex v2.0.0-rc.3 | ||
/** | ||
* vuex v2.0.0-rc.4 | ||
* (c) 2016 Evan You | ||
* Released under the MIT License. | ||
* @license MIT | ||
*/ | ||
@@ -12,38 +12,40 @@ (function (global, factory) { | ||
var devtoolHook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; | ||
var devtoolHook = | ||
typeof window !== 'undefined' && | ||
window.__VUE_DEVTOOLS_GLOBAL_HOOK__ | ||
function devtoolPlugin(store) { | ||
if (!devtoolHook) return; | ||
function devtoolPlugin (store) { | ||
if (!devtoolHook) return | ||
store._devtoolHook = devtoolHook; | ||
store._devtoolHook = devtoolHook | ||
devtoolHook.emit('vuex:init', store); | ||
devtoolHook.emit('vuex:init', store) | ||
devtoolHook.on('vuex:travel-to-state', function (targetState) { | ||
store.replaceState(targetState); | ||
}); | ||
store.replaceState(targetState) | ||
}) | ||
store.subscribe(function (mutation, state) { | ||
devtoolHook.emit('vuex:mutation', mutation, state); | ||
}); | ||
devtoolHook.emit('vuex:mutation', mutation, state) | ||
}) | ||
} | ||
function applyMixin (Vue) { | ||
var version = Number(Vue.version.split('.')[0]); | ||
var version = Number(Vue.version.split('.')[0]) | ||
if (version >= 2) { | ||
var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1; | ||
Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit }); | ||
var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1 | ||
Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit }) | ||
} else { | ||
(function () { | ||
// override init and inject vuex init procedure | ||
// for 1.x backwards compatibility. | ||
var _init = Vue.prototype._init; | ||
Vue.prototype._init = function () { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
// override init and inject vuex init procedure | ||
// for 1.x backwards compatibility. | ||
var _init = Vue.prototype._init | ||
Vue.prototype._init = function (options) { | ||
if ( options === void 0 ) options = {}; | ||
options.init = options.init ? [vuexInit].concat(options.init) : vuexInit; | ||
_init.call(this, options); | ||
}; | ||
})(); | ||
options.init = options.init | ||
? [vuexInit].concat(options.init) | ||
: vuexInit | ||
_init.call(this, options) | ||
} | ||
} | ||
@@ -55,9 +57,9 @@ | ||
function vuexInit() { | ||
var options = this.$options; | ||
function vuexInit () { | ||
var options = this.$options | ||
// store injection | ||
if (options.store) { | ||
this.$store = options.store; | ||
this.$store = options.store | ||
} else if (options.parent && options.parent.$store) { | ||
this.$store = options.parent.$store; | ||
this.$store = options.parent.$store | ||
} | ||
@@ -67,404 +69,291 @@ } | ||
function mapState(map) { | ||
var res = {}; | ||
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 mapState (states) { | ||
var res = {} | ||
normalizeMap(states).forEach(function (ref) { | ||
var key = ref.key; | ||
var val = ref.val; | ||
res[key] = function mappedState () { | ||
return typeof val === 'function' | ||
? val.call(this, this.$store.state, this.$store.getters) | ||
: this.$store.state[val] | ||
} | ||
}) | ||
return res | ||
} | ||
function mapMutations(mutations) { | ||
var res = {}; | ||
normalizeMap(mutations).forEach(function (_ref) { | ||
var key = _ref.key; | ||
var val = _ref.val; | ||
function mapMutations (mutations) { | ||
var res = {} | ||
normalizeMap(mutations).forEach(function (ref) { | ||
var key = ref.key; | ||
var val = ref.val; | ||
res[key] = function mappedMutation() { | ||
var _$store; | ||
res[key] = function mappedMutation () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
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; | ||
return this.$store.commit.apply(this.$store, [val].concat(args)) | ||
} | ||
}) | ||
return res | ||
} | ||
function mapGetters(getters) { | ||
var res = {}; | ||
normalizeMap(getters).forEach(function (_ref2) { | ||
var key = _ref2.key; | ||
var val = _ref2.val; | ||
function mapGetters (getters) { | ||
var res = {} | ||
normalizeMap(getters).forEach(function (ref) { | ||
var key = ref.key; | ||
var val = ref.val; | ||
res[key] = function mappedGetter() { | ||
res[key] = function mappedGetter () { | ||
if (!(val in this.$store.getters)) { | ||
console.error("[vuex] unknown getter: " + val); | ||
console.error(("[vuex] unknown getter: " + val)) | ||
} | ||
return this.$store.getters[val]; | ||
}; | ||
}); | ||
return res; | ||
return this.$store.getters[val] | ||
} | ||
}) | ||
return res | ||
} | ||
function mapActions(actions) { | ||
var res = {}; | ||
normalizeMap(actions).forEach(function (_ref3) { | ||
var key = _ref3.key; | ||
var val = _ref3.val; | ||
function mapActions (actions) { | ||
var res = {} | ||
normalizeMap(actions).forEach(function (ref) { | ||
var key = ref.key; | ||
var val = ref.val; | ||
res[key] = function mappedAction() { | ||
var _$store2; | ||
res[key] = function mappedAction () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return (_$store2 = this.$store).dispatch.apply(_$store2, [val].concat(args)); | ||
}; | ||
}); | ||
return res; | ||
return this.$store.dispatch.apply(this.$store, [val].concat(args)) | ||
} | ||
}) | ||
return res | ||
} | ||
function normalizeMap(map) { | ||
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] }; | ||
}); | ||
function normalizeMap (map) { | ||
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] }); }) | ||
} | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
}; | ||
var Vue // bind on install | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var Store = function Store (options) { | ||
var this$1 = this; | ||
if ( options === void 0 ) options = {}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
assert(Vue, "must call Vue.use(Vuex) before creating a store instance.") | ||
assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.") | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var state = options.state; if ( state === void 0 ) state = {}; | ||
var plugins = options.plugins; if ( plugins === void 0 ) plugins = []; | ||
var strict = options.strict; if ( strict === void 0 ) strict = false; | ||
var Vue = void 0; // bind on install | ||
// store internal state | ||
this._options = options | ||
this._committing = false | ||
this._actions = Object.create(null) | ||
this._mutations = Object.create(null) | ||
this._wrappedGetters = Object.create(null) | ||
this._runtimeModules = Object.create(null) | ||
this._subscribers = [] | ||
this._pendingActions = [] | ||
var Store = function () { | ||
function Store() { | ||
var _this = this; | ||
// bind commit and dispatch to self | ||
var store = this | ||
var ref = this; | ||
var dispatch = ref.dispatch; | ||
var commit = ref.commit; | ||
this.dispatch = function boundDispatch (type, payload) { | ||
return dispatch.call(store, type, payload) | ||
} | ||
this.commit = function boundCommit (type, payload) { | ||
return commit.call(store, type, payload) | ||
} | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
classCallCheck(this, Store); | ||
// strict mode | ||
this.strict = strict | ||
assert(Vue, 'must call Vue.use(Vuex) before creating a store instance.'); | ||
assert(typeof Promise !== 'undefined', 'vuex requires a Promise polyfill in this browser.'); | ||
// init root module. | ||
// this also recursively registers all sub-modules | ||
// and collects all module getters inside this._wrappedGetters | ||
installModule(this, state, [], options) | ||
var _options$state = options.state; | ||
var state = _options$state === undefined ? {} : _options$state; | ||
var _options$modules = options.modules; | ||
var modules = _options$modules === undefined ? {} : _options$modules; | ||
var _options$plugins = options.plugins; | ||
var plugins = _options$plugins === undefined ? [] : _options$plugins; | ||
var _options$strict = options.strict; | ||
var strict = _options$strict === undefined ? false : _options$strict; | ||
// initialize the store vm, which is responsible for the reactivity | ||
// (also registers _wrappedGetters as computed properties) | ||
resetStoreVM(this, state) | ||
// store internal state | ||
// apply plugins | ||
plugins.concat(devtoolPlugin).forEach(function (plugin) { return plugin(this$1); }) | ||
}; | ||
this._options = options; | ||
this._committing = false; | ||
this._actions = Object.create(null); | ||
this._mutations = Object.create(null); | ||
this._subscribers = []; | ||
this._pendingActions = []; | ||
var prototypeAccessors = { state: {} }; | ||
// bind commit and dispatch to self | ||
var store = this; | ||
var dispatch = this.dispatch; | ||
var commit = this.commit; | ||
prototypeAccessors.state.get = function () { | ||
return this._vm.state | ||
}; | ||
this.dispatch = function boundDispatch(type, payload) { | ||
return dispatch.call(store, type, payload); | ||
}; | ||
this.commit = function boundCommit(type, payload) { | ||
return commit.call(store, type, payload); | ||
}; | ||
prototypeAccessors.state.set = function (v) { | ||
assert(false, "Use store.replaceState() to explicit replace store state.") | ||
}; | ||
// init state and getters | ||
var getters = extractModuleGetters(options.getters, modules); | ||
initStoreState(this, state, getters); | ||
Store.prototype.commit = function commit (type, payload) { | ||
var this$1 = this; | ||
// apply root module | ||
this.module([], options); | ||
// check object-style commit | ||
var mutation | ||
if (isObject(type) && type.type) { | ||
payload = mutation = type | ||
type = type.type | ||
} else { | ||
mutation = { type: type, payload: payload } | ||
} | ||
var entry = this._mutations[type] | ||
if (!entry) { | ||
console.error(("[vuex] unknown mutation type: " + type)) | ||
return | ||
} | ||
this._withCommit(function () { | ||
entry.forEach(function commitIterator (handler) { | ||
handler(payload) | ||
}) | ||
}) | ||
if (!payload || !payload.silent) { | ||
this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); }) | ||
} | ||
}; | ||
// strict mode | ||
if (strict) enableStrictMode(this); | ||
Store.prototype.dispatch = function dispatch (type, payload) { | ||
var entry = this._actions[type] | ||
if (!entry) { | ||
console.error(("[vuex] unknown action type: " + type)) | ||
return | ||
} | ||
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 | ||
}) | ||
}; | ||
// apply plugins | ||
plugins.concat(devtoolPlugin).forEach(function (plugin) { | ||
return plugin(_this); | ||
}); | ||
Store.prototype.subscribe = function subscribe (fn) { | ||
var subs = this._subscribers | ||
if (subs.indexOf(fn) < 0) { | ||
subs.push(fn) | ||
} | ||
createClass(Store, [{ | ||
key: 'replaceState', | ||
value: function replaceState(state) { | ||
this._committing = true; | ||
this._vm.state = state; | ||
this._committing = false; | ||
return function () { | ||
var i = subs.indexOf(fn) | ||
if (i > -1) { | ||
subs.splice(i, 1) | ||
} | ||
}, { | ||
key: 'module', | ||
value: function module(path, _module, hot) { | ||
var _this2 = this; | ||
} | ||
}; | ||
this._committing = true; | ||
if (typeof path === 'string') path = [path]; | ||
assert(Array.isArray(path), 'module path must be a string or an Array.'); | ||
Store.prototype.watch = function watch (getter, cb, options) { | ||
var this$1 = this; | ||
var isRoot = !path.length; | ||
var state = _module.state; | ||
var actions = _module.actions; | ||
var mutations = _module.mutations; | ||
var modules = _module.modules; | ||
assert(typeof getter === 'function', "store.watch only accepts a function.") | ||
return this._vm.$watch(function () { return getter(this$1.state); }, cb, options) | ||
}; | ||
// set state | ||
Store.prototype.replaceState = function replaceState (state) { | ||
var this$1 = this; | ||
if (!isRoot && !hot) { | ||
var parentState = getNestedState(this.state, path.slice(0, -1)); | ||
if (!parentState) debugger; | ||
var moduleName = path[path.length - 1]; | ||
Vue.set(parentState, moduleName, state || {}); | ||
} | ||
this._withCommit(function () { | ||
this$1._vm.state = state | ||
}) | ||
}; | ||
if (mutations) { | ||
Object.keys(mutations).forEach(function (key) { | ||
_this2.mutation(key, mutations[key], path); | ||
}); | ||
} | ||
Store.prototype.registerModule = function registerModule (path, module) { | ||
if (typeof path === 'string') path = [path] | ||
assert(Array.isArray(path), "module path must be a string or an Array.") | ||
this._runtimeModules[path.join('.')] = module | ||
installModule(this, this.state, path, module) | ||
// reset store to update getters... | ||
resetStoreVM(this, this.state) | ||
}; | ||
if (actions) { | ||
Object.keys(actions).forEach(function (key) { | ||
_this2.action(key, actions[key], path); | ||
}); | ||
} | ||
Store.prototype.unregisterModule = function unregisterModule (path) { | ||
var this$1 = this; | ||
if (modules) { | ||
Object.keys(modules).forEach(function (key) { | ||
_this2.module(path.concat(key), modules[key], hot); | ||
}); | ||
} | ||
this._committing = false; | ||
} | ||
}, { | ||
key: 'mutation', | ||
value: function mutation(type, handler) { | ||
var path = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
if (typeof path === 'string') path = [path] | ||
assert(Array.isArray(path), "module path must be a string or an Array.") | ||
delete this._runtimeModules[path.join('.')] | ||
this._withCommit(function () { | ||
var parentState = getNestedState(this$1.state, path.slice(0, -1)) | ||
Vue.delete(parentState, path[path.length - 1]) | ||
}) | ||
resetStore(this) | ||
}; | ||
var entry = this._mutations[type] || (this._mutations[type] = []); | ||
var store = this; | ||
entry.push(function wrappedMutationHandler(payload) { | ||
handler(getNestedState(store.state, path), payload); | ||
}); | ||
Store.prototype.hotUpdate = function hotUpdate (newOptions) { | ||
var options = this._options | ||
if (newOptions.actions) { | ||
options.actions = newOptions.actions | ||
} | ||
if (newOptions.mutations) { | ||
options.mutations = newOptions.mutations | ||
} | ||
if (newOptions.getters) { | ||
options.getters = newOptions.getters | ||
} | ||
}, { | ||
key: 'action', | ||
value: function action(type, handler) { | ||
var path = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var entry = this._actions[type] || (this._actions[type] = []); | ||
var store = this; | ||
var dispatch = this.dispatch; | ||
var commit = this.commit; | ||
entry.push(function wrappedActionHandler(payload, cb) { | ||
var res = handler({ | ||
dispatch: dispatch, | ||
commit: commit, | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb); | ||
if (!isPromise(res)) { | ||
res = Promise.resolve(res); | ||
} | ||
if (store._devtoolHook) { | ||
return res.catch(function (err) { | ||
store._devtoolHook.emit('vuex:error', err); | ||
throw err; | ||
}); | ||
} else { | ||
return res; | ||
} | ||
}); | ||
if (newOptions.modules) { | ||
for (var key in newOptions.modules) { | ||
options.modules[key] = newOptions.modules[key] | ||
} | ||
}, { | ||
key: 'commit', | ||
value: function commit(type, payload) { | ||
var _this3 = this; | ||
} | ||
resetStore(this) | ||
}; | ||
// check object-style commit | ||
var mutation = void 0; | ||
if (isObject(type) && type.type) { | ||
payload = mutation = type; | ||
type = type.type; | ||
} else { | ||
mutation = { type: type, payload: payload }; | ||
} | ||
var entry = this._mutations[type]; | ||
if (!entry) { | ||
console.error('[vuex] unknown mutation type: ' + type); | ||
return; | ||
} | ||
this._committing = true; | ||
entry.forEach(function commitIterator(handler) { | ||
handler(payload); | ||
}); | ||
this._committing = false; | ||
if (!payload || !payload.silent) { | ||
this._subscribers.forEach(function (sub) { | ||
return sub(mutation, _this3.state); | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'dispatch', | ||
value: function dispatch(type, payload) { | ||
var entry = this._actions[type]; | ||
if (!entry) { | ||
console.error('[vuex] unknown action type: ' + type); | ||
return; | ||
} | ||
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', | ||
value: function subscribe(fn) { | ||
var subs = this._subscribers; | ||
if (subs.indexOf(fn) < 0) { | ||
subs.push(fn); | ||
} | ||
return function () { | ||
var i = subs.indexOf(fn); | ||
if (i > -1) { | ||
subs.splice(i, 1); | ||
} | ||
}; | ||
} | ||
}, { | ||
key: 'watch', | ||
value: function watch(getter, cb, options) { | ||
var _this4 = this; | ||
Store.prototype.onActionsResolved = function onActionsResolved (cb) { | ||
Promise.all(this._pendingActions).then(cb) | ||
}; | ||
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 _this5 = this; | ||
Store.prototype._withCommit = function _withCommit (fn) { | ||
var committing = this._committing | ||
this._committing = true | ||
fn() | ||
this._committing = committing | ||
}; | ||
this._actions = Object.create(null); | ||
this._mutations = Object.create(null); | ||
var options = this._options; | ||
if (newOptions.actions) { | ||
options.actions = newOptions.actions; | ||
} | ||
if (newOptions.mutations) { | ||
options.mutations = newOptions.mutations; | ||
} | ||
if (newOptions.modules) { | ||
for (var key in newOptions.modules) { | ||
options.modules[key] = newOptions.modules[key]; | ||
} | ||
} | ||
this.module([], options, true); | ||
Object.defineProperties( Store.prototype, prototypeAccessors ); | ||
// update getters | ||
var getters = extractModuleGetters(newOptions.getters, newOptions.modules); | ||
if (Object.keys(getters).length) { | ||
(function () { | ||
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. | ||
_this5._committing = true; | ||
oldVm.state = null; | ||
_this5._committing = false; | ||
Vue.nextTick(function () { | ||
return oldVm.$destroy(); | ||
}); | ||
})(); | ||
} | ||
} | ||
}, { | ||
key: 'state', | ||
get: function get() { | ||
return this._vm.state; | ||
}, | ||
set: function set(v) { | ||
assert(false, 'Use store.replaceState() to explicit replace store state.'); | ||
} | ||
}]); | ||
return Store; | ||
}(); | ||
function assert (condition, msg) { | ||
if (!condition) throw new Error(("[vuex] " + msg)) | ||
} | ||
function assert(condition, msg) { | ||
if (!condition) throw new Error('[vuex] ' + msg); | ||
function resetStore (store) { | ||
store._actions = Object.create(null) | ||
store._mutations = Object.create(null) | ||
store._wrappedGetters = Object.create(null) | ||
var state = store.state | ||
// init root module | ||
installModule(store, state, [], store._options, true) | ||
// init all runtime modules | ||
Object.keys(store._runtimeModules).forEach(function (key) { | ||
installModule(store, state, key.split('.'), store._runtimeModules[key], true) | ||
}) | ||
// reset vm | ||
resetStoreVM(store, state) | ||
} | ||
function initStoreState(store, state, getters) { | ||
// bind getters | ||
store.getters = {}; | ||
var computed = {}; | ||
Object.keys(getters).forEach(function (key) { | ||
var fn = getters[key]; | ||
function resetStoreVM (store, state) { | ||
var oldVm = store._vm | ||
// bind store public getters | ||
store.getters = {} | ||
var wrappedGetters = store._wrappedGetters | ||
var computed = {} | ||
Object.keys(wrappedGetters).forEach(function (key) { | ||
var fn = wrappedGetters[key] | ||
// use computed to leverage its lazy-caching mechanism | ||
computed[key] = function () { | ||
return fn(store); | ||
}; | ||
computed[key] = function () { return fn(store); } | ||
Object.defineProperty(store.getters, key, { | ||
get: function get() { | ||
return store._vm[key]; | ||
} | ||
}); | ||
}); | ||
get: function () { return store._vm[key]; } | ||
}) | ||
}) | ||
@@ -474,76 +363,148 @@ // use a Vue instance to store the state tree | ||
// some funky global mixins | ||
var silent = Vue.config.silent; | ||
Vue.config.silent = true; | ||
var silent = Vue.config.silent | ||
Vue.config.silent = true | ||
store._vm = new Vue({ | ||
data: { state: state }, | ||
computed: computed | ||
}); | ||
Vue.config.silent = silent; | ||
}) | ||
Vue.config.silent = silent | ||
// enable strict mode for new vm | ||
if (store.strict) { | ||
enableStrictMode(store) | ||
} | ||
if (oldVm) { | ||
// dispatch changes in all subscribed watchers | ||
// to force getter re-evaluation. | ||
store._withCommit(function () { | ||
oldVm.state = null | ||
}) | ||
Vue.nextTick(function () { return oldVm.$destroy(); }) | ||
} | ||
} | ||
function extractModuleGetters() { | ||
var getters = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var modules = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var path = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
function installModule (store, rootState, path, module, hot) { | ||
var isRoot = !path.length | ||
var state = module.state; | ||
var actions = module.actions; | ||
var mutations = module.mutations; | ||
var getters = module.getters; | ||
var modules = module.modules; | ||
if (!path.length) { | ||
wrapGetters(getters, getters, path, true); | ||
// set state | ||
if (!isRoot && !hot) { | ||
var parentState = getNestedState(rootState, path.slice(0, -1)) | ||
var moduleName = path[path.length - 1] | ||
store._withCommit(function () { | ||
Vue.set(parentState, moduleName, state || {}) | ||
}) | ||
} | ||
if (!modules) { | ||
return getters; | ||
if (mutations) { | ||
Object.keys(mutations).forEach(function (key) { | ||
registerMutation(store, key, mutations[key], path) | ||
}) | ||
} | ||
Object.keys(modules).forEach(function (key) { | ||
var module = modules[key]; | ||
var modulePath = path.concat(key); | ||
if (module.getters) { | ||
wrapGetters(getters, module.getters, modulePath); | ||
if (actions) { | ||
Object.keys(actions).forEach(function (key) { | ||
registerAction(store, key, actions[key], path) | ||
}) | ||
} | ||
if (getters) { | ||
wrapGetters(store, getters, path) | ||
} | ||
if (modules) { | ||
Object.keys(modules).forEach(function (key) { | ||
installModule(store, rootState, path.concat(key), modules[key], hot) | ||
}) | ||
} | ||
} | ||
function registerMutation (store, type, handler, path) { | ||
if ( path === void 0 ) path = []; | ||
var entry = store._mutations[type] || (store._mutations[type] = []) | ||
entry.push(function wrappedMutationHandler (payload) { | ||
handler(getNestedState(store.state, path), payload) | ||
}) | ||
} | ||
function registerAction (store, type, handler, path) { | ||
if ( path === void 0 ) path = []; | ||
var entry = store._actions[type] || (store._actions[type] = []) | ||
var dispatch = store.dispatch; | ||
var commit = store.commit; | ||
entry.push(function wrappedActionHandler (payload, cb) { | ||
var res = handler({ | ||
dispatch: dispatch, | ||
commit: commit, | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb) | ||
if (!isPromise(res)) { | ||
res = Promise.resolve(res) | ||
} | ||
extractModuleGetters(getters, module.modules, modulePath); | ||
}); | ||
return getters; | ||
if (store._devtoolHook) { | ||
return res.catch(function (err) { | ||
store._devtoolHook.emit('vuex:error', err) | ||
throw err | ||
}) | ||
} else { | ||
return res | ||
} | ||
}) | ||
} | ||
function wrapGetters(getters, moduleGetters, modulePath, force) { | ||
function wrapGetters (store, moduleGetters, modulePath) { | ||
Object.keys(moduleGetters).forEach(function (getterKey) { | ||
var rawGetter = moduleGetters[getterKey]; | ||
if (getters[getterKey] && !force) { | ||
console.error('[vuex] duplicate getter key: ' + getterKey); | ||
return; | ||
var rawGetter = moduleGetters[getterKey] | ||
if (store._wrappedGetters[getterKey]) { | ||
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 | ||
); | ||
}; | ||
}); | ||
store._wrappedGetters[getterKey] = function wrappedGetter (store) { | ||
return rawGetter( | ||
getNestedState(store.state, modulePath), // local state | ||
store.getters, // getters | ||
store.state // root state | ||
) | ||
} | ||
}) | ||
} | ||
function enableStrictMode(store) { | ||
function enableStrictMode (store) { | ||
store._vm.$watch('state', function () { | ||
assert(store._committing, 'Do not mutate vuex store state outside mutation handlers.'); | ||
}, { deep: true, sync: true }); | ||
assert(store._committing, "Do not mutate vuex store state outside mutation handlers.") | ||
}, { deep: true, sync: true }) | ||
} | ||
function isObject(obj) { | ||
return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object'; | ||
function isObject (obj) { | ||
return obj !== null && typeof obj === 'object' | ||
} | ||
function isPromise(val) { | ||
return val && typeof val.then === 'function'; | ||
function isPromise (val) { | ||
return val && typeof val.then === 'function' | ||
} | ||
function getNestedState(state, path) { | ||
return path.length ? path.reduce(function (state, key) { | ||
return state[key]; | ||
}, state) : state; | ||
function getNestedState (state, path) { | ||
return path.length | ||
? path.reduce(function (state, key) { return state[key]; }, state) | ||
: state | ||
} | ||
function install(_Vue) { | ||
function install (_Vue) { | ||
if (Vue) { | ||
console.error('[vuex] already installed. Vue.use(Vuex) should be called only once.'); | ||
return; | ||
console.error( | ||
'[vuex] already installed. Vue.use(Vuex) should be called only once.' | ||
) | ||
return | ||
} | ||
Vue = _Vue; | ||
applyMixin(Vue); | ||
Vue = _Vue | ||
applyMixin(Vue) | ||
} | ||
@@ -553,3 +514,3 @@ | ||
if (typeof window !== 'undefined' && window.Vue) { | ||
install(window.Vue); | ||
install(window.Vue) | ||
} | ||
@@ -564,3 +525,3 @@ | ||
mapActions: mapActions | ||
}; | ||
} | ||
@@ -567,0 +528,0 @@ return index; |
@@ -1,6 +0,6 @@ | ||
/*! | ||
* Vuex v2.0.0-rc.3 | ||
/** | ||
* vuex v2.0.0-rc.4 | ||
* (c) 2016 Evan You | ||
* Released under the MIT License. | ||
* @license MIT | ||
*/ | ||
!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}); | ||
!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){b&&(t._devtoolHook=b,b.emit("vuex:init",t),b.on("vuex:travel-to-state",function(e){t.replaceState(e)}),t.subscribe(function(t,e){b.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{var o=t.prototype._init;t.prototype._init=function(t){void 0===t&&(t={}),t.init=t.init?[e].concat(t.init):e,o.call(this,t)}}}function n(t){var e={};return s(t).forEach(function(t){var n=t.key,i=t.val;e[n]=function(){return"function"==typeof i?i.call(this,this.$store.state,this.$store.getters):this.$store.state[i]}}),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;e--;)t[e]=arguments[e];return this.$store.commit.apply(this.$store,[i].concat(t))}}),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;e--;)t[e]=arguments[e];return this.$store.dispatch.apply(this.$store,[i].concat(t))}}),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 c(t,e){if(!t)throw new Error("[vuex] "+e)}function u(t){t._actions=Object.create(null),t._mutations=Object.create(null),t._wrappedGetters=Object.create(null);var e=t.state;f(t,e,[],t._options,!0),Object.keys(t._runtimeModules).forEach(function(n){f(t,e,n.split("."),t._runtimeModules[n],!0)}),a(t,e)}function a(t,e){var n=t._vm;t.getters={};var i=t._wrappedGetters,o={};Object.keys(i).forEach(function(e){var n=i[e];o[e]=function(){return n(t)},Object.defineProperty(t.getters,e,{get:function(){return t._vm[e]}})});var r=g.config.silent;g.config.silent=!0,t._vm=new g({data:{state:e},computed:o}),g.config.silent=r,t.strict&&v(t),n&&(t._withCommit(function(){n.state=null}),g.nextTick(function(){return n.$destroy()}))}function f(t,e,n,i,o){var r=!n.length,s=i.state,c=i.actions,u=i.mutations,a=i.getters,v=i.modules;if(!r&&!o){var m=y(e,n.slice(0,-1)),d=n[n.length-1];t._withCommit(function(){g.set(m,d,s||{})})}u&&Object.keys(u).forEach(function(e){l(t,e,u[e],n)}),c&&Object.keys(c).forEach(function(e){h(t,e,c[e],n)}),a&&p(t,a,n),v&&Object.keys(v).forEach(function(i){f(t,e,n.concat(i),v[i],o)})}function l(t,e,n,i){void 0===i&&(i=[]);var o=t._mutations[e]||(t._mutations[e]=[]);o.push(function(e){n(y(t.state,i),e)})}function h(t,e,n,i){void 0===i&&(i=[]);var o=t._actions[e]||(t._actions[e]=[]),r=t.dispatch,s=t.commit;o.push(function(e,o){var c=n({dispatch:r,commit:s,getters:t.getters,state:y(t.state,i),rootState:t.state},e,o);return d(c)||(c=Promise.resolve(c)),t._devtoolHook?c.catch(function(e){throw t._devtoolHook.emit("vuex:error",e),e}):c})}function p(t,e,n){Object.keys(e).forEach(function(i){var o=e[i];return t._wrappedGetters[i]?void console.error("[vuex] duplicate getter key: "+i):void(t._wrappedGetters[i]=function(t){return o(y(t.state,n),t.getters,t.state)})})}function v(t){t._vm.$watch("state",function(){c(t._committing,"Do not mutate vuex store state outside mutation handlers.")},{deep:!0,sync:!0})}function m(t){return null!==t&&"object"==typeof t}function d(t){return t&&"function"==typeof t.then}function y(t,e){return e.length?e.reduce(function(t,e){return t[e]},t):t}function _(t){return g?void console.error("[vuex] already installed. Vue.use(Vuex) should be called only once."):(g=t,void e(g))}var g,b="undefined"!=typeof window&&window.__VUE_DEVTOOLS_GLOBAL_HOOK__,w=function(e){var n=this;void 0===e&&(e={}),c(g,"must call Vue.use(Vuex) before creating a store instance."),c("undefined"!=typeof Promise,"vuex requires a Promise polyfill in this browser.");var i=e.state;void 0===i&&(i={});var o=e.plugins;void 0===o&&(o=[]);var r=e.strict;void 0===r&&(r=!1),this._options=e,this._committing=!1,this._actions=Object.create(null),this._mutations=Object.create(null),this._wrappedGetters=Object.create(null),this._runtimeModules=Object.create(null),this._subscribers=[],this._pendingActions=[];var s=this,u=this,l=u.dispatch,h=u.commit;this.dispatch=function(t,e){return l.call(s,t,e)},this.commit=function(t,e){return h.call(s,t,e)},this.strict=r,f(this,i,[],e),a(this,i),o.concat(t).forEach(function(t){return t(n)})},O={state:{}};O.state.get=function(){return this._vm.state},O.state.set=function(t){c(!1,"Use store.replaceState() to explicit replace store state.")},w.prototype.commit=function(t,e){var n,i=this;m(t)&&t.type?(e=n=t,t=t.type):n={type:t,payload:e};var o=this._mutations[t];return o?(this._withCommit(function(){o.forEach(function(t){t(e)})}),void(e&&e.silent||this._subscribers.forEach(function(t){return t(n,i.state)}))):void console.error("[vuex] unknown mutation type: "+t)},w.prototype.dispatch=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})},w.prototype.subscribe=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)}},w.prototype.watch=function(t,e,n){var i=this;return c("function"==typeof t,"store.watch only accepts a function."),this._vm.$watch(function(){return t(i.state)},e,n)},w.prototype.replaceState=function(t){var e=this;this._withCommit(function(){e._vm.state=t})},w.prototype.registerModule=function(t,e){"string"==typeof t&&(t=[t]),c(Array.isArray(t),"module path must be a string or an Array."),this._runtimeModules[t.join(".")]=e,f(this,this.state,t,e),a(this,this.state)},w.prototype.unregisterModule=function(t){var e=this;"string"==typeof t&&(t=[t]),c(Array.isArray(t),"module path must be a string or an Array."),delete this._runtimeModules[t.join(".")],this._withCommit(function(){var n=y(e.state,t.slice(0,-1));g.delete(n,t[t.length-1])}),u(this)},w.prototype.hotUpdate=function(t){var e=this._options;if(t.actions&&(e.actions=t.actions),t.mutations&&(e.mutations=t.mutations),t.getters&&(e.getters=t.getters),t.modules)for(var n in t.modules)e.modules[n]=t.modules[n];u(this)},w.prototype.onActionsResolved=function(t){Promise.all(this._pendingActions).then(t)},w.prototype._withCommit=function(t){var e=this._committing;this._committing=!0,t(),this._committing=e},Object.defineProperties(w.prototype,O),"undefined"!=typeof window&&window.Vue&&_(window.Vue);var x={Store:w,install:_,mapState:n,mapMutations:i,mapGetters:o,mapActions:r};return x}); |
{ | ||
"name": "vuex", | ||
"version": "2.0.0-rc.3", | ||
"version": "2.0.0-rc.4", | ||
"description": "state management for Vue.js", | ||
@@ -12,16 +12,11 @@ "main": "dist/vuex.js", | ||
"scripts": { | ||
"counter": "cd examples/counter && webpack-dev-server --inline --hot --config ../webpack.shared.config.js", | ||
"counter-hot": "cd examples/counter-hot && webpack-dev-server --inline --hot --config ../webpack.shared.config.js", | ||
"todomvc": "cd examples/todomvc && webpack-dev-server --inline --hot --config ../webpack.shared.config.js", | ||
"cart": "cd examples/shopping-cart && webpack-dev-server --inline --hot --config ../webpack.shared.config.js", | ||
"chat": "cd examples/chat && webpack-dev-server --inline --hot --config ../webpack.shared.config.js", | ||
"build": "node build/build.js", | ||
"build-examples": "BABEL_ENV=development webpack --config examples/webpack.build-all.config.js", | ||
"unit": "BABEL_ENV=development mocha test/unit/test.js --compilers js:babel-core/register 2>/dev/null", | ||
"pree2e": "npm run build-examples", | ||
"e2e": "casperjs test --concise ./test/e2e", | ||
"test": "eslint src && npm run unit && npm run e2e", | ||
"dev": "node examples/server.js", | ||
"dev:dist": "rollup -wm -c build/rollup.config.js", | ||
"build": "rollup -c build/rollup.config.js && uglifyjs dist/vuex.js -cm --comments -o dist/vuex.min.js", | ||
"test": "eslint src && npm run test:unit && npm run test:e2e", | ||
"test:unit": "rollup -c build/rollup.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json", | ||
"test:e2e": "node test/e2e/runner.js", | ||
"release": "bash build/release.sh", | ||
"docs": "cd docs && gitbook serve", | ||
"deploy-docs": "cd docs && ./deploy.sh", | ||
"release": "bash build/release.sh" | ||
"docs:deploy": "cd docs && ./deploy.sh" | ||
}, | ||
@@ -47,20 +42,24 @@ "repository": { | ||
"babel-runtime": "^6.0.0", | ||
"casperjs": "^1.1.0-beta5", | ||
"chai": "^3.4.1", | ||
"cross-spawn": "^4.0.0", | ||
"css-loader": "^0.23.1", | ||
"eslint": "^2.2.0", | ||
"eslint-config-vue": "^1.0.0", | ||
"mocha": "^2.3.4", | ||
"express": "^4.14.0", | ||
"jasmine": "^2.4.1", | ||
"jasmine-core": "^2.4.1", | ||
"nightwatch": "^0.9.5", | ||
"nightwatch-helpers": "^1.1.0", | ||
"phantomjs-prebuilt": "^2.1.7", | ||
"rollup": "^0.32.0", | ||
"rollup-plugin-babel": "^2.4.0", | ||
"sinon": "^1.17.3", | ||
"sinon-chai": "^2.8.0", | ||
"rollup": "^0.34.1", | ||
"rollup-plugin-buble": "^0.12.1", | ||
"rollup-watch": "^2.5.0", | ||
"selenium-server": "^2.53.1", | ||
"todomvc-app-css": "^2.0.3", | ||
"uglify-js": "^2.6.2", | ||
"vue": "^2.0.0-alpha.7", | ||
"vue": "^2.0.0-beta.5", | ||
"vue-loader": "^9.0.3", | ||
"webpack": "^1.12.8", | ||
"webpack-dev-server": "^1.12.1" | ||
"webpack-dev-middleware": "^1.6.1", | ||
"webpack-hot-middleware": "^2.12.2" | ||
} | ||
} |
@@ -1,7 +0,8 @@ | ||
export function mapState (map) { | ||
export function mapState (states) { | ||
const res = {} | ||
Object.keys(map).forEach(key => { | ||
const fn = map[key] | ||
normalizeMap(states).forEach(({ key, val }) => { | ||
res[key] = function mappedState () { | ||
return fn.call(this, this.$store.state, this.$store.getters) | ||
return typeof val === 'function' | ||
? val.call(this, this.$store.state, this.$store.getters) | ||
: this.$store.state[val] | ||
} | ||
@@ -16,3 +17,3 @@ }) | ||
res[key] = function mappedMutation (...args) { | ||
return this.$store.commit(val, ...args) | ||
return this.$store.commit.apply(this.$store, [val].concat(args)) | ||
} | ||
@@ -40,3 +41,3 @@ }) | ||
res[key] = function mappedAction (...args) { | ||
return this.$store.dispatch(val, ...args) | ||
return this.$store.dispatch.apply(this.$store, [val].concat(args)) | ||
} | ||
@@ -43,0 +44,0 @@ }) |
296
src/index.js
@@ -14,3 +14,2 @@ import devtoolPlugin from './plugins/devtool' | ||
state = {}, | ||
modules = {}, | ||
plugins = [], | ||
@@ -25,2 +24,4 @@ strict = false | ||
this._mutations = Object.create(null) | ||
this._wrappedGetters = Object.create(null) | ||
this._runtimeModules = Object.create(null) | ||
this._subscribers = [] | ||
@@ -39,11 +40,13 @@ this._pendingActions = [] | ||
// init state and getters | ||
const getters = extractModuleGetters(options.getters, modules) | ||
initStoreState(this, state, getters) | ||
// strict mode | ||
this.strict = strict | ||
// apply root module | ||
this.module([], options) | ||
// init root module. | ||
// this also recursively registers all sub-modules | ||
// and collects all module getters inside this._wrappedGetters | ||
installModule(this, state, [], options) | ||
// strict mode | ||
if (strict) enableStrictMode(this) | ||
// initialize the store vm, which is responsible for the reactivity | ||
// (also registers _wrappedGetters as computed properties) | ||
resetStoreVM(this, state) | ||
@@ -62,83 +65,2 @@ // apply plugins | ||
replaceState (state) { | ||
this._committing = true | ||
this._vm.state = state | ||
this._committing = false | ||
} | ||
module (path, module, hot) { | ||
this._committing = true | ||
if (typeof path === 'string') path = [path] | ||
assert(Array.isArray(path), `module path must be a string or an Array.`) | ||
const isRoot = !path.length | ||
const { | ||
state, | ||
actions, | ||
mutations, | ||
modules | ||
} = module | ||
// set state | ||
if (!isRoot && !hot) { | ||
const parentState = getNestedState(this.state, path.slice(0, -1)) | ||
if (!parentState) debugger | ||
const moduleName = path[path.length - 1] | ||
Vue.set(parentState, moduleName, state || {}) | ||
} | ||
if (mutations) { | ||
Object.keys(mutations).forEach(key => { | ||
this.mutation(key, mutations[key], path) | ||
}) | ||
} | ||
if (actions) { | ||
Object.keys(actions).forEach(key => { | ||
this.action(key, actions[key], path) | ||
}) | ||
} | ||
if (modules) { | ||
Object.keys(modules).forEach(key => { | ||
this.module(path.concat(key), modules[key], hot) | ||
}) | ||
} | ||
this._committing = false | ||
} | ||
mutation (type, handler, path = []) { | ||
const entry = this._mutations[type] || (this._mutations[type] = []) | ||
const store = this | ||
entry.push(function wrappedMutationHandler (payload) { | ||
handler(getNestedState(store.state, path), payload) | ||
}) | ||
} | ||
action (type, handler, path = []) { | ||
const entry = this._actions[type] || (this._actions[type] = []) | ||
const store = this | ||
const { dispatch, commit } = this | ||
entry.push(function wrappedActionHandler (payload, cb) { | ||
let res = handler({ | ||
dispatch, | ||
commit, | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb) | ||
if (!isPromise(res)) { | ||
res = Promise.resolve(res) | ||
} | ||
if (store._devtoolHook) { | ||
return res.catch(err => { | ||
store._devtoolHook.emit('vuex:error', err) | ||
throw err | ||
}) | ||
} else { | ||
return res | ||
} | ||
}) | ||
} | ||
commit (type, payload) { | ||
@@ -158,7 +80,7 @@ // check object-style commit | ||
} | ||
this._committing = true | ||
entry.forEach(function commitIterator (handler) { | ||
handler(payload) | ||
this._withCommit(() => { | ||
entry.forEach(function commitIterator (handler) { | ||
handler(payload) | ||
}) | ||
}) | ||
this._committing = false | ||
if (!payload || !payload.silent) { | ||
@@ -186,6 +108,2 @@ this._subscribers.forEach(sub => sub(mutation, this.state)) | ||
onActionsResolved (cb) { | ||
Promise.all(this._pendingActions).then(cb) | ||
} | ||
subscribe (fn) { | ||
@@ -209,5 +127,29 @@ const subs = this._subscribers | ||
replaceState (state) { | ||
this._withCommit(() => { | ||
this._vm.state = state | ||
}) | ||
} | ||
registerModule (path, module) { | ||
if (typeof path === 'string') path = [path] | ||
assert(Array.isArray(path), `module path must be a string or an Array.`) | ||
this._runtimeModules[path.join('.')] = module | ||
installModule(this, this.state, path, module) | ||
// reset store to update getters... | ||
resetStoreVM(this, this.state) | ||
} | ||
unregisterModule (path) { | ||
if (typeof path === 'string') path = [path] | ||
assert(Array.isArray(path), `module path must be a string or an Array.`) | ||
delete this._runtimeModules[path.join('.')] | ||
this._withCommit(() => { | ||
const parentState = getNestedState(this.state, path.slice(0, -1)) | ||
Vue.delete(parentState, path[path.length - 1]) | ||
}) | ||
resetStore(this) | ||
} | ||
hotUpdate (newOptions) { | ||
this._actions = Object.create(null) | ||
this._mutations = Object.create(null) | ||
const options = this._options | ||
@@ -220,2 +162,5 @@ if (newOptions.actions) { | ||
} | ||
if (newOptions.getters) { | ||
options.getters = newOptions.getters | ||
} | ||
if (newOptions.modules) { | ||
@@ -226,20 +171,15 @@ for (const key in newOptions.modules) { | ||
} | ||
this.module([], options, true) | ||
resetStore(this) | ||
} | ||
// update getters | ||
const getters = extractModuleGetters(newOptions.getters, newOptions.modules) | ||
if (Object.keys(getters).length) { | ||
const oldVm = this._vm | ||
initStoreState(this, this.state, getters) | ||
if (this.strict) { | ||
enableStrictMode(this) | ||
} | ||
// dispatch changes in all subscribed watchers | ||
// to force getter re-evaluation. | ||
this._committing = true | ||
oldVm.state = null | ||
this._committing = false | ||
Vue.nextTick(() => oldVm.$destroy()) | ||
} | ||
onActionsResolved (cb) { | ||
Promise.all(this._pendingActions).then(cb) | ||
} | ||
_withCommit (fn) { | ||
const committing = this._committing | ||
this._committing = true | ||
fn() | ||
this._committing = committing | ||
} | ||
} | ||
@@ -251,8 +191,26 @@ | ||
function initStoreState (store, state, getters) { | ||
// bind getters | ||
function resetStore (store) { | ||
store._actions = Object.create(null) | ||
store._mutations = Object.create(null) | ||
store._wrappedGetters = Object.create(null) | ||
const state = store.state | ||
// init root module | ||
installModule(store, state, [], store._options, true) | ||
// init all runtime modules | ||
Object.keys(store._runtimeModules).forEach(key => { | ||
installModule(store, state, key.split('.'), store._runtimeModules[key], true) | ||
}) | ||
// reset vm | ||
resetStoreVM(store, state) | ||
} | ||
function resetStoreVM (store, state) { | ||
const oldVm = store._vm | ||
// bind store public getters | ||
store.getters = {} | ||
const wrappedGetters = store._wrappedGetters | ||
const computed = {} | ||
Object.keys(getters).forEach(key => { | ||
const fn = getters[key] | ||
Object.keys(wrappedGetters).forEach(key => { | ||
const fn = wrappedGetters[key] | ||
// use computed to leverage its lazy-caching mechanism | ||
@@ -275,30 +233,100 @@ computed[key] = () => fn(store) | ||
Vue.config.silent = silent | ||
// enable strict mode for new vm | ||
if (store.strict) { | ||
enableStrictMode(store) | ||
} | ||
if (oldVm) { | ||
// dispatch changes in all subscribed watchers | ||
// to force getter re-evaluation. | ||
store._withCommit(() => { | ||
oldVm.state = null | ||
}) | ||
Vue.nextTick(() => oldVm.$destroy()) | ||
} | ||
} | ||
function extractModuleGetters (getters = {}, modules = {}, path = []) { | ||
if (!path.length) { | ||
wrapGetters(getters, getters, path, true) | ||
function installModule (store, rootState, path, module, hot) { | ||
const isRoot = !path.length | ||
const { | ||
state, | ||
actions, | ||
mutations, | ||
getters, | ||
modules | ||
} = module | ||
// set state | ||
if (!isRoot && !hot) { | ||
const parentState = getNestedState(rootState, path.slice(0, -1)) | ||
const moduleName = path[path.length - 1] | ||
store._withCommit(() => { | ||
Vue.set(parentState, moduleName, state || {}) | ||
}) | ||
} | ||
if (!modules) { | ||
return getters | ||
if (mutations) { | ||
Object.keys(mutations).forEach(key => { | ||
registerMutation(store, key, mutations[key], path) | ||
}) | ||
} | ||
Object.keys(modules).forEach(key => { | ||
const module = modules[key] | ||
const modulePath = path.concat(key) | ||
if (module.getters) { | ||
wrapGetters(getters, module.getters, modulePath) | ||
if (actions) { | ||
Object.keys(actions).forEach(key => { | ||
registerAction(store, key, actions[key], path) | ||
}) | ||
} | ||
if (getters) { | ||
wrapGetters(store, getters, path) | ||
} | ||
if (modules) { | ||
Object.keys(modules).forEach(key => { | ||
installModule(store, rootState, path.concat(key), modules[key], hot) | ||
}) | ||
} | ||
} | ||
function registerMutation (store, type, handler, path = []) { | ||
const entry = store._mutations[type] || (store._mutations[type] = []) | ||
entry.push(function wrappedMutationHandler (payload) { | ||
handler(getNestedState(store.state, path), payload) | ||
}) | ||
} | ||
function registerAction (store, type, handler, path = []) { | ||
const entry = store._actions[type] || (store._actions[type] = []) | ||
const { dispatch, commit } = store | ||
entry.push(function wrappedActionHandler (payload, cb) { | ||
let res = handler({ | ||
dispatch, | ||
commit, | ||
getters: store.getters, | ||
state: getNestedState(store.state, path), | ||
rootState: store.state | ||
}, payload, cb) | ||
if (!isPromise(res)) { | ||
res = Promise.resolve(res) | ||
} | ||
extractModuleGetters(getters, module.modules, modulePath) | ||
if (store._devtoolHook) { | ||
return res.catch(err => { | ||
store._devtoolHook.emit('vuex:error', err) | ||
throw err | ||
}) | ||
} else { | ||
return res | ||
} | ||
}) | ||
return getters | ||
} | ||
function wrapGetters (getters, moduleGetters, modulePath, force) { | ||
function wrapGetters (store, moduleGetters, modulePath) { | ||
Object.keys(moduleGetters).forEach(getterKey => { | ||
const rawGetter = moduleGetters[getterKey] | ||
if (getters[getterKey] && !force) { | ||
if (store._wrappedGetters[getterKey]) { | ||
console.error(`[vuex] duplicate getter key: ${getterKey}`) | ||
return | ||
} | ||
getters[getterKey] = function wrappedGetter (store) { | ||
store._wrappedGetters[getterKey] = function wrappedGetter (store) { | ||
return rawGetter( | ||
@@ -305,0 +333,0 @@ getNestedState(store.state, modulePath), // local 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
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
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
66809
12
0
42
29
963
1