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