pinia-0c8641d4.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import{i as t}from"./vue-demi-71ba0ef2.js";import{ao as e,h as n,an as s,p as o,i as a,X as c,x as r,aw as i,ab as u,ax as f,g as p,f as l,n as h,ad as d,m as y}from"./@vue-37836d09.js";
  2. /*!
  3. * pinia v2.3.1
  4. * (c) 2025 Eduardo San Martin Morote
  5. * @license MIT
  6. */
  7. /**
  8. * setActivePinia must be called to handle SSR at the top of functions like
  9. * `fetch`, `setup`, `serverPrefetch` and others
  10. */let b;
  11. /**
  12. * Sets or unsets the active pinia. Used in SSR and internally when calling
  13. * actions and getters
  14. *
  15. * @param pinia - Pinia instance
  16. */
  17. // @ts-expect-error: cannot constrain the type of the return
  18. const v=t=>b=t,_=/* istanbul ignore next */Symbol();function m(
  19. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  20. t){return t&&"object"==typeof t&&"[object Object]"===Object.prototype.toString.call(t)&&"function"!=typeof t.toJSON}
  21. // type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> }
  22. // TODO: can we change these to numbers?
  23. /**
  24. * Possible types for SubscriptionCallback
  25. */var j;
  26. /**
  27. * Creates a Pinia instance to be used by the application
  28. */
  29. function O(){const o=e(!0),a=o.run((()=>n({})));
  30. // NOTE: here we could check the window object for a state and directly set it
  31. // if there is anything like it with Vue 3 SSR
  32. let c=[],r=[];
  33. // plugins added before calling app.use(pinia)
  34. const i=s({install(t){
  35. // this allows calling useStore() outside of a component setup after
  36. // installing pinia's plugin
  37. v(i),i._a=t,t.provide(_,i),t.config.globalProperties.$pinia=i,r.forEach((t=>c.push(t))),r=[]},use(e){return this._a||t?c.push(e):r.push(e),this},_p:c,
  38. // it's actually undefined here
  39. // @ts-expect-error
  40. _a:null,_e:o,_s:new Map,state:a});return i}!function(t){
  41. /**
  42. * Direct mutation of the state:
  43. *
  44. * - `store.name = 'new name'`
  45. * - `store.$state.name = 'new name'`
  46. * - `store.list.push('new item')`
  47. */
  48. t.direct="direct",
  49. /**
  50. * Mutated the state with `$patch` and an object
  51. *
  52. * - `store.$patch({ name: 'newName' })`
  53. */
  54. t.patchObject="patch object",
  55. /**
  56. * Mutated the state with `$patch` and a function
  57. *
  58. * - `store.$patch(state => state.name = 'newName')`
  59. */
  60. t.patchFunction="patch function"}(j||(j={}));const $=()=>{};function g(t,e,n,s=$){t.push(e);const o=()=>{const n=t.indexOf(e);n>-1&&(t.splice(n,1),s())};return!n&&p()&&l(o),o}function S(t,...e){t.slice().forEach((t=>{t(...e)}))}const P=t=>t()
  61. /**
  62. * Marks a function as an action for `$onAction`
  63. * @internal
  64. */,w=Symbol(),E=Symbol();function x(t,e){
  65. // Handle Map instances
  66. t instanceof Map&&e instanceof Map?e.forEach(((e,n)=>t.set(n,e))):t instanceof Set&&e instanceof Set&&
  67. // Handle Set instances
  68. e.forEach(t.add,t);
  69. // no need to go through symbols because they cannot be serialized anyway
  70. for(const n in e){if(!e.hasOwnProperty(n))continue;const s=e[n],o=t[n];m(o)&&m(s)&&t.hasOwnProperty(n)&&!r(s)&&!i(s)?
  71. // NOTE: here I wanted to warn about inconsistent types but it's not possible because in setup stores one might
  72. // start the value of a property as a certain type e.g. a Map, and then for some reason, during SSR, change that
  73. // to `undefined`. When trying to hydrate, we want to override the Map with `undefined`.
  74. t[n]=x(o,s):
  75. // @ts-expect-error: subPatch is a valid value
  76. t[n]=s}return t}const I=/* istanbul ignore next */Symbol();
  77. /**
  78. * Returns whether a value should be hydrated
  79. *
  80. * @param obj - target variable
  81. * @returns true if `obj` should be hydrated
  82. */const{assign:M}=Object;function A(t,s,o={},f,p,l){let d;const y=M({actions:{}},o),b={deep:!0};
  83. // watcher options for $subscribe
  84. // internal state
  85. let _,O,A,F=[],k=[];// set to true at the end
  86. const z=f.state.value[t];
  87. // avoid setting the state for option stores if it is set
  88. // by the setup
  89. // avoid triggering too many listeners
  90. // https://github.com/vuejs/pinia/issues/1129
  91. let C;function J(e){let n;_=O=!1,"function"==typeof e?(e(f.state.value[t]),n={type:j.patchFunction,storeId:t,events:A}):(x(f.state.value[t],e),n={type:j.patchObject,payload:e,storeId:t,events:A});const s=C=Symbol();h().then((()=>{C===s&&(_=!0)})),O=!0,
  92. // because we paused the watcher, we need to manually call the subscriptions
  93. S(F,n,f.state.value[t])}l||z||(f.state.value[t]={}),n({});const N=l?function(){const{state:t}=o,e=t?t():{};
  94. // we use a patch to group all changes into one single subscription
  95. this.$patch((t=>{
  96. // @ts-expect-error: FIXME: shouldn't error?
  97. M(t,e)}))}:/* istanbul ignore next */
  98. $;
  99. /**
  100. * Helper that wraps function so it can be tracked with $onAction
  101. * @param fn - action to wrap
  102. * @param name - name of the action
  103. */
  104. const W=(e,n="")=>{if(w in e)return e[E]=n,e;const s=function(){v(f);const n=Array.from(arguments),o=[],a=[];let c;
  105. // @ts-expect-error
  106. S(k,{args:n,name:s[E],store:q,after:function(t){o.push(t)},onError:function(t){a.push(t)}});try{c=e.apply(this&&this.$id===t?this:q,n);
  107. // handle sync errors
  108. }catch(r){throw S(a,r),r}return c instanceof Promise?c.then((t=>(S(o,t),t))).catch((t=>(S(a,t),Promise.reject(t)))):(
  109. // trigger after callbacks
  110. S(o,c),c)};// will be set later
  111. // @ts-expect-error: we are intentionally limiting the returned type to just Fn
  112. // because all the added properties are internals that are exposed through `$onAction()` only
  113. return s[w]=!0,s[E]=n,s},X={_p:f,
  114. // _s: scope,
  115. $id:t,$onAction:g.bind(null,k),$patch:J,$reset:N,$subscribe(e,n={}){const s=g(F,e,n.detached,(()=>o())),o=d.run((()=>a((()=>f.state.value[t]),(s=>{("sync"===n.flush?O:_)&&e({storeId:t,type:j.direct,events:A},s)}),M({},b,n))));return s},$dispose:function(){d.stop(),F=[],k=[],f._s.delete(t)}},q=c(X);
  116. // store the partial store now so the setup of stores can instantiate each other before they are finished without
  117. // creating infinite loops.
  118. f._s.set(t,q);const B=(f._a&&f._a.runWithContext||P)((()=>f._e.run((()=>(d=e()).run((()=>s({action:W})))))));
  119. // TODO: idea create skipSerialize that marks properties as non serializable and they are skipped
  120. // overwrite existing actions to support $onAction
  121. for(const e in B){const n=B[e];if(r(n)&&(!r(G=n)||!G.effect)||i(n))
  122. // mark it as a piece of state to be serialized
  123. l||(
  124. // in setup stores we must hydrate the state and sync pinia state tree with the refs the user just created
  125. !z||m(D=n)&&D.hasOwnProperty(I)||(r(n)?n.value=z[e]:
  126. // probably a reactive object, lets recursively assign
  127. // @ts-expect-error: prop is unknown
  128. x(n,z[e])),f.state.value[t][e]=n);
  129. // action
  130. else if("function"==typeof n){const t=W(n,e);
  131. // this a hot module replacement store because the hotUpdate method needs
  132. // to do it with the right context
  133. /* istanbul ignore if */
  134. // @ts-expect-error
  135. B[e]=t,
  136. // list actions so they can be used in plugins
  137. // @ts-expect-error
  138. y.actions[e]=n}}
  139. // add the state, getters, and action properties
  140. /* istanbul ignore if */var D,G;return M(q,B),
  141. // allows retrieving reactive objects with `storeToRefs()`. Must be called after assigning to the reactive object.
  142. // Make `storeToRefs()` work with `reactive()` #799
  143. M(u(q),B),
  144. // use this instead of a computed with setter to be able to create it anywhere
  145. // without linking the computed lifespan to wherever the store is first
  146. // created.
  147. Object.defineProperty(q,"$state",{get:()=>f.state.value[t],set:t=>{J((e=>{
  148. // @ts-expect-error: FIXME: shouldn't error?
  149. M(e,t)}))}}),
  150. // apply all plugins
  151. f._p.forEach((t=>{M(q,d.run((()=>t({store:q,app:f._a,pinia:f,options:y}))))})),
  152. // only apply hydrate to option stores with an initial state in pinia
  153. z&&l&&o.hydrate&&o.hydrate(q.$state,z),_=!0,O=!0,q}
  154. // allows unused stores to be tree shaken
  155. /*! #__NO_SIDE_EFFECTS__ */function F(
  156. // TODO: add proper types from above
  157. t,e,n){let a,c;const r="function"==typeof e;function i(t,n){const i=f();(t=
  158. // in test mode, ignore the argument provided as we can always retrieve a
  159. // pinia instance with getActivePinia()
  160. t||(i?o(_,null):null))&&v(t),(t=b)._s.has(a)||(
  161. // creating the store registers it in `pinia._s`
  162. r?A(a,e,c,t):function(t,e,n){const{state:o,actions:a,getters:c}=e,r=n.state.value[t];let i;i=A(t,(function(){r||(n.state.value[t]=o?o():{});
  163. // avoid creating a state in pinia.state.value
  164. const e=d(n.state.value[t]);return M(e,a,Object.keys(c||{}).reduce(((e,o)=>(e[o]=s(y((()=>{v(n);
  165. // it was created just before
  166. const e=n._s.get(t);
  167. // @ts-expect-error
  168. // return getters![name].call(context, context)
  169. // TODO: avoid reading the getter while assigning with a global variable
  170. return c[o].call(e,e)}))),e)),{}))}),e,n,0,!0)}(a,c,t));
  171. // StoreGeneric cannot be casted towards Store
  172. return t._s.get(a)}return"string"==typeof t?(a=t,
  173. // the option store setup will contain the actual options in this case
  174. c=r?n:e):(c=t,a=t.id),i.$id=a,i}export{O as c,F as d};
  175. //# sourceMappingURL=pinia-0c8641d4.js.map