vuex.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /**
  2. * vuex v3.0.1
  3. * (c) 2017 Evan You
  4. * @license MIT
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global.Vuex = factory());
  10. }(this, (function () { 'use strict';
  11. var applyMixin = function (Vue) {
  12. var version = Number(Vue.version.split('.')[0]);
  13. if (version >= 2) {
  14. Vue.mixin({ beforeCreate: vuexInit });
  15. } else {
  16. // override init and inject vuex init procedure
  17. // for 1.x backwards compatibility.
  18. var _init = Vue.prototype._init;
  19. Vue.prototype._init = function (options) {
  20. if ( options === void 0 ) options = {};
  21. options.init = options.init
  22. ? [vuexInit].concat(options.init)
  23. : vuexInit;
  24. _init.call(this, options);
  25. };
  26. }
  27. /**
  28. * Vuex init hook, injected into each instances init hooks list.
  29. */
  30. function vuexInit () {
  31. var options = this.$options;
  32. // store injection
  33. if (options.store) {
  34. this.$store = typeof options.store === 'function'
  35. ? options.store()
  36. : options.store;
  37. } else if (options.parent && options.parent.$store) {
  38. this.$store = options.parent.$store;
  39. }
  40. }
  41. };
  42. var devtoolHook =
  43. typeof window !== 'undefined' &&
  44. window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
  45. function devtoolPlugin (store) {
  46. if (!devtoolHook) { return }
  47. store._devtoolHook = devtoolHook;
  48. devtoolHook.emit('vuex:init', store);
  49. devtoolHook.on('vuex:travel-to-state', function (targetState) {
  50. store.replaceState(targetState);
  51. });
  52. store.subscribe(function (mutation, state) {
  53. devtoolHook.emit('vuex:mutation', mutation, state);
  54. });
  55. }
  56. /**
  57. * Get the first item that pass the test
  58. * by second argument function
  59. *
  60. * @param {Array} list
  61. * @param {Function} f
  62. * @return {*}
  63. */
  64. /**
  65. * Deep copy the given object considering circular structure.
  66. * This function caches all nested objects and its copies.
  67. * If it detects circular structure, use cached copy to avoid infinite loop.
  68. *
  69. * @param {*} obj
  70. * @param {Array<Object>} cache
  71. * @return {*}
  72. */
  73. /**
  74. * forEach for object
  75. */
  76. function forEachValue (obj, fn) {
  77. Object.keys(obj).forEach(function (key) { return fn(obj[key], key); });
  78. }
  79. function isObject (obj) {
  80. return obj !== null && typeof obj === 'object'
  81. }
  82. function isPromise (val) {
  83. return val && typeof val.then === 'function'
  84. }
  85. function assert (condition, msg) {
  86. if (!condition) { throw new Error(("[vuex] " + msg)) }
  87. }
  88. var Module = function Module (rawModule, runtime) {
  89. this.runtime = runtime;
  90. this._children = Object.create(null);
  91. this._rawModule = rawModule;
  92. var rawState = rawModule.state;
  93. this.state = (typeof rawState === 'function' ? rawState() : rawState) || {};
  94. };
  95. var prototypeAccessors$1 = { namespaced: { configurable: true } };
  96. prototypeAccessors$1.namespaced.get = function () {
  97. return !!this._rawModule.namespaced
  98. };
  99. Module.prototype.addChild = function addChild (key, module) {
  100. this._children[key] = module;
  101. };
  102. Module.prototype.removeChild = function removeChild (key) {
  103. delete this._children[key];
  104. };
  105. Module.prototype.getChild = function getChild (key) {
  106. return this._children[key]
  107. };
  108. Module.prototype.update = function update (rawModule) {
  109. this._rawModule.namespaced = rawModule.namespaced;
  110. if (rawModule.actions) {
  111. this._rawModule.actions = rawModule.actions;
  112. }
  113. if (rawModule.mutations) {
  114. this._rawModule.mutations = rawModule.mutations;
  115. }
  116. if (rawModule.getters) {
  117. this._rawModule.getters = rawModule.getters;
  118. }
  119. };
  120. Module.prototype.forEachChild = function forEachChild (fn) {
  121. forEachValue(this._children, fn);
  122. };
  123. Module.prototype.forEachGetter = function forEachGetter (fn) {
  124. if (this._rawModule.getters) {
  125. forEachValue(this._rawModule.getters, fn);
  126. }
  127. };
  128. Module.prototype.forEachAction = function forEachAction (fn) {
  129. if (this._rawModule.actions) {
  130. forEachValue(this._rawModule.actions, fn);
  131. }
  132. };
  133. Module.prototype.forEachMutation = function forEachMutation (fn) {
  134. if (this._rawModule.mutations) {
  135. forEachValue(this._rawModule.mutations, fn);
  136. }
  137. };
  138. Object.defineProperties( Module.prototype, prototypeAccessors$1 );
  139. var ModuleCollection = function ModuleCollection (rawRootModule) {
  140. // register root module (Vuex.Store options)
  141. this.register([], rawRootModule, false);
  142. };
  143. ModuleCollection.prototype.get = function get (path) {
  144. return path.reduce(function (module, key) {
  145. return module.getChild(key)
  146. }, this.root)
  147. };
  148. ModuleCollection.prototype.getNamespace = function getNamespace (path) {
  149. var module = this.root;
  150. return path.reduce(function (namespace, key) {
  151. module = module.getChild(key);
  152. return namespace + (module.namespaced ? key + '/' : '')
  153. }, '')
  154. };
  155. ModuleCollection.prototype.update = function update$1 (rawRootModule) {
  156. update([], this.root, rawRootModule);
  157. };
  158. ModuleCollection.prototype.register = function register (path, rawModule, runtime) {
  159. var this$1 = this;
  160. if ( runtime === void 0 ) runtime = true;
  161. {
  162. assertRawModule(path, rawModule);
  163. }
  164. var newModule = new Module(rawModule, runtime);
  165. if (path.length === 0) {
  166. this.root = newModule;
  167. } else {
  168. var parent = this.get(path.slice(0, -1));
  169. parent.addChild(path[path.length - 1], newModule);
  170. }
  171. // register nested modules
  172. if (rawModule.modules) {
  173. forEachValue(rawModule.modules, function (rawChildModule, key) {
  174. this$1.register(path.concat(key), rawChildModule, runtime);
  175. });
  176. }
  177. };
  178. ModuleCollection.prototype.unregister = function unregister (path) {
  179. var parent = this.get(path.slice(0, -1));
  180. var key = path[path.length - 1];
  181. if (!parent.getChild(key).runtime) { return }
  182. parent.removeChild(key);
  183. };
  184. function update (path, targetModule, newModule) {
  185. {
  186. assertRawModule(path, newModule);
  187. }
  188. // update target module
  189. targetModule.update(newModule);
  190. // update nested modules
  191. if (newModule.modules) {
  192. for (var key in newModule.modules) {
  193. if (!targetModule.getChild(key)) {
  194. {
  195. console.warn(
  196. "[vuex] trying to add a new module '" + key + "' on hot reloading, " +
  197. 'manual reload is needed'
  198. );
  199. }
  200. return
  201. }
  202. update(
  203. path.concat(key),
  204. targetModule.getChild(key),
  205. newModule.modules[key]
  206. );
  207. }
  208. }
  209. }
  210. var functionAssert = {
  211. assert: function (value) { return typeof value === 'function'; },
  212. expected: 'function'
  213. };
  214. var objectAssert = {
  215. assert: function (value) { return typeof value === 'function' ||
  216. (typeof value === 'object' && typeof value.handler === 'function'); },
  217. expected: 'function or object with "handler" function'
  218. };
  219. var assertTypes = {
  220. getters: functionAssert,
  221. mutations: functionAssert,
  222. actions: objectAssert
  223. };
  224. function assertRawModule (path, rawModule) {
  225. Object.keys(assertTypes).forEach(function (key) {
  226. if (!rawModule[key]) { return }
  227. var assertOptions = assertTypes[key];
  228. forEachValue(rawModule[key], function (value, type) {
  229. assert(
  230. assertOptions.assert(value),
  231. makeAssertionMessage(path, key, type, value, assertOptions.expected)
  232. );
  233. });
  234. });
  235. }
  236. function makeAssertionMessage (path, key, type, value, expected) {
  237. var buf = key + " should be " + expected + " but \"" + key + "." + type + "\"";
  238. if (path.length > 0) {
  239. buf += " in module \"" + (path.join('.')) + "\"";
  240. }
  241. buf += " is " + (JSON.stringify(value)) + ".";
  242. return buf
  243. }
  244. var Vue; // bind on install
  245. var Store = function Store (options) {
  246. var this$1 = this;
  247. if ( options === void 0 ) options = {};
  248. // Auto install if it is not done yet and `window` has `Vue`.
  249. // To allow users to avoid auto-installation in some cases,
  250. // this code should be placed here. See #731
  251. if (!Vue && typeof window !== 'undefined' && window.Vue) {
  252. install(window.Vue);
  253. }
  254. {
  255. assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
  256. assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.");
  257. assert(this instanceof Store, "Store must be called with the new operator.");
  258. }
  259. var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
  260. var strict = options.strict; if ( strict === void 0 ) strict = false;
  261. var state = options.state; if ( state === void 0 ) state = {};
  262. if (typeof state === 'function') {
  263. state = state() || {};
  264. }
  265. // store internal state
  266. this._committing = false;
  267. this._actions = Object.create(null);
  268. this._actionSubscribers = [];
  269. this._mutations = Object.create(null);
  270. this._wrappedGetters = Object.create(null);
  271. this._modules = new ModuleCollection(options);
  272. this._modulesNamespaceMap = Object.create(null);
  273. this._subscribers = [];
  274. this._watcherVM = new Vue();
  275. // bind commit and dispatch to self
  276. var store = this;
  277. var ref = this;
  278. var dispatch = ref.dispatch;
  279. var commit = ref.commit;
  280. this.dispatch = function boundDispatch (type, payload) {
  281. return dispatch.call(store, type, payload)
  282. };
  283. this.commit = function boundCommit (type, payload, options) {
  284. return commit.call(store, type, payload, options)
  285. };
  286. // strict mode
  287. this.strict = strict;
  288. // init root module.
  289. // this also recursively registers all sub-modules
  290. // and collects all module getters inside this._wrappedGetters
  291. installModule(this, state, [], this._modules.root);
  292. // initialize the store vm, which is responsible for the reactivity
  293. // (also registers _wrappedGetters as computed properties)
  294. resetStoreVM(this, state);
  295. // apply plugins
  296. plugins.forEach(function (plugin) { return plugin(this$1); });
  297. if (Vue.config.devtools) {
  298. devtoolPlugin(this);
  299. }
  300. };
  301. var prototypeAccessors = { state: { configurable: true } };
  302. prototypeAccessors.state.get = function () {
  303. return this._vm._data.$$state
  304. };
  305. prototypeAccessors.state.set = function (v) {
  306. {
  307. assert(false, "Use store.replaceState() to explicit replace store state.");
  308. }
  309. };
  310. Store.prototype.commit = function commit (_type, _payload, _options) {
  311. var this$1 = this;
  312. // check object-style commit
  313. var ref = unifyObjectStyle(_type, _payload, _options);
  314. var type = ref.type;
  315. var payload = ref.payload;
  316. var options = ref.options;
  317. var mutation = { type: type, payload: payload };
  318. var entry = this._mutations[type];
  319. if (!entry) {
  320. {
  321. console.error(("[vuex] unknown mutation type: " + type));
  322. }
  323. return
  324. }
  325. this._withCommit(function () {
  326. entry.forEach(function commitIterator (handler) {
  327. handler(payload);
  328. });
  329. });
  330. this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); });
  331. if (
  332. "development" !== 'production' &&
  333. options && options.silent
  334. ) {
  335. console.warn(
  336. "[vuex] mutation type: " + type + ". Silent option has been removed. " +
  337. 'Use the filter functionality in the vue-devtools'
  338. );
  339. }
  340. };
  341. Store.prototype.dispatch = function dispatch (_type, _payload) {
  342. var this$1 = this;
  343. // check object-style dispatch
  344. var ref = unifyObjectStyle(_type, _payload);
  345. var type = ref.type;
  346. var payload = ref.payload;
  347. var action = { type: type, payload: payload };
  348. var entry = this._actions[type];
  349. if (!entry) {
  350. {
  351. console.error(("[vuex] unknown action type: " + type));
  352. }
  353. return
  354. }
  355. this._actionSubscribers.forEach(function (sub) { return sub(action, this$1.state); });
  356. return entry.length > 1
  357. ? Promise.all(entry.map(function (handler) { return handler(payload); }))
  358. : entry[0](payload)
  359. };
  360. Store.prototype.subscribe = function subscribe (fn) {
  361. return genericSubscribe(fn, this._subscribers)
  362. };
  363. Store.prototype.subscribeAction = function subscribeAction (fn) {
  364. return genericSubscribe(fn, this._actionSubscribers)
  365. };
  366. Store.prototype.watch = function watch (getter, cb, options) {
  367. var this$1 = this;
  368. {
  369. assert(typeof getter === 'function', "store.watch only accepts a function.");
  370. }
  371. return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
  372. };
  373. Store.prototype.replaceState = function replaceState (state) {
  374. var this$1 = this;
  375. this._withCommit(function () {
  376. this$1._vm._data.$$state = state;
  377. });
  378. };
  379. Store.prototype.registerModule = function registerModule (path, rawModule, options) {
  380. if ( options === void 0 ) options = {};
  381. if (typeof path === 'string') { path = [path]; }
  382. {
  383. assert(Array.isArray(path), "module path must be a string or an Array.");
  384. assert(path.length > 0, 'cannot register the root module by using registerModule.');
  385. }
  386. this._modules.register(path, rawModule);
  387. installModule(this, this.state, path, this._modules.get(path), options.preserveState);
  388. // reset store to update getters...
  389. resetStoreVM(this, this.state);
  390. };
  391. Store.prototype.unregisterModule = function unregisterModule (path) {
  392. var this$1 = this;
  393. if (typeof path === 'string') { path = [path]; }
  394. {
  395. assert(Array.isArray(path), "module path must be a string or an Array.");
  396. }
  397. this._modules.unregister(path);
  398. this._withCommit(function () {
  399. var parentState = getNestedState(this$1.state, path.slice(0, -1));
  400. Vue.delete(parentState, path[path.length - 1]);
  401. });
  402. resetStore(this);
  403. };
  404. Store.prototype.hotUpdate = function hotUpdate (newOptions) {
  405. this._modules.update(newOptions);
  406. resetStore(this, true);
  407. };
  408. Store.prototype._withCommit = function _withCommit (fn) {
  409. var committing = this._committing;
  410. this._committing = true;
  411. fn();
  412. this._committing = committing;
  413. };
  414. Object.defineProperties( Store.prototype, prototypeAccessors );
  415. function genericSubscribe (fn, subs) {
  416. if (subs.indexOf(fn) < 0) {
  417. subs.push(fn);
  418. }
  419. return function () {
  420. var i = subs.indexOf(fn);
  421. if (i > -1) {
  422. subs.splice(i, 1);
  423. }
  424. }
  425. }
  426. function resetStore (store, hot) {
  427. store._actions = Object.create(null);
  428. store._mutations = Object.create(null);
  429. store._wrappedGetters = Object.create(null);
  430. store._modulesNamespaceMap = Object.create(null);
  431. var state = store.state;
  432. // init all modules
  433. installModule(store, state, [], store._modules.root, true);
  434. // reset vm
  435. resetStoreVM(store, state, hot);
  436. }
  437. function resetStoreVM (store, state, hot) {
  438. var oldVm = store._vm;
  439. // bind store public getters
  440. store.getters = {};
  441. var wrappedGetters = store._wrappedGetters;
  442. var computed = {};
  443. forEachValue(wrappedGetters, function (fn, key) {
  444. // use computed to leverage its lazy-caching mechanism
  445. computed[key] = function () { return fn(store); };
  446. Object.defineProperty(store.getters, key, {
  447. get: function () { return store._vm[key]; },
  448. enumerable: true // for local getters
  449. });
  450. });
  451. // use a Vue instance to store the state tree
  452. // suppress warnings just in case the user has added
  453. // some funky global mixins
  454. var silent = Vue.config.silent;
  455. Vue.config.silent = true;
  456. store._vm = new Vue({
  457. data: {
  458. $$state: state
  459. },
  460. computed: computed
  461. });
  462. Vue.config.silent = silent;
  463. // enable strict mode for new vm
  464. if (store.strict) {
  465. enableStrictMode(store);
  466. }
  467. if (oldVm) {
  468. if (hot) {
  469. // dispatch changes in all subscribed watchers
  470. // to force getter re-evaluation for hot reloading.
  471. store._withCommit(function () {
  472. oldVm._data.$$state = null;
  473. });
  474. }
  475. Vue.nextTick(function () { return oldVm.$destroy(); });
  476. }
  477. }
  478. function installModule (store, rootState, path, module, hot) {
  479. var isRoot = !path.length;
  480. var namespace = store._modules.getNamespace(path);
  481. // register in namespace map
  482. if (module.namespaced) {
  483. store._modulesNamespaceMap[namespace] = module;
  484. }
  485. // set state
  486. if (!isRoot && !hot) {
  487. var parentState = getNestedState(rootState, path.slice(0, -1));
  488. var moduleName = path[path.length - 1];
  489. store._withCommit(function () {
  490. Vue.set(parentState, moduleName, module.state);
  491. });
  492. }
  493. var local = module.context = makeLocalContext(store, namespace, path);
  494. module.forEachMutation(function (mutation, key) {
  495. var namespacedType = namespace + key;
  496. registerMutation(store, namespacedType, mutation, local);
  497. });
  498. module.forEachAction(function (action, key) {
  499. var type = action.root ? key : namespace + key;
  500. var handler = action.handler || action;
  501. registerAction(store, type, handler, local);
  502. });
  503. module.forEachGetter(function (getter, key) {
  504. var namespacedType = namespace + key;
  505. registerGetter(store, namespacedType, getter, local);
  506. });
  507. module.forEachChild(function (child, key) {
  508. installModule(store, rootState, path.concat(key), child, hot);
  509. });
  510. }
  511. /**
  512. * make localized dispatch, commit, getters and state
  513. * if there is no namespace, just use root ones
  514. */
  515. function makeLocalContext (store, namespace, path) {
  516. var noNamespace = namespace === '';
  517. var local = {
  518. dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
  519. var args = unifyObjectStyle(_type, _payload, _options);
  520. var payload = args.payload;
  521. var options = args.options;
  522. var type = args.type;
  523. if (!options || !options.root) {
  524. type = namespace + type;
  525. if ("development" !== 'production' && !store._actions[type]) {
  526. console.error(("[vuex] unknown local action type: " + (args.type) + ", global type: " + type));
  527. return
  528. }
  529. }
  530. return store.dispatch(type, payload)
  531. },
  532. commit: noNamespace ? store.commit : function (_type, _payload, _options) {
  533. var args = unifyObjectStyle(_type, _payload, _options);
  534. var payload = args.payload;
  535. var options = args.options;
  536. var type = args.type;
  537. if (!options || !options.root) {
  538. type = namespace + type;
  539. if ("development" !== 'production' && !store._mutations[type]) {
  540. console.error(("[vuex] unknown local mutation type: " + (args.type) + ", global type: " + type));
  541. return
  542. }
  543. }
  544. store.commit(type, payload, options);
  545. }
  546. };
  547. // getters and state object must be gotten lazily
  548. // because they will be changed by vm update
  549. Object.defineProperties(local, {
  550. getters: {
  551. get: noNamespace
  552. ? function () { return store.getters; }
  553. : function () { return makeLocalGetters(store, namespace); }
  554. },
  555. state: {
  556. get: function () { return getNestedState(store.state, path); }
  557. }
  558. });
  559. return local
  560. }
  561. function makeLocalGetters (store, namespace) {
  562. var gettersProxy = {};
  563. var splitPos = namespace.length;
  564. Object.keys(store.getters).forEach(function (type) {
  565. // skip if the target getter is not match this namespace
  566. if (type.slice(0, splitPos) !== namespace) { return }
  567. // extract local getter type
  568. var localType = type.slice(splitPos);
  569. // Add a port to the getters proxy.
  570. // Define as getter property because
  571. // we do not want to evaluate the getters in this time.
  572. Object.defineProperty(gettersProxy, localType, {
  573. get: function () { return store.getters[type]; },
  574. enumerable: true
  575. });
  576. });
  577. return gettersProxy
  578. }
  579. function registerMutation (store, type, handler, local) {
  580. var entry = store._mutations[type] || (store._mutations[type] = []);
  581. entry.push(function wrappedMutationHandler (payload) {
  582. handler.call(store, local.state, payload);
  583. });
  584. }
  585. function registerAction (store, type, handler, local) {
  586. var entry = store._actions[type] || (store._actions[type] = []);
  587. entry.push(function wrappedActionHandler (payload, cb) {
  588. var res = handler.call(store, {
  589. dispatch: local.dispatch,
  590. commit: local.commit,
  591. getters: local.getters,
  592. state: local.state,
  593. rootGetters: store.getters,
  594. rootState: store.state
  595. }, payload, cb);
  596. if (!isPromise(res)) {
  597. res = Promise.resolve(res);
  598. }
  599. if (store._devtoolHook) {
  600. return res.catch(function (err) {
  601. store._devtoolHook.emit('vuex:error', err);
  602. throw err
  603. })
  604. } else {
  605. return res
  606. }
  607. });
  608. }
  609. function registerGetter (store, type, rawGetter, local) {
  610. if (store._wrappedGetters[type]) {
  611. {
  612. console.error(("[vuex] duplicate getter key: " + type));
  613. }
  614. return
  615. }
  616. store._wrappedGetters[type] = function wrappedGetter (store) {
  617. return rawGetter(
  618. local.state, // local state
  619. local.getters, // local getters
  620. store.state, // root state
  621. store.getters // root getters
  622. )
  623. };
  624. }
  625. function enableStrictMode (store) {
  626. store._vm.$watch(function () { return this._data.$$state }, function () {
  627. {
  628. assert(store._committing, "Do not mutate vuex store state outside mutation handlers.");
  629. }
  630. }, { deep: true, sync: true });
  631. }
  632. function getNestedState (state, path) {
  633. return path.length
  634. ? path.reduce(function (state, key) { return state[key]; }, state)
  635. : state
  636. }
  637. function unifyObjectStyle (type, payload, options) {
  638. if (isObject(type) && type.type) {
  639. options = payload;
  640. payload = type;
  641. type = type.type;
  642. }
  643. {
  644. assert(typeof type === 'string', ("Expects string as the type, but found " + (typeof type) + "."));
  645. }
  646. return { type: type, payload: payload, options: options }
  647. }
  648. function install (_Vue) {
  649. if (Vue && _Vue === Vue) {
  650. {
  651. console.error(
  652. '[vuex] already installed. Vue.use(Vuex) should be called only once.'
  653. );
  654. }
  655. return
  656. }
  657. Vue = _Vue;
  658. applyMixin(Vue);
  659. }
  660. var mapState = normalizeNamespace(function (namespace, states) {
  661. var res = {};
  662. normalizeMap(states).forEach(function (ref) {
  663. var key = ref.key;
  664. var val = ref.val;
  665. res[key] = function mappedState () {
  666. var state = this.$store.state;
  667. var getters = this.$store.getters;
  668. if (namespace) {
  669. var module = getModuleByNamespace(this.$store, 'mapState', namespace);
  670. if (!module) {
  671. return
  672. }
  673. state = module.context.state;
  674. getters = module.context.getters;
  675. }
  676. return typeof val === 'function'
  677. ? val.call(this, state, getters)
  678. : state[val]
  679. };
  680. // mark vuex getter for devtools
  681. res[key].vuex = true;
  682. });
  683. return res
  684. });
  685. var mapMutations = normalizeNamespace(function (namespace, mutations) {
  686. var res = {};
  687. normalizeMap(mutations).forEach(function (ref) {
  688. var key = ref.key;
  689. var val = ref.val;
  690. res[key] = function mappedMutation () {
  691. var args = [], len = arguments.length;
  692. while ( len-- ) args[ len ] = arguments[ len ];
  693. var commit = this.$store.commit;
  694. if (namespace) {
  695. var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
  696. if (!module) {
  697. return
  698. }
  699. commit = module.context.commit;
  700. }
  701. return typeof val === 'function'
  702. ? val.apply(this, [commit].concat(args))
  703. : commit.apply(this.$store, [val].concat(args))
  704. };
  705. });
  706. return res
  707. });
  708. var mapGetters = normalizeNamespace(function (namespace, getters) {
  709. var res = {};
  710. normalizeMap(getters).forEach(function (ref) {
  711. var key = ref.key;
  712. var val = ref.val;
  713. val = namespace + val;
  714. res[key] = function mappedGetter () {
  715. if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
  716. return
  717. }
  718. if ("development" !== 'production' && !(val in this.$store.getters)) {
  719. console.error(("[vuex] unknown getter: " + val));
  720. return
  721. }
  722. return this.$store.getters[val]
  723. };
  724. // mark vuex getter for devtools
  725. res[key].vuex = true;
  726. });
  727. return res
  728. });
  729. var mapActions = normalizeNamespace(function (namespace, actions) {
  730. var res = {};
  731. normalizeMap(actions).forEach(function (ref) {
  732. var key = ref.key;
  733. var val = ref.val;
  734. res[key] = function mappedAction () {
  735. var args = [], len = arguments.length;
  736. while ( len-- ) args[ len ] = arguments[ len ];
  737. var dispatch = this.$store.dispatch;
  738. if (namespace) {
  739. var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
  740. if (!module) {
  741. return
  742. }
  743. dispatch = module.context.dispatch;
  744. }
  745. return typeof val === 'function'
  746. ? val.apply(this, [dispatch].concat(args))
  747. : dispatch.apply(this.$store, [val].concat(args))
  748. };
  749. });
  750. return res
  751. });
  752. var createNamespacedHelpers = function (namespace) { return ({
  753. mapState: mapState.bind(null, namespace),
  754. mapGetters: mapGetters.bind(null, namespace),
  755. mapMutations: mapMutations.bind(null, namespace),
  756. mapActions: mapActions.bind(null, namespace)
  757. }); };
  758. function normalizeMap (map) {
  759. return Array.isArray(map)
  760. ? map.map(function (key) { return ({ key: key, val: key }); })
  761. : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
  762. }
  763. function normalizeNamespace (fn) {
  764. return function (namespace, map) {
  765. if (typeof namespace !== 'string') {
  766. map = namespace;
  767. namespace = '';
  768. } else if (namespace.charAt(namespace.length - 1) !== '/') {
  769. namespace += '/';
  770. }
  771. return fn(namespace, map)
  772. }
  773. }
  774. function getModuleByNamespace (store, helper, namespace) {
  775. var module = store._modulesNamespaceMap[namespace];
  776. if ("development" !== 'production' && !module) {
  777. console.error(("[vuex] module namespace not found in " + helper + "(): " + namespace));
  778. }
  779. return module
  780. }
  781. var index = {
  782. Store: Store,
  783. install: install,
  784. version: '3.0.1',
  785. mapState: mapState,
  786. mapMutations: mapMutations,
  787. mapGetters: mapGetters,
  788. mapActions: mapActions,
  789. createNamespacedHelpers: createNamespacedHelpers
  790. };
  791. return index;
  792. })));