UNPKG

pinia

Version:

Intuitive, type safe and flexible Store for Vue

8 lines (7 loc) 5.3 kB
/*! * pinia v3.0.4 * (c) 2025 Eduardo San Martin Morote * @license MIT */ var Pinia=function(t,e){"use strict";let n;const o=t=>n=t,c=Symbol();function i(t){return t&&"object"==typeof t&&"[object Object]"===Object.prototype.toString.call(t)&&"function"!=typeof t.toJSON}var r;t.MutationType=void 0,(r=t.MutationType||(t.MutationType={})).direct="direct",r.patchObject="patch object",r.patchFunction="patch function";const s=()=>{};function a(t,n,o,c=s){t.add(n);const i=()=>{t.delete(n)&&c()};return!o&&e.getCurrentScope()&&e.onScopeDispose(i),i}function u(t,...e){t.forEach((t=>{t(...e)}))}const f=t=>t(),p=Symbol(),l=Symbol();function h(t,n){t instanceof Map&&n instanceof Map?n.forEach(((e,n)=>t.set(n,e))):t instanceof Set&&n instanceof Set&&n.forEach(t.add,t);for(const o in n){if(!n.hasOwnProperty(o))continue;const c=n[o],r=t[o];t[o]=i(r)&&i(c)&&t.hasOwnProperty(o)&&!e.isRef(c)&&!e.isReactive(c)?h(r,c):c}return t}const d=Symbol();function y(t){return!i(t)||!Object.prototype.hasOwnProperty.call(t,d)}const{assign:v}=Object;function b(n,c,i={},r,d,b){let S;const $=v({actions:{}},i),_={deep:!0};let j,m,O,R=new Set,g=new Set;const w=r.state.value[n];let P;function A(o){let c;j=m=!1,"function"==typeof o?(o(r.state.value[n]),c={type:t.MutationType.patchFunction,storeId:n,events:O}):(h(r.state.value[n],o),c={type:t.MutationType.patchObject,payload:o,storeId:n,events:O});const i=P=Symbol();e.nextTick().then((()=>{P===i&&(j=!0)})),m=!0,u(R,c,r.state.value[n])}b||w||(r.state.value[n]={}),e.ref({});const M=b?function(){const{state:t}=i,e=t?t():{};this.$patch((t=>{v(t,e)}))}:s;const k=(t,e="")=>{if(p in t)return t[l]=e,t;const c=function(){o(r);const e=Array.from(arguments),i=new Set,s=new Set;let a;u(g,{args:e,name:c[l],store:E,after:function(t){i.add(t)},onError:function(t){s.add(t)}});try{a=t.apply(this&&this.$id===n?this:E,e)}catch(t){throw u(s,t),t}return a instanceof Promise?a.then((t=>(u(i,t),t))).catch((t=>(u(s,t),Promise.reject(t)))):(u(i,a),a)};return c[p]=!0,c[l]=e,c},T={_p:r,$id:n,$onAction:a.bind(null,g),$patch:A,$reset:M,$subscribe(o,c={}){const i=a(R,o,c.detached,(()=>s())),s=S.run((()=>e.watch((()=>r.state.value[n]),(e=>{("sync"===c.flush?m:j)&&o({storeId:n,type:t.MutationType.direct,events:O},e)}),v({},_,c))));return i},$dispose:function(){S.stop(),R.clear(),g.clear(),r._s.delete(n)}},E=e.reactive(T);r._s.set(n,E);const x=(r._a&&r._a.runWithContext||f)((()=>r._e.run((()=>(S=e.effectScope()).run((()=>c({action:k})))))));for(const t in x){const o=x[t];if(e.isRef(o)&&(!e.isRef(I=o)||!I.effect)||e.isReactive(o))b||(w&&y(o)&&(e.isRef(o)?o.value=w[t]:h(o,w[t])),r.state.value[n][t]=o);else if("function"==typeof o){const e=k(o,t);x[t]=e,$.actions[t]=o}}var I;return v(E,x),v(e.toRaw(E),x),Object.defineProperty(E,"$state",{get:()=>r.state.value[n],set:t=>{A((e=>{v(e,t)}))}}),r._p.forEach((t=>{v(E,S.run((()=>t({store:E,app:r._a,pinia:r,options:$}))))})),w&&b&&i.hydrate&&i.hydrate(E.$state,w),j=!0,m=!0,E} /*! #__NO_SIDE_EFFECTS__ */let S="Store";function $(t,e){return Array.isArray(e)?e.reduce(((e,n)=>(e[n]=function(){return t(this.$pinia)[n]},e)),{}):Object.keys(e).reduce(((n,o)=>(n[o]=function(){const n=t(this.$pinia),c=e[o];return"function"==typeof c?c.call(this,n):n[c]},n)),{})}const _=$;return t.acceptHMRUpdate=function(t,e){return()=>{}},t.createPinia=function(){const t=e.effectScope(!0),n=t.run((()=>e.ref({})));let i=[],r=[];const s=e.markRaw({install(t){o(s),s._a=t,t.provide(c,s),t.config.globalProperties.$pinia=s,r.forEach((t=>i.push(t))),r=[]},use(t){return this._a?i.push(t):r.push(t),this},_p:i,_a:null,_e:t,_s:new Map,state:n});return s},t.defineStore=function(t,i,r){let s;const a="function"==typeof i;function u(r,u){const f=e.hasInjectionContext();(r=r||(f?e.inject(c,null):null))&&o(r),(r=n)._s.has(t)||(a?b(t,i,s,r):function(t,n,c){const{state:i,actions:r,getters:s}=n,a=c.state.value[t];let u;u=b(t,(function(){a||(c.state.value[t]=i?i():{});const n=e.toRefs(c.state.value[t]);return v(n,r,Object.keys(s||{}).reduce(((n,i)=>(n[i]=e.markRaw(e.computed((()=>{o(c);const e=c._s.get(t);return s[i].call(e,e)}))),n)),{}))}),n,c,0,!0)}(t,s,r));return r._s.get(t)}return s=a?r:i,u.$id=t,u},t.disposePinia=function(t){t._e.stop(),t._s.clear(),t._p.splice(0),t.state.value={},t._a=null},t.getActivePinia=()=>e.hasInjectionContext()&&e.inject(c)||n,t.mapActions=function(t,e){return Array.isArray(e)?e.reduce(((e,n)=>(e[n]=function(...e){return t(this.$pinia)[n](...e)},e)),{}):Object.keys(e).reduce(((n,o)=>(n[o]=function(...n){return t(this.$pinia)[e[o]](...n)},n)),{})},t.mapGetters=_,t.mapState=$,t.mapStores=function(...t){return t.reduce(((t,e)=>(t[e.$id+S]=function(){return e(this.$pinia)},t)),{})},t.mapWritableState=function(t,e){return Array.isArray(e)?e.reduce(((e,n)=>(e[n]={get(){return t(this.$pinia)[n]},set(e){return t(this.$pinia)[n]=e}},e)),{}):Object.keys(e).reduce(((n,o)=>(n[o]={get(){return t(this.$pinia)[e[o]]},set(n){return t(this.$pinia)[e[o]]=n}},n)),{})},t.setActivePinia=o,t.setMapStoreSuffix=function(t){S=t},t.shouldHydrate=y,t.skipHydrate=function(t){return Object.defineProperty(t,d,{})},t.storeToRefs=function(t){const n=e.toRaw(t),o={};for(const c in n){const i=n[c];i.effect?o[c]=e.computed({get:()=>t[c],set(e){t[c]=e}}):(e.isRef(i)||e.isReactive(i))&&(o[c]=e.toRef(t,c))}return o},t}({},Vue);