UNPKG

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