vuex.esm.js 25 KB

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