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