UNPKG

9.94 kBJavaScriptView Raw
1/**
2 * vuex v4.0.0-alpha.1
3 * (c) 2020 Evan You
4 * @license MIT
5 */
6!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("vue")):"function"==typeof define&&define.amd?define(["vue"],e):(t=t||self).Vuex=e(t.Vue)}(this,function(t){"use strict";var e="store";var n=("undefined"!=typeof window?window:"undefined"!=typeof global?global:{}).__VUE_DEVTOOLS_GLOBAL_HOOK__;function r(t,e){Object.keys(t).forEach(function(n){return e(t[n],n)})}function o(t){return null!==t&&"object"==typeof t}var i=function(t,e){this.runtime=e,this._children=Object.create(null),this._rawModule=t;var n=t.state;this.state=("function"==typeof n?n():n)||{}},a={namespaced:{configurable:!0}};a.namespaced.get=function(){return!!this._rawModule.namespaced},i.prototype.addChild=function(t,e){this._children[t]=e},i.prototype.removeChild=function(t){delete this._children[t]},i.prototype.getChild=function(t){return this._children[t]},i.prototype.update=function(t){this._rawModule.namespaced=t.namespaced,t.actions&&(this._rawModule.actions=t.actions),t.mutations&&(this._rawModule.mutations=t.mutations),t.getters&&(this._rawModule.getters=t.getters)},i.prototype.forEachChild=function(t){r(this._children,t)},i.prototype.forEachGetter=function(t){this._rawModule.getters&&r(this._rawModule.getters,t)},i.prototype.forEachAction=function(t){this._rawModule.actions&&r(this._rawModule.actions,t)},i.prototype.forEachMutation=function(t){this._rawModule.mutations&&r(this._rawModule.mutations,t)},Object.defineProperties(i.prototype,a);var s=function(t){this.register([],t,!1)};s.prototype.get=function(t){return t.reduce(function(t,e){return t.getChild(e)},this.root)},s.prototype.getNamespace=function(t){var e=this.root;return t.reduce(function(t,n){return t+((e=e.getChild(n)).namespaced?n+"/":"")},"")},s.prototype.update=function(t){!function t(e,n,r){n.update(r);if(r.modules)for(var o in r.modules){if(!n.getChild(o))return;t(e.concat(o),n.getChild(o),r.modules[o])}}([],this.root,t)},s.prototype.register=function(t,e,n){var o=this;void 0===n&&(n=!0);var a=new i(e,n);0===t.length?this.root=a:this.get(t.slice(0,-1)).addChild(t[t.length-1],a);e.modules&&r(e.modules,function(e,r){o.register(t.concat(r),e,n)})},s.prototype.unregister=function(t){var e=this.get(t.slice(0,-1)),n=t[t.length-1];e.getChild(n).runtime&&e.removeChild(n)};var c=function(t){var e=this;void 0===t&&(t={});var r=t.plugins;void 0===r&&(r=[]);var o=t.strict;void 0===o&&(o=!1),this._committing=!1,this._actions=Object.create(null),this._actionSubscribers=[],this._mutations=Object.create(null),this._wrappedGetters=Object.create(null),this._modules=new s(t),this._modulesNamespaceMap=Object.create(null),this._subscribers=[],this._makeLocalGettersCache=Object.create(null);var i=this,a=this.dispatch,c=this.commit;this.dispatch=function(t,e){return a.call(i,t,e)},this.commit=function(t,e,n){return c.call(i,t,e,n)},this.strict=o;var u=this._modules.root.state;l(this,u,[],this._modules.root),p(this,u),r.forEach(function(t){return t(e)}),(void 0===t.devtools||t.devtools)&&function(t){n&&(t._devtoolHook=n,n.emit("vuex:init",t),n.on("vuex:travel-to-state",function(e){t.replaceState(e)}),t.subscribe(function(t,e){n.emit("vuex:mutation",t,e)}))}(this)},u={state:{configurable:!0}};function f(t,e){return e.indexOf(t)<0&&e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}function h(t,e){t._actions=Object.create(null),t._mutations=Object.create(null),t._wrappedGetters=Object.create(null),t._modulesNamespaceMap=Object.create(null);var n=t.state;l(t,n,[],t._modules.root,!0),p(t,n,e)}function p(e,n,o){var i=e._vm;e.getters={},e._makeLocalGettersCache=Object.create(null);var a=e._wrappedGetters,s={};r(a,function(n,r){s[r]=function(t,e){return function(){return t(e)}}(n,e),Object.defineProperty(e.getters,r,{get:function(){return t.computed(function(){return s[r]()}).value},enumerable:!0})}),e._vm=t.reactive({_data:{$$state:n}}),e.strict&&function(e){t.watch(function(){return e._vm._data.$$state},function(){},{deep:!0,flush:"sync"})}(e),i&&o&&e._withCommit(function(){i._data.$$state=null})}function l(t,e,n,r,o){var i=!n.length,a=t._modules.getNamespace(n);if(r.namespaced&&(t._modulesNamespaceMap[a],t._modulesNamespaceMap[a]=r),!i&&!o){var s=d(e,n.slice(0,-1)),c=n[n.length-1];t._withCommit(function(){s[c]=r.state})}var u=r.context=function(t,e,n){var r=""===e,o={dispatch:r?t.dispatch:function(n,r,o){var i=m(n,r,o),a=i.payload,s=i.options,c=i.type;return s&&s.root||(c=e+c),t.dispatch(c,a)},commit:r?t.commit:function(n,r,o){var i=m(n,r,o),a=i.payload,s=i.options,c=i.type;s&&s.root||(c=e+c),t.commit(c,a,s)}};return Object.defineProperties(o,{getters:{get:r?function(){return t.getters}:function(){return function(t,e){if(!t._makeLocalGettersCache[e]){var n={},r=e.length;Object.keys(t.getters).forEach(function(o){if(o.slice(0,r)===e){var i=o.slice(r);Object.defineProperty(n,i,{get:function(){return t.getters[o]},enumerable:!0})}}),t._makeLocalGettersCache[e]=n}return t._makeLocalGettersCache[e]}(t,e)}},state:{get:function(){return d(t.state,n)}}}),o}(t,a,n);r.forEachMutation(function(e,n){!function(t,e,n,r){(t._mutations[e]||(t._mutations[e]=[])).push(function(e){n.call(t,r.state,e)})}(t,a+n,e,u)}),r.forEachAction(function(e,n){var r=e.root?n:a+n,o=e.handler||e;!function(t,e,n,r){(t._actions[e]||(t._actions[e]=[])).push(function(e){var o,i=n.call(t,{dispatch:r.dispatch,commit:r.commit,getters:r.getters,state:r.state,rootGetters:t.getters,rootState:t.state},e);return(o=i)&&"function"==typeof o.then||(i=Promise.resolve(i)),t._devtoolHook?i.catch(function(e){throw t._devtoolHook.emit("vuex:error",e),e}):i})}(t,r,o,u)}),r.forEachGetter(function(e,n){!function(t,e,n,r){if(t._wrappedGetters[e])return;t._wrappedGetters[e]=function(t){return n(r.state,r.getters,t.state,t.getters)}}(t,a+n,e,u)}),r.forEachChild(function(r,i){l(t,e,n.concat(i),r,o)})}function d(t,e){return e.reduce(function(t,e){return t[e]},t)}function m(t,e,n){return o(t)&&t.type&&(n=e,e=t,t=t.type),{type:t,payload:e,options:n}}c.prototype.install=function(t,n){!function(t,n,r){t.provide(r||e,n),t.mixin({beforeCreate:function(){this.parent?this.$store=this.parent.$options.$store:this.$store="function"==typeof n?n():n}})}(t,this,n)},u.state.get=function(){return this._vm._data.$$state},u.state.set=function(t){},c.prototype.commit=function(t,e,n){var r=this,o=m(t,e,n),i=o.type,a=o.payload,s={type:i,payload:a},c=this._mutations[i];c&&(this._withCommit(function(){c.forEach(function(t){t(a)})}),this._subscribers.slice().forEach(function(t){return t(s,r.state)}))},c.prototype.dispatch=function(t,e){var n=this,r=m(t,e),o=r.type,i=r.payload,a={type:o,payload:i},s=this._actions[o];if(s){try{this._actionSubscribers.slice().filter(function(t){return t.before}).forEach(function(t){return t.before(a,n.state)})}catch(t){}return(s.length>1?Promise.all(s.map(function(t){return t(i)})):s[0](i)).then(function(t){try{n._actionSubscribers.filter(function(t){return t.after}).forEach(function(t){return t.after(a,n.state)})}catch(t){}return t})}},c.prototype.subscribe=function(t){return f(t,this._subscribers)},c.prototype.subscribeAction=function(t){return f("function"==typeof t?{before:t}:t,this._actionSubscribers)},c.prototype.watch=function(e,n,r){var o=this;return t.watch(function(){return e(o.state,o.getters)},n,Object.assign({},r))},c.prototype.replaceState=function(t){var e=this;this._withCommit(function(){e._vm._data.$$state=t})},c.prototype.registerModule=function(t,e,n){void 0===n&&(n={}),"string"==typeof t&&(t=[t]),this._modules.register(t,e),l(this,this.state,t,this._modules.get(t),n.preserveState),p(this,this.state)},c.prototype.unregisterModule=function(t){var e=this;"string"==typeof t&&(t=[t]),this._modules.unregister(t),this._withCommit(function(){delete d(e.state,t.slice(0,-1))[t[t.length-1]]}),h(this)},c.prototype.hotUpdate=function(t){this._modules.update(t),h(this,!0)},c.prototype._withCommit=function(t){var e=this._committing;this._committing=!0,t(),this._committing=e},Object.defineProperties(c.prototype,u);var v=w(function(t,e){var n={};return b(e).forEach(function(e){var r=e.key,o=e.val;n[r]=function(){var e=this.$store.state,n=this.$store.getters;if(t){var r=O(this.$store,"mapState",t);if(!r)return;e=r.context.state,n=r.context.getters}return"function"==typeof o?o.call(this,e,n):e[o]},n[r].vuex=!0}),n}),_=w(function(t,e){var n={};return b(e).forEach(function(e){var r=e.key,o=e.val;n[r]=function(){for(var e=[],n=arguments.length;n--;)e[n]=arguments[n];var r=this.$store.commit;if(t){var i=O(this.$store,"mapMutations",t);if(!i)return;r=i.context.commit}return"function"==typeof o?o.apply(this,[r].concat(e)):r.apply(this.$store,[o].concat(e))}}),n}),y=w(function(t,e){var n={};return b(e).forEach(function(e){var r=e.key,o=e.val;o=t+o,n[r]=function(){if(!t||O(this.$store,"mapGetters",t))return this.$store.getters[o]},n[r].vuex=!0}),n}),g=w(function(t,e){var n={};return b(e).forEach(function(e){var r=e.key,o=e.val;n[r]=function(){for(var e=[],n=arguments.length;n--;)e[n]=arguments[n];var r=this.$store.dispatch;if(t){var i=O(this.$store,"mapActions",t);if(!i)return;r=i.context.dispatch}return"function"==typeof o?o.apply(this,[r].concat(e)):r.apply(this.$store,[o].concat(e))}}),n});function b(t){return function(t){return Array.isArray(t)||o(t)}(t)?Array.isArray(t)?t.map(function(t){return{key:t,val:t}}):Object.keys(t).map(function(e){return{key:e,val:t[e]}}):[]}function w(t){return function(e,n){return"string"!=typeof e?(n=e,e=""):"/"!==e.charAt(e.length-1)&&(e+="/"),t(e,n)}}function O(t,e,n){return t._modulesNamespaceMap[n]}return{version:"4.0.0-alpha.1",createStore:function(t){return new c(t)},Store:c,useStore:function(n){return void 0===n&&(n=null),t.inject(null!==n?n:e)},mapState:v,mapMutations:_,mapGetters:y,mapActions:g,createNamespacedHelpers:function(t){return{mapState:v.bind(null,t),mapGetters:y.bind(null,t),mapMutations:_.bind(null,t),mapActions:g.bind(null,t)}}}});
\No newline at end of file