vuex.esm.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /**
  2. * vuex v3.1.2
  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. this._makeLocalGettersCache = Object.create(null);
  270. // bind commit and dispatch to self
  271. var store = this;
  272. var ref = this;
  273. var dispatch = ref.dispatch;
  274. var commit = ref.commit;
  275. this.dispatch = function boundDispatch (type, payload) {
  276. return dispatch.call(store, type, payload)
  277. };
  278. this.commit = function boundCommit (type, payload, options) {
  279. return commit.call(store, type, payload, options)
  280. };
  281. // strict mode
  282. this.strict = strict;
  283. var state = this._modules.root.state;
  284. // init root module.
  285. // this also recursively registers all sub-modules
  286. // and collects all module getters inside this._wrappedGetters
  287. installModule(this, state, [], this._modules.root);
  288. // initialize the store vm, which is responsible for the reactivity
  289. // (also registers _wrappedGetters as computed properties)
  290. resetStoreVM(this, state);
  291. // apply plugins
  292. plugins.forEach(function (plugin) { return plugin(this$1); });
  293. var useDevtools = options.devtools !== undefined ? options.devtools : Vue.config.devtools;
  294. if (useDevtools) {
  295. devtoolPlugin(this);
  296. }
  297. };
  298. var prototypeAccessors$1 = { state: { configurable: true } };
  299. prototypeAccessors$1.state.get = function () {
  300. return this._vm._data.$$state
  301. };
  302. prototypeAccessors$1.state.set = function (v) {
  303. if (process.env.NODE_ENV !== 'production') {
  304. assert(false, "use store.replaceState() to explicit replace store state.");
  305. }
  306. };
  307. Store.prototype.commit = function commit (_type, _payload, _options) {
  308. var this$1 = this;
  309. // check object-style commit
  310. var ref = unifyObjectStyle(_type, _payload, _options);
  311. var type = ref.type;
  312. var payload = ref.payload;
  313. var options = ref.options;
  314. var mutation = { type: type, payload: payload };
  315. var entry = this._mutations[type];
  316. if (!entry) {
  317. if (process.env.NODE_ENV !== 'production') {
  318. console.error(("[vuex] unknown mutation type: " + type));
  319. }
  320. return
  321. }
  322. this._withCommit(function () {
  323. entry.forEach(function commitIterator (handler) {
  324. handler(payload);
  325. });
  326. });
  327. this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); });
  328. if (
  329. process.env.NODE_ENV !== 'production' &&
  330. options && options.silent
  331. ) {
  332. console.warn(
  333. "[vuex] mutation type: " + type + ". Silent option has been removed. " +
  334. 'Use the filter functionality in the vue-devtools'
  335. );
  336. }
  337. };
  338. Store.prototype.dispatch = function dispatch (_type, _payload) {
  339. var this$1 = this;
  340. // check object-style dispatch
  341. var ref = unifyObjectStyle(_type, _payload);
  342. var type = ref.type;
  343. var payload = ref.payload;
  344. var action = { type: type, payload: payload };
  345. var entry = this._actions[type];
  346. if (!entry) {
  347. if (process.env.NODE_ENV !== 'production') {
  348. console.error(("[vuex] unknown action type: " + type));
  349. }
  350. return
  351. }
  352. try {
  353. this._actionSubscribers
  354. .filter(function (sub) { return sub.before; })
  355. .forEach(function (sub) { return sub.before(action, this$1.state); });
  356. } catch (e) {
  357. if (process.env.NODE_ENV !== 'production') {
  358. console.warn("[vuex] error in before action subscribers: ");
  359. console.error(e);
  360. }
  361. }
  362. var result = entry.length > 1
  363. ? Promise.all(entry.map(function (handler) { return handler(payload); }))
  364. : entry[0](payload);
  365. return result.then(function (res) {
  366. try {
  367. this$1._actionSubscribers
  368. .filter(function (sub) { return sub.after; })
  369. .forEach(function (sub) { return sub.after(action, this$1.state); });
  370. } catch (e) {
  371. if (process.env.NODE_ENV !== 'production') {
  372. console.warn("[vuex] error in after action subscribers: ");
  373. console.error(e);
  374. }
  375. }
  376. return res
  377. })
  378. };
  379. Store.prototype.subscribe = function subscribe (fn) {
  380. return genericSubscribe(fn, this._subscribers)
  381. };
  382. Store.prototype.subscribeAction = function subscribeAction (fn) {
  383. var subs = typeof fn === 'function' ? { before: fn } : fn;
  384. return genericSubscribe(subs, this._actionSubscribers)
  385. };
  386. Store.prototype.watch = function watch (getter, cb, options) {
  387. var this$1 = this;
  388. if (process.env.NODE_ENV !== 'production') {
  389. assert(typeof getter === 'function', "store.watch only accepts a function.");
  390. }
  391. return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
  392. };
  393. Store.prototype.replaceState = function replaceState (state) {
  394. var this$1 = this;
  395. this._withCommit(function () {
  396. this$1._vm._data.$$state = state;
  397. });
  398. };
  399. Store.prototype.registerModule = function registerModule (path, rawModule, options) {
  400. if ( options === void 0 ) options = {};
  401. if (typeof path === 'string') { path = [path]; }
  402. if (process.env.NODE_ENV !== 'production') {
  403. assert(Array.isArray(path), "module path must be a string or an Array.");
  404. assert(path.length > 0, 'cannot register the root module by using registerModule.');
  405. }
  406. this._modules.register(path, rawModule);
  407. installModule(this, this.state, path, this._modules.get(path), options.preserveState);
  408. // reset store to update getters...
  409. resetStoreVM(this, this.state);
  410. };
  411. Store.prototype.unregisterModule = function unregisterModule (path) {
  412. var this$1 = this;
  413. if (typeof path === 'string') { path = [path]; }
  414. if (process.env.NODE_ENV !== 'production') {
  415. assert(Array.isArray(path), "module path must be a string or an Array.");
  416. }
  417. this._modules.unregister(path);
  418. this._withCommit(function () {
  419. var parentState = getNestedState(this$1.state, path.slice(0, -1));
  420. Vue.delete(parentState, path[path.length - 1]);
  421. });
  422. resetStore(this);
  423. };
  424. Store.prototype.hotUpdate = function hotUpdate (newOptions) {
  425. this._modules.update(newOptions);
  426. resetStore(this, true);
  427. };
  428. Store.prototype._withCommit = function _withCommit (fn) {
  429. var committing = this._committing;
  430. this._committing = true;
  431. fn();
  432. this._committing = committing;
  433. };
  434. Object.defineProperties( Store.prototype, prototypeAccessors$1 );
  435. function genericSubscribe (fn, subs) {
  436. if (subs.indexOf(fn) < 0) {
  437. subs.push(fn);
  438. }
  439. return function () {
  440. var i = subs.indexOf(fn);
  441. if (i > -1) {
  442. subs.splice(i, 1);
  443. }
  444. }
  445. }
  446. function resetStore (store, hot) {
  447. store._actions = Object.create(null);
  448. store._mutations = Object.create(null);
  449. store._wrappedGetters = Object.create(null);
  450. store._modulesNamespaceMap = Object.create(null);
  451. var state = store.state;
  452. // init all modules
  453. installModule(store, state, [], store._modules.root, true);
  454. // reset vm
  455. resetStoreVM(store, state, hot);
  456. }
  457. function resetStoreVM (store, state, hot) {
  458. var oldVm = store._vm;
  459. // bind store public getters
  460. store.getters = {};
  461. // reset local getters cache
  462. store._makeLocalGettersCache = Object.create(null);
  463. var wrappedGetters = store._wrappedGetters;
  464. var computed = {};
  465. forEachValue(wrappedGetters, function (fn, key) {
  466. // use computed to leverage its lazy-caching mechanism
  467. // direct inline function use will lead to closure preserving oldVm.
  468. // using partial to return function with only arguments preserved in closure environment.
  469. computed[key] = partial(fn, store);
  470. Object.defineProperty(store.getters, key, {
  471. get: function () { return store._vm[key]; },
  472. enumerable: true // for local getters
  473. });
  474. });
  475. // use a Vue instance to store the state tree
  476. // suppress warnings just in case the user has added
  477. // some funky global mixins
  478. var silent = Vue.config.silent;
  479. Vue.config.silent = true;
  480. store._vm = new Vue({
  481. data: {
  482. $$state: state
  483. },
  484. computed: computed
  485. });
  486. Vue.config.silent = silent;
  487. // enable strict mode for new vm
  488. if (store.strict) {
  489. enableStrictMode(store);
  490. }
  491. if (oldVm) {
  492. if (hot) {
  493. // dispatch changes in all subscribed watchers
  494. // to force getter re-evaluation for hot reloading.
  495. store._withCommit(function () {
  496. oldVm._data.$$state = null;
  497. });
  498. }
  499. Vue.nextTick(function () { return oldVm.$destroy(); });
  500. }
  501. }
  502. function installModule (store, rootState, path, module, hot) {
  503. var isRoot = !path.length;
  504. var namespace = store._modules.getNamespace(path);
  505. // register in namespace map
  506. if (module.namespaced) {
  507. if (store._modulesNamespaceMap[namespace] && process.env.NODE_ENV !== 'production') {
  508. console.error(("[vuex] duplicate namespace " + namespace + " for the namespaced module " + (path.join('/'))));
  509. }
  510. store._modulesNamespaceMap[namespace] = module;
  511. }
  512. // set state
  513. if (!isRoot && !hot) {
  514. var parentState = getNestedState(rootState, path.slice(0, -1));
  515. var moduleName = path[path.length - 1];
  516. store._withCommit(function () {
  517. if (process.env.NODE_ENV !== 'production') {
  518. if (moduleName in parentState) {
  519. console.warn(
  520. ("[vuex] state field \"" + moduleName + "\" was overridden by a module with the same name at \"" + (path.join('.')) + "\"")
  521. );
  522. }
  523. }
  524. Vue.set(parentState, moduleName, module.state);
  525. });
  526. }
  527. var local = module.context = makeLocalContext(store, namespace, path);
  528. module.forEachMutation(function (mutation, key) {
  529. var namespacedType = namespace + key;
  530. registerMutation(store, namespacedType, mutation, local);
  531. });
  532. module.forEachAction(function (action, key) {
  533. var type = action.root ? key : namespace + key;
  534. var handler = action.handler || action;
  535. registerAction(store, type, handler, local);
  536. });
  537. module.forEachGetter(function (getter, key) {
  538. var namespacedType = namespace + key;
  539. registerGetter(store, namespacedType, getter, local);
  540. });
  541. module.forEachChild(function (child, key) {
  542. installModule(store, rootState, path.concat(key), child, hot);
  543. });
  544. }
  545. /**
  546. * make localized dispatch, commit, getters and state
  547. * if there is no namespace, just use root ones
  548. */
  549. function makeLocalContext (store, namespace, path) {
  550. var noNamespace = namespace === '';
  551. var local = {
  552. dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
  553. var args = unifyObjectStyle(_type, _payload, _options);
  554. var payload = args.payload;
  555. var options = args.options;
  556. var type = args.type;
  557. if (!options || !options.root) {
  558. type = namespace + type;
  559. if (process.env.NODE_ENV !== 'production' && !store._actions[type]) {
  560. console.error(("[vuex] unknown local action type: " + (args.type) + ", global type: " + type));
  561. return
  562. }
  563. }
  564. return store.dispatch(type, payload)
  565. },
  566. commit: noNamespace ? store.commit : function (_type, _payload, _options) {
  567. var args = unifyObjectStyle(_type, _payload, _options);
  568. var payload = args.payload;
  569. var options = args.options;
  570. var type = args.type;
  571. if (!options || !options.root) {
  572. type = namespace + type;
  573. if (process.env.NODE_ENV !== 'production' && !store._mutations[type]) {
  574. console.error(("[vuex] unknown local mutation type: " + (args.type) + ", global type: " + type));
  575. return
  576. }
  577. }
  578. store.commit(type, payload, options);
  579. }
  580. };
  581. // getters and state object must be gotten lazily
  582. // because they will be changed by vm update
  583. Object.defineProperties(local, {
  584. getters: {
  585. get: noNamespace
  586. ? function () { return store.getters; }
  587. : function () { return makeLocalGetters(store, namespace); }
  588. },
  589. state: {
  590. get: function () { return getNestedState(store.state, path); }
  591. }
  592. });
  593. return local
  594. }
  595. function makeLocalGetters (store, namespace) {
  596. if (!store._makeLocalGettersCache[namespace]) {
  597. var gettersProxy = {};
  598. var splitPos = namespace.length;
  599. Object.keys(store.getters).forEach(function (type) {
  600. // skip if the target getter is not match this namespace
  601. if (type.slice(0, splitPos) !== namespace) { return }
  602. // extract local getter type
  603. var localType = type.slice(splitPos);
  604. // Add a port to the getters proxy.
  605. // Define as getter property because
  606. // we do not want to evaluate the getters in this time.
  607. Object.defineProperty(gettersProxy, localType, {
  608. get: function () { return store.getters[type]; },
  609. enumerable: true
  610. });
  611. });
  612. store._makeLocalGettersCache[namespace] = gettersProxy;
  613. }
  614. return store._makeLocalGettersCache[namespace]
  615. }
  616. function registerMutation (store, type, handler, local) {
  617. var entry = store._mutations[type] || (store._mutations[type] = []);
  618. entry.push(function wrappedMutationHandler (payload) {
  619. handler.call(store, local.state, payload);
  620. });
  621. }
  622. function registerAction (store, type, handler, local) {
  623. var entry = store._actions[type] || (store._actions[type] = []);
  624. entry.push(function wrappedActionHandler (payload) {
  625. var res = handler.call(store, {
  626. dispatch: local.dispatch,
  627. commit: local.commit,
  628. getters: local.getters,
  629. state: local.state,
  630. rootGetters: store.getters,
  631. rootState: store.state
  632. }, payload);
  633. if (!isPromise(res)) {
  634. res = Promise.resolve(res);
  635. }
  636. if (store._devtoolHook) {
  637. return res.catch(function (err) {
  638. store._devtoolHook.emit('vuex:error', err);
  639. throw err
  640. })
  641. } else {
  642. return res
  643. }
  644. });
  645. }
  646. function registerGetter (store, type, rawGetter, local) {
  647. if (store._wrappedGetters[type]) {
  648. if (process.env.NODE_ENV !== 'production') {
  649. console.error(("[vuex] duplicate getter key: " + type));
  650. }
  651. return
  652. }
  653. store._wrappedGetters[type] = function wrappedGetter (store) {
  654. return rawGetter(
  655. local.state, // local state
  656. local.getters, // local getters
  657. store.state, // root state
  658. store.getters // root getters
  659. )
  660. };
  661. }
  662. function enableStrictMode (store) {
  663. store._vm.$watch(function () { return this._data.$$state }, function () {
  664. if (process.env.NODE_ENV !== 'production') {
  665. assert(store._committing, "do not mutate vuex store state outside mutation handlers.");
  666. }
  667. }, { deep: true, sync: true });
  668. }
  669. function getNestedState (state, path) {
  670. return path.length
  671. ? path.reduce(function (state, key) { return state[key]; }, state)
  672. : state
  673. }
  674. function unifyObjectStyle (type, payload, options) {
  675. if (isObject(type) && type.type) {
  676. options = payload;
  677. payload = type;
  678. type = type.type;
  679. }
  680. if (process.env.NODE_ENV !== 'production') {
  681. assert(typeof type === 'string', ("expects string as the type, but found " + (typeof type) + "."));
  682. }
  683. return { type: type, payload: payload, options: options }
  684. }
  685. function install (_Vue) {
  686. if (Vue && _Vue === Vue) {
  687. if (process.env.NODE_ENV !== 'production') {
  688. console.error(
  689. '[vuex] already installed. Vue.use(Vuex) should be called only once.'
  690. );
  691. }
  692. return
  693. }
  694. Vue = _Vue;
  695. applyMixin(Vue);
  696. }
  697. /**
  698. * Reduce the code which written in Vue.js for getting the state.
  699. * @param {String} [namespace] - Module's namespace
  700. * @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.
  701. * @param {Object}
  702. */
  703. var mapState = normalizeNamespace(function (namespace, states) {
  704. var res = {};
  705. if (process.env.NODE_ENV !== 'production' && !isValidMap(states)) {
  706. console.error('[vuex] mapState: mapper parameter must be either an Array or an Object');
  707. }
  708. normalizeMap(states).forEach(function (ref) {
  709. var key = ref.key;
  710. var val = ref.val;
  711. res[key] = function mappedState () {
  712. var state = this.$store.state;
  713. var getters = this.$store.getters;
  714. if (namespace) {
  715. var module = getModuleByNamespace(this.$store, 'mapState', namespace);
  716. if (!module) {
  717. return
  718. }
  719. state = module.context.state;
  720. getters = module.context.getters;
  721. }
  722. return typeof val === 'function'
  723. ? val.call(this, state, getters)
  724. : state[val]
  725. };
  726. // mark vuex getter for devtools
  727. res[key].vuex = true;
  728. });
  729. return res
  730. });
  731. /**
  732. * Reduce the code which written in Vue.js for committing the mutation
  733. * @param {String} [namespace] - Module's namespace
  734. * @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.
  735. * @return {Object}
  736. */
  737. var mapMutations = normalizeNamespace(function (namespace, mutations) {
  738. var res = {};
  739. if (process.env.NODE_ENV !== 'production' && !isValidMap(mutations)) {
  740. console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object');
  741. }
  742. normalizeMap(mutations).forEach(function (ref) {
  743. var key = ref.key;
  744. var val = ref.val;
  745. res[key] = function mappedMutation () {
  746. var args = [], len = arguments.length;
  747. while ( len-- ) args[ len ] = arguments[ len ];
  748. // Get the commit method from store
  749. var commit = this.$store.commit;
  750. if (namespace) {
  751. var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
  752. if (!module) {
  753. return
  754. }
  755. commit = module.context.commit;
  756. }
  757. return typeof val === 'function'
  758. ? val.apply(this, [commit].concat(args))
  759. : commit.apply(this.$store, [val].concat(args))
  760. };
  761. });
  762. return res
  763. });
  764. /**
  765. * Reduce the code which written in Vue.js for getting the getters
  766. * @param {String} [namespace] - Module's namespace
  767. * @param {Object|Array} getters
  768. * @return {Object}
  769. */
  770. var mapGetters = normalizeNamespace(function (namespace, getters) {
  771. var res = {};
  772. if (process.env.NODE_ENV !== 'production' && !isValidMap(getters)) {
  773. console.error('[vuex] mapGetters: mapper parameter must be either an Array or an Object');
  774. }
  775. normalizeMap(getters).forEach(function (ref) {
  776. var key = ref.key;
  777. var val = ref.val;
  778. // The namespace has been mutated by normalizeNamespace
  779. val = namespace + val;
  780. res[key] = function mappedGetter () {
  781. if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
  782. return
  783. }
  784. if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
  785. console.error(("[vuex] unknown getter: " + val));
  786. return
  787. }
  788. return this.$store.getters[val]
  789. };
  790. // mark vuex getter for devtools
  791. res[key].vuex = true;
  792. });
  793. return res
  794. });
  795. /**
  796. * Reduce the code which written in Vue.js for dispatch the action
  797. * @param {String} [namespace] - Module's namespace
  798. * @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.
  799. * @return {Object}
  800. */
  801. var mapActions = normalizeNamespace(function (namespace, actions) {
  802. var res = {};
  803. if (process.env.NODE_ENV !== 'production' && !isValidMap(actions)) {
  804. console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object');
  805. }
  806. normalizeMap(actions).forEach(function (ref) {
  807. var key = ref.key;
  808. var val = ref.val;
  809. res[key] = function mappedAction () {
  810. var args = [], len = arguments.length;
  811. while ( len-- ) args[ len ] = arguments[ len ];
  812. // get dispatch function from store
  813. var dispatch = this.$store.dispatch;
  814. if (namespace) {
  815. var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
  816. if (!module) {
  817. return
  818. }
  819. dispatch = module.context.dispatch;
  820. }
  821. return typeof val === 'function'
  822. ? val.apply(this, [dispatch].concat(args))
  823. : dispatch.apply(this.$store, [val].concat(args))
  824. };
  825. });
  826. return res
  827. });
  828. /**
  829. * Rebinding namespace param for mapXXX function in special scoped, and return them by simple object
  830. * @param {String} namespace
  831. * @return {Object}
  832. */
  833. var createNamespacedHelpers = function (namespace) { return ({
  834. mapState: mapState.bind(null, namespace),
  835. mapGetters: mapGetters.bind(null, namespace),
  836. mapMutations: mapMutations.bind(null, namespace),
  837. mapActions: mapActions.bind(null, namespace)
  838. }); };
  839. /**
  840. * Normalize the map
  841. * normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
  842. * normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
  843. * @param {Array|Object} map
  844. * @return {Object}
  845. */
  846. function normalizeMap (map) {
  847. if (!isValidMap(map)) {
  848. return []
  849. }
  850. return Array.isArray(map)
  851. ? map.map(function (key) { return ({ key: key, val: key }); })
  852. : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
  853. }
  854. /**
  855. * Validate whether given map is valid or not
  856. * @param {*} map
  857. * @return {Boolean}
  858. */
  859. function isValidMap (map) {
  860. return Array.isArray(map) || isObject(map)
  861. }
  862. /**
  863. * 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.
  864. * @param {Function} fn
  865. * @return {Function}
  866. */
  867. function normalizeNamespace (fn) {
  868. return function (namespace, map) {
  869. if (typeof namespace !== 'string') {
  870. map = namespace;
  871. namespace = '';
  872. } else if (namespace.charAt(namespace.length - 1) !== '/') {
  873. namespace += '/';
  874. }
  875. return fn(namespace, map)
  876. }
  877. }
  878. /**
  879. * Search a special module from store by namespace. if module not exist, print error message.
  880. * @param {Object} store
  881. * @param {String} helper
  882. * @param {String} namespace
  883. * @return {Object}
  884. */
  885. function getModuleByNamespace (store, helper, namespace) {
  886. var module = store._modulesNamespaceMap[namespace];
  887. if (process.env.NODE_ENV !== 'production' && !module) {
  888. console.error(("[vuex] module namespace not found in " + helper + "(): " + namespace));
  889. }
  890. return module
  891. }
  892. var index_esm = {
  893. Store: Store,
  894. install: install,
  895. version: '3.1.2',
  896. mapState: mapState,
  897. mapMutations: mapMutations,
  898. mapGetters: mapGetters,
  899. mapActions: mapActions,
  900. createNamespacedHelpers: createNamespacedHelpers
  901. };
  902. export default index_esm;
  903. export { Store, install, mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers };