vuex.esm.js 28 KB

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