vuex.esm.js 26 KB

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