UNPKG

vuex-local

Version:

Local state management within Vuex

10 lines 3.24 kB
/*! * vuex-local v0.2.0 * https://github.com/ktsn/vuex-local * * @license * Copyright (c) 2016-present katashin * Released under the MIT license * https://github.com/ktsn/vuex-local/blob/master/LICENSE */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.VuexLocal={})}(this,function(t){"use strict";function e(t,e){return[E,e,t].join("/")}function n(t,e){var n={};return Object.keys(t).forEach(function(o){n[e(t[o],o)]=t[o]}),n}function o(t,e){var n={};return Object.keys(t).forEach(function(o){n[o]=e(t[o],o)}),n}function r(t){return null!==t&&"object"==typeof t}function i(t){console.error("[vuex-local] "+t)}function c(t,e){if(!t)throw new Error("[vuex-local] "+e)}function u(t,e,n){var o=n.state,r=n.getters,i=void 0===r?{}:r,c=n.actions,u=void 0===c?{}:c,s=n.mutations,d=void 0===s?{}:s,h=e[e.length-1];t.registerModule(e,{state:o,getters:f(a(i,h),h),actions:f(l(u,i,h),h),mutations:f(d,h)})}function s(t,e){t.unregisterModule(e)}function f(t,o){return n(t,function(t,n){return e(n,o)})}function a(t,e){return o(t,function(n){return function(o,r,i){var c=d(Object.keys(t),r,e);return n(o,c,i,r)}})}function l(t,n,r){return o(t,function(t){return function(o,i){var c=o.commit,u=o.dispatch;o.commit=function(t,n,o){return void 0===o&&(o={}),"object"==typeof t&&(o=n,t=t.type,n=t),c(o.root?t:e(t,r),n,o)},o.dispatch=function(t,n,o){return void 0===o&&(o={}),"object"==typeof t&&(o=n,t=t.type,n=t),u(o.root?t:e(t,r),n,o)};var s=o.getters;o.rootGetters=s,o.getters=d(Object.keys(n),s,r),t(o,i)}})}function d(t,n,o){var r={};return t.forEach(function(t){Object.defineProperty(r,t,{get:function(){return n[e(t,o)]}})}),r}function h(t,e,n){p(t,n.state,e);var o=e[e.length-1];n.getters&&v(t,n.getters,o),n.actions&&m(t,n.actions,o),n.mutations&&y(t,n.mutations,o)}function b(t,e){return e.reduce(function(t,e){return t[e]},t)}function p(t,e,n){Object.keys(e).forEach(function(e){Object.defineProperty(t,e,{enumerable:!0,configurable:!0,get:function(){return b(t.$store.state,n)[e]}})})}function v(t,n,o){Object.keys(n).forEach(function(n){Object.defineProperty(t,n,{enumerable:!0,configurable:!0,get:function(){return t.$store.getters[e(n,o)]}})})}function m(t,n,o){Object.keys(n).forEach(function(n){t[n]=function(r){return t.$store.dispatch(e(n,o),r)}})}function y(t,n,o){Object.keys(n).forEach(function(n){t[n]=function(r){t.$store.commit(e(n,o),r)}})}function g(t,e){var n=e.parentModulePath,o=void 0===n?[]:n;t.mixin({created:function(){if(this.$options.local){c(this.$store,"store must be injected"),j(this.$store,o);var t=this.$options.local.call(this),e=t.name;c("string"==typeof e,"local module name must be string"),c(""!==e,"local module name cannot be empty"),c(r(t.state),"state must be object");var n=this._localModulePath=o.concat(e);u(this.$store,n,t),h(this,n,t)}},beforeDestroy:function(){this.$options.local&&s(this.$store,this._localModulePath)}})}function j(t,e){var n=t.state,o=[];e.forEach(function(e){n=n[e],o.push(e),void 0===n&&t.registerModule(o,{})})}function O(t,e){if(void 0===e&&(e={}),$)return void i("already installed");$=t,g($,e)}var $,E="local";t.install=O,Object.defineProperty(t,"__esModule",{value:!0})});