UNPKG

vuex-smart-module

Version:

Type safe Vuex module with powerful module features

10 lines 10 kB
/*! * vuex-smart-module v0.6.2 * https://github.com/ktsn/vuex-smart-module * * @license * Copyright (c) 2018 katashin * Released under the MIT license * https://github.com/ktsn/vuex-smart-module/blob/master/LICENSE */ var a,b;a=this,b=function(t,o,n){"use strict";var i=function(){return(i=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};var j=(e.prototype.$init=function(t){},Object.defineProperty(e.prototype,"state",{get:function(){return this.__ctx__.state},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"getters",{get:function(){return this.__ctx__.getters},enumerable:!1,configurable:!0}),e);function e(){}var O=(Object.defineProperty(r.prototype,"state",{get:function(){return this.__ctx__.state},enumerable:!1,configurable:!0}),r);function r(){}var x=(u.prototype.$init=function(t){},Object.defineProperty(u.prototype,"state",{get:function(){return this.__ctx__.state},enumerable:!1,configurable:!0}),Object.defineProperty(u.prototype,"getters",{get:function(){return this.__ctx__.getters},enumerable:!1,configurable:!0}),Object.defineProperty(u.prototype,"commit",{get:function(){return this.__ctx__.commit},enumerable:!1,configurable:!0}),Object.defineProperty(u.prototype,"dispatch",{get:function(){return this.__ctx__.dispatch},enumerable:!1,configurable:!0}),Object.defineProperty(u.prototype,"actions",{get:function(){return this.__ctx__.actions},enumerable:!1,configurable:!0}),Object.defineProperty(u.prototype,"mutations",{get:function(){return this.__ctx__.mutations},enumerable:!1,configurable:!0}),u);function u(){}function P(){}function S(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return function(e){t.forEach(function(t){return t(e)})}}function c(t,e){return t.reduce(function(t,e){return t[e]},e)}function a(e,n){var o={};return Object.keys(e).forEach(function(t){o[t]=n(e[t],t)}),o}function M(t,e){if(!t)throw new Error("[vuex-smart-module] ".concat(e))}function s(t){console.warn("[vuex-smart-module] DEPRECATED: ".concat(t))}function w(n,t,o,r){void 0===r&&(r={constructor:!0}),n.constructor!==t&&(Object.getOwnPropertyNames(n).forEach(function(t){var e;r[t]||(r[t]=!0,e=Object.getOwnPropertyDescriptor(n,t),o(e,t))}),w(Object.getPrototypeOf(n),t,o,r))}function p(t,e){var n=[];return w(t,e,function(t,e){"function"==typeof t.value&&n.push(e)}),n}function f(t){var e="The module need to be registered a store before using `Module#context` or `createMapper`";return{get path(){return M(void 0!==t.path,e),t.path},get namespace(){return M(void 0!==t.namespace,e),t.namespace}}}function l(t,e,n,o,r){return"string"==typeof n?t(e+n,o,r):t(i(i({},n),{type:e+n.type}),o)}function d(t,e,n,o,r){l(t.commit,e,n,o,r)}function h(t,e,n,o,r){return l(t.dispatch,e,n,o,r)}function m(o,r){var i=r.length,u={};return Object.keys(o.getters).forEach(function(t){var e=r===t.slice(0,i),n=t.slice(i);e&&n&&Object.defineProperty(u,n,{get:function(){return o.getters[t]},enumerable:!0})}),u}var y=(Object.defineProperty(b.prototype,"mutations",{get:function(){var n=this;if(this.__mutations__)return this.__mutations__;var t={},e=this.moduleOptions.mutations;return e&&p(e.prototype,O).forEach(function(e){Object.defineProperty(t,e,{value:function(t){return n.commit(e,t)},enumerable:!0})}),this.__mutations__=t},enumerable:!1,configurable:!0}),Object.defineProperty(b.prototype,"actions",{get:function(){var n=this;if(this.__actions__)return this.__actions__;var t={},e=this.moduleOptions.actions;return e&&p(e.prototype,x).forEach(function(e){Object.defineProperty(t,e,{value:function(t){return n.dispatch(e,t)},enumerable:!0})}),this.__actions__=t},enumerable:!1,configurable:!0}),Object.defineProperty(b.prototype,"state",{get:function(){return c(this.pos.path,this.store.state)},enumerable:!1,configurable:!0}),Object.defineProperty(b.prototype,"getters",{get:function(){return m(this.store,this.pos.namespace)},enumerable:!1,configurable:!0}),Object.defineProperty(b.prototype,"modules",{get:function(){var n=this,o={},r=this.moduleOptions.modules;return r&&Object.keys(r).forEach(function(t){var e=r[t];Object.defineProperty(o,t,{get:function(){return new b(f(e),n.store,e.options)}})}),o},enumerable:!1,configurable:!0}),b);function b(t,e,n){var o=this;this.pos=t,this.store=e,this.moduleOptions=n,this.commit=function(t,e,n){return d(o.store,o.pos.namespace,t,e,n)},this.dispatch=function(t,e,n){return h(o.store,o.pos.namespace,t,e,n)}}var g=(v.prototype.mapState=function(t){var o=this.pos;return _(t,function(n){return function(){var t=c(o.path,this.$store.state);if("function"!=typeof n)return t[n];var e=m(this.$store,o.namespace);return n.call(this,t,e)}})},v.prototype.mapGetters=function(t){var n=this.pos;return _(t,function(t){function e(){return this.$store.getters[n.namespace+t]}return e.vuex=!0,e})},v.prototype.mapMutations=function(t){var i=this.pos;return _(t,function(r){return function(){for(var n=this,t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var o=function(t,e){return d(n.$store,i.namespace,t,e)};return"function"==typeof r?r.apply(this,[o].concat(t)):o(r,t[0])}})},v.prototype.mapActions=function(t){var i=this.pos;return _(t,function(r){return function(){for(var n=this,t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var o=function(t,e){return h(n.$store,i.namespace,t,e)};return"function"==typeof r?r.apply(this,[o].concat(t)):o(r,t[0])}})},v);function v(t){this.pos=t}function _(t,e){return a(Array.isArray(t)?t.reduce(function(t,e){return t[e]=e,t},{}):t,e)}var $=(E.prototype.clone=function(){var t=i({},this.options);return t.modules&&(t.modules=a(t.modules,function(t){return t.clone()})),new E(t)},E.prototype.context=function(t){return new y(f(this),t,this.options)},E.prototype.mapState=function(t){return s("`Module#mapState` is deprecated. Use `createMapper` instead."),this.mapper.mapState(t)},E.prototype.mapGetters=function(t){return s("`Module#mapGetters` is deprecated. Use `createMapper` instead."),this.mapper.mapGetters(t)},E.prototype.mapMutations=function(t){return s("`Module#mapMutations` is deprecated. Use `createMapper` instead."),this.mapper.mapMutations(t)},E.prototype.mapActions=function(t){return s("`Module#mapActions` is deprecated. Use `createMapper` instead."),this.mapper.mapActions(t)},E.prototype.getStoreOptions=function(){var o="vuex-smart-module/injectStore",t=this.create([],""),e=t.options,n=t.injectStore;e.actions||(e.actions={}),e.actions[o]=function(){n(this)};return i(i({},e),{plugins:[function(e){e.dispatch(o);var n=e.hotUpdate;e.hotUpdate=function(t){n.call(e,t),e.dispatch(o)}}]})},E.prototype.create=function(r,i){M(!this.path||this.path.join(".")===r.join("."),"You are reusing one module on multiple places in the same store.\nClone it by `module.clone()` method to make sure every module in the store is unique."),this.path=r,this.namespace=i;var t,n,u,c,e,o,a,s,p,f,l,d,h=this.options,m=h.namespaced,y=void 0===m||m,b=h.state,g=h.getters,v=h.mutations,m=h.actions,_=h.modules,h=_?Object.keys(_).reduce(function(t,e){var n=_[e],o=null===(o=n.options.namespaced)||void 0===o||o?e+"/":"",o=n.create(r.concat(e),y?i+o:o);return t.options[e]=o.options,t.injectStore=S(t.injectStore,o.injectStore),t},{options:{},injectStore:P}):void 0,t=g&&(n=this,u=new(t=g),c={},w(t.prototype,j,function(t,e){var n,o;"function"!=typeof t.value&&!t.get||(n=t.value,o=t.get,c[e]=function(){var t=u;return o?o.call(t):n?n.bind(t):void 0})},{constructor:!0,$init:!0}),{getters:c,injectStore:function(t){var e=n.context(t);Object.defineProperty(u,"__ctx__",{get:function(){return e},configurable:!0}),u.$init(t)}}),e=v&&(o=this,a=new(e=v),s={},w(e.prototype,O,function(t,n){"function"==typeof t.value&&(s[n]=function(t,e){return a[n].call(a,e)})}),{mutations:s,injectStore:function(t){var e=o.context(t);Object.defineProperty(a,"__ctx__",{get:function(){return e},configurable:!0})}}),p=m&&(f=this,l=new(p=m),d={},w(p.prototype,x,function(t,n){"function"==typeof t.value&&(d[n]=function(t,e){return l[n].call(l,e)})},{constructor:!0,$init:!0}),{actions:d,injectStore:function(t){var e=f.context(t);Object.defineProperty(l,"__ctx__",{get:function(){return e},configurable:!0}),l.$init(t)}});return{options:{namespaced:y,state:function(){return b?new b:{}},getters:t&&t.getters,mutations:e&&e.mutations,actions:p&&p.actions,modules:h&&h.options},injectStore:S(h?h.injectStore:P,t?t.injectStore:P,e?e.injectStore:P,p?p.injectStore:P)}},E);function E(t){this.options=t=void 0===t?{}:t,this.mapper=new g(f(this))}t.Actions=x,t.Context=y,t.Getters=j,t.Module=$,t.Mutations=O,t.createComposable=function(e){return function(){var t=null===(t=n.getCurrentInstance())||void 0===t?void 0:t.proxy;return M(t,"Failed to get the current component instance"),M(t.$store,"Vuex store is not installed"),e.context(t.$store)}},t.createMapper=function(t){return new g(f(t))},t.createStore=function(t,e){void 0===e&&(e={});var t=(n=t.create([],"")).options,n=n.injectStore;return new o.Store(i(i(i({},t),e),{modules:i(i({},t.modules),e.modules),plugins:[n].concat(e.plugins||[])}))},t.hotUpdate=function(t,e){var e=(n=e.create([],"")).options,n=n.injectStore;t.hotUpdate(e),n(t)},t.inject=function(t,e){var t=t.prototype,n={};return Object.keys(e).forEach(function(t){n[t]={configurable:!0,enumerable:!0,writable:!0,value:e[t]}}),Object.create(t,n)},t.registerModule=function(t,e,n,o,r){e="string"==typeof e?[e]:e,o=(n=o.create(e,""!==(o=n)&&null!==o?"/"===o[o.length-1]?o:o+"/":"")).options,n=n.injectStore,t.registerModule(e,o,r),n(t)},t.unregisterModule=function(t,e){M(e.path,"The module seems not registered in the store"),t.unregisterModule(e.path)},Object.defineProperty(t,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?b(exports,require("vuex"),require("vue-demi")):"function"==typeof define&&define.amd?define(["exports","vuex","vue-demi"],b):b((a="undefined"!=typeof globalThis?globalThis:a||self).VuexSmartModule={},a.Vuex,a.VueDemi);