@qundus/qstate
Version:
your everyday state pal
3 lines • 3.8 kB
JavaScript
import {computed}from'nanostores';var A=Object.defineProperty;var c=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,h=Object.prototype.propertyIsEnumerable;var m=(e,o,t)=>o in e?A(e,o,{enumerable:true,configurable:true,writable:true,value:t}):e[o]=t,p=(e,o)=>{for(var t in o||(o={}))x.call(o,t)&&m(e,t,o[t]);if(c)for(var t of c(o))h.call(o,t)&&m(e,t,o[t]);return e};var f="QSTATE";function l(){return typeof document=="undefined"}var a={setLocal:(e,o,t)=>(e.cookies.set(o,t,{path:"/"}),e.locals[o]=t,{key:o,data:t}),getLocal:(e,o,t)=>{var s;let n=(s=e.cookies.get(o))==null?void 0:s.json();return n==null&&(n=t,e.cookies.set(o,n,{path:"/"})),e.locals[o]=n,{key:o,data:n}},refreshServerContent(){if(l()){console.warn("qstate: you're trying to set state with astroSetAddon on server, aborting!");return}let e=document.createElement("form");e.method="POST",e.action=window.location.pathname,e.style.display="none";let o=document.createElement("button");o.type="submit",e.appendChild(o),document.body.appendChild(e);let t=new MouseEvent("click",{bubbles:true,cancelable:true,view:window});e.addEventListener("",n=>{}),o.dispatchEvent(t);}};var C=(e,o)=>{if(e==null)return null;if(e.name==null)return null;let t=e.name;return {middleware:n=>a.getLocal(n,t,e.value).data,get:n=>typeof n=="undefined"?(console.error("qstate: please pass Astro global object to use store value!"),null):n.locals[t],set:async(n,s)=>{try{let r=s;if(r==null){let{actions:v}=await import('astro:actions');r=v;}let i=null,d=f;if(r==null?i="astro:actions actions object do not exist":r[d]==null?i=d+" is not added to actions, please add qStateAstroActions!":typeof r[d].setLocal!="function"&&(i="setLocal action is undefined, this is fatal!, how did this happen?"),i!=null)return console.error("qstate: ",i),e.get();e.set(n),await r[d].setLocal({key:t,data:n}),a.refreshServerContent();}catch(r){console.error("qstate: an error occured while trying to setLocal "+t+`
`+r);}}}};var b=()=>({get isServerSide(){return l}});var y=(e,o,t)=>{let n=t;if(n==null||n.hooks==null)return;let{hooks:s}=n,r=false;e.hooks={};for(let i in s){let d=s[i];typeof d=="function"&&(r=true,Object.defineProperty(e.hooks,i,{enumerable:true,writable:false,configurable:false,value:d==null?void 0:d(e,n)}));}r||(e.hooks=null);};var S=(e,o,t)=>{let n=t;if(n==null||!n.persistOptions)return;let s=n==null?void 0:n.persistOptions;s&&(s!=="pass-down"&&delete n.persistOptions,e.__options=Object.freeze(Object.assign({},p({},n))));};function u(e){let o=Object.assign({},e!=null?e:{});return function(n){return n==null?e==null?null:o:(n=p(p({},o),n),((o==null?void 0:o.hooks)!=null||(n==null?void 0:n.hooks)!=null)&&(n.hooks=p(p({},o==null?void 0:o.hooks),n==null?void 0:n.hooks)),n)}}function O(e,o,t){if(o==null||typeof o!="function")throw new Error("state: derived function must exist!");let n=computed(e,s=>{var r,i;try{return o(s)}catch(d){return (i=(r=t==null?void 0:t.events)==null?void 0:r.onError)==null||i.call(r,d),null}});return y(n,void 0,t),S(n,void 0,t),n}var Z=(e,o)=>{if(e==null)return null;let t=u(o);return (n,s)=>{let r=t(s);return O(e,n,r)}};var oo=e=>{if(e==null)return null;let o=null,t=e.get();return Array.isArray(t)?o=[...t]:typeof t=="object"?o=Object.assign({},t):o=t,o};function _(){throw new Error("qstate: aborting state update!")}var no=e=>e==null?null:o=>{try{if(typeof o!="function")throw new Error("qstate: update is not of type function!");let t=null,n=e.get();return Array.isArray(n)?t=[...n]:typeof n=="object"?t=Object.assign({},n):t=n,t=o==null?void 0:o({$next:t,prev:n,abort:_}),e.set(t),null}catch(t){return {message:t}}};export{C as astroIntegrationAddon,b as checksAddon,Z as deriveAddon,oo as getNewAddon,no as updateAddon};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map