UNPKG

nervue

Version:

Vue composition and options API-compatible lightweight store library

2 lines (1 loc) 4.05 kB
!function(e,t){"use strict";function n(...e){console.warn("[nervue]:",...e)}function r(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}function o(e,t){"map"===r(e)&&t.forEach(((t,n)=>e.set(n,t)));for(const n in t)t.hasOwnProperty(n)&&(e[n]="object"===r(t[n])?o(e[n],t[n]):t[n]);return e}const s=Symbol.for("nervue");class c{installed=!1;_s={};_p=[];_e=t.effectScope();_a={};static sets=[];set(e){this.installed?this._s[e.$id]=e:c.sets.push(e)}unset(e){this._s[e]&&delete this._s[e]}use(e){this._p.push(e)}install(){this.installed||(c.sets.length&&(c.sets.forEach((e=>this._s[e.$id]=e)),c.sets=[]),this.installed=!0)}}let i=null;function u(){return i||(i=new c),i}function a(e){const{id:n,state:r,actions:s,guards:c,computed:i}=e,{assign:u}=Object;const a={},f={},l={};function h(e,...t){e.slice().forEach((e=>e(...t)))}function p(e){return{beforeList:a[e],afterList:f[e],onErrorList:l[e]}}function d(e){"function"==typeof e?e(this.$state):"object"==typeof e&&o(this.$state,e)}const b=r?.()||{},v=c?function(e,t,n){return new Proxy(t,{get:function(e,t,n){return Reflect.get(e,t,n)},set:function(e,t,r,o){let s={next:!1,value:r};if(n[t]){for(const e of n[t]){const t=e(s.value);s.next=t.next,s.value=r=t.value||r}s.next&&Reflect.set(e,t,s.value,o)}else Reflect.set(e,t,r,o);return!0}})}(0,b,c):null,y=t.ref(v||b),m={};m.$id=n,m.$patch=d,m.$subscribe=function(e){const{name:n,detached:r,before:o,after:s,onError:c}=e;let i,u,h;function p(){return new Promise((e=>{a[n]?.splice(i,1),f[n]?.splice(u,1),l[n]?.splice(h,1),e(!0)}))}return o&&!a[n]&&(a[n]=[]),s&&!f[n]&&(f[n]=[]),c&&!l[n]&&(l[n]=[]),o&&(i=a[n].push(o)-1),s&&(u=f[n].push(s)-1),c&&(h=l[n].push(c)-1),!r&&t.getCurrentInstance()&&t.onUnmounted(p),p},Object.defineProperty(m,"$state",{get:()=>t.toRaw(y.value),set:e=>{d(e)}}),Object.defineProperty(m,"_guards",{writable:!1,configurable:!0,value:c||{}}),Object.defineProperty(m,"_computed",{writable:!1,configurable:!0,value:Object.keys(i||{})});const j=t.reactive(u(m,t.toRefs(y.value),s,Object.keys(i||{}).reduce(((e,n)=>(e[n]=t.markRaw(t.computed((()=>i[n].call(j,j)))),e)),{})));return s&&Object.keys(s).forEach((e=>{j[e]=function(e,t,n){return function(){const{beforeList:r,afterList:o,onErrorList:s}=p(t),c=Array.from(arguments);let i;r&&h(r,...c);try{i=n.call(e,...c)}catch(e){throw s&&h(s,e),e}return i instanceof Promise?i.then((e=>(o&&h(o,e),e))).catch((e=>(s&&h(s,e),Promise.reject(e)))):(o&&h(o,i),i)}}(j,e,j[e])})),j}e.createComponent=function(e){return t.defineComponent({name:`use-${e.$id}-store`.toLowerCase(),setup:(n,{attrs:r,slots:o})=>()=>t.h("div",{class:"v-nervue",...r},{default:()=>o.default?.({...e()})})})},e.createNervue=function(){return u().constructor.prototype.install=t.isVue3?function(){const e=u(),{install:t}=e;return function(n){n&&(t.call(e),e._a=n,n.config.globalProperties.$nervue=u(),n.provide(s,u()))}}():function(){const e=u(),{install:t}=e;return function(n){e.installed||(t.call(e),n.mixin({beforeCreate(){const t=this.$options;t.nervue&&(this.$nervue=t.nervue,e._a=this)}}))}}(),u()},e.defineStore=function(e){const{assign:n}=Object,r=u(),o=r._e.run((()=>t.effectScope().run((()=>a(e))))),s=()=>o,c={};return r?._p.forEach((t=>n(c,t({store:o,options:e})||{}))),n(o,c),s.$id=o.$id,r.set(s),s},e.mapActions=function(e,t){const n=e(),r={};if(t)Array.isArray(t)?t.forEach((e=>{r[e]=n[e]})):Object.keys(t).forEach((e=>{r[e]=n[t[e]]}));else for(const e of Object.keys(n))"function"==typeof n[e]&&(r[e]=n[e]);return r},e.mapState=function(e,t){const n={};if(t)Array.isArray(t)?t.forEach((t=>{n[t]=function(){return e()[t]}})):Object.keys(t).forEach((r=>{n[r]=function(){const n=e();return"function"==typeof t[r]?t[r].call(this,n):n[t[r]]}}));else{const t=e();Object.keys(t).forEach((e=>{"function"!=typeof t[e]&&(n[e]=function(){return t[e]})}))}return n},e.nervueSymbol=s,e.useNervue=u,e.useStore=function(e){const t=u();return t.installed?t?._s[e]?t._s[e]():void n(`"${e}" store doesn't exist in the root object`):n("[nervue]: You should to create Nervue instance")}}({},vueDemi);