vuex.esm.js 29 KB

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