2 lines
6.7 KiB
JavaScript
2 lines
6.7 KiB
JavaScript
var e=null,t=new Set,n=!1,r=()=>{n=!1;try{for(let e of t)e.run();t.clear()}catch(e){console.error(`SigPro Flush Error:`,e)}},i=i=>{let a=new Set;if(typeof i==`function`){let n=!0,r,o={dependencies:new Set,cleanupHandlers:new Set,markDirty:()=>{n||(n=!0,a.forEach(e=>{e.markDirty&&e.markDirty(),t.add(e)}))},run:()=>{o.dependencies.forEach(e=>e.delete(o)),o.dependencies.clear();let t=e;e=o;try{r=i()}finally{e=t,n=!1}}};return()=>(e&&(a.add(e),e.dependencies.add(a)),n&&o.run(),r)}return(...o)=>{if(o.length){let e=typeof o[0]==`function`?o[0](i):o[0];Object.is(i,e)||(i=e,a.forEach(e=>{e.markDirty&&e.markDirty(),t.add(e)}),!n&&t.size&&(n=!0,queueMicrotask(r)))}return e&&(a.add(e),e.dependencies.add(a)),i}},a=t=>{let n={dependencies:new Set,cleanupHandlers:new Set,run(){this.cleanupHandlers.forEach(e=>e()),this.cleanupHandlers.clear(),this.dependencies.forEach(e=>e.delete(this)),this.dependencies.clear();let n=e;e=this;try{let e=t();typeof e==`function`&&(this.cleanupFunction=e)}finally{e=n}},stop(){this.cleanupHandlers.forEach(e=>e()),this.dependencies.forEach(e=>e.delete(this)),this.cleanupFunction?.()}};return e&&e.cleanupHandlers.add(()=>n.stop()),n.run(),()=>n.stop()},o=(e,t,n=localStorage)=>{let r=n.getItem(e),o=i(r===null?t:JSON.parse(r));return a(()=>{let t=o();t==null?n.removeItem(e):n.setItem(e,JSON.stringify(t))}),o},s=(e,...t)=>{let n=s._templateCache??=new WeakMap,r=(e,t)=>t.reduce((e,t)=>e?.childNodes?.[t],e),i=(e,t)=>{let n=e.textContent.split(`{{part}}`),r=e.parentNode,i=0;n.forEach((o,s)=>{if(o&&r.insertBefore(document.createTextNode(o),e),s<n.length-1){let n=t[i++],o=document.createComment(`s`),s=document.createComment(`e`);r.insertBefore(o,e),r.insertBefore(s,e);let c;a(()=>{let e=typeof n==`function`?n():n;if(e===c)return;if(c=e,typeof e!=`object`&&!Array.isArray(e)){let t=o.nextSibling;if(t!==s&&t?.nodeType===3)t.textContent=e??``;else{for(;o.nextSibling!==s;)r.removeChild(o.nextSibling);r.insertBefore(document.createTextNode(e??``),s)}return}for(;o.nextSibling!==s;)r.removeChild(o.nextSibling);let t=Array.isArray(e)?e:[e],i=document.createDocumentFragment();t.forEach(e=>{if(e==null||e===!1)return;let t=e instanceof Node?e:document.createTextNode(e);i.appendChild(t)}),r.insertBefore(i,s)})}}),e.remove()},o=n.get(e);if(!o){let t=document.createElement(`template`);t.innerHTML=e.join(`{{part}}`);let r=[],i=document.createTreeWalker(t.content,133),a=e=>{let n=[];for(;e&&e!==t.content;){let t=0;for(let n=e.previousSibling;n;n=n.previousSibling)t++;n.push(t),e=e.parentNode}return n.reverse()},s;for(;s=i.nextNode();){let e=!1,t={type:s.nodeType,path:a(s),parts:[]};if(s.nodeType===1)for(let n=0;n<s.attributes.length;n++){let r=s.attributes[n];r.value.includes(`{{part}}`)&&(t.parts.push({name:r.name}),e=!0)}else s.nodeType===3&&s.textContent.includes(`{{part}}`)&&(e=!0);e&&r.push(t)}n.set(e,o={template:t,dynamicNodes:r})}let c=o.template.content.cloneNode(!0),l=0;return o.dynamicNodes.map(e=>({node:r(c,e.path),info:e})).forEach(({node:e,info:n})=>{if(e){if(n.type===1)n.parts.forEach(n=>{let r=t[l++],i=n.name,o=i[0];if(o===`@`){let[t,...n]=i.slice(1).split(`.`),o=i=>{if(n.includes(`prevent`)&&i.preventDefault(),n.includes(`stop`)&&i.stopPropagation(),!(n.includes(`self`)&&i.target!==e)){if(n.some(e=>e.startsWith(`debounce`))){let t=n.find(e=>e.startsWith(`debounce`))?.split(`:`)[1]||300;clearTimeout(e._debounceTimer),e._debounceTimer=setTimeout(()=>r(i),t);return}n.includes(`once`)&&e.removeEventListener(t,o),r(i)}};e.addEventListener(t,o,{passive:n.includes(`passive`),capture:n.includes(`capture`)}),a.onCleanup&&a.onCleanup(()=>e.removeEventListener(t,o))}else if(o===`:`){let t=i.slice(1),n=e.type===`checkbox`||e.type===`radio`?`change`:`input`;a(()=>{let n=typeof r==`function`?r():r;e[t]!==n&&(e[t]=n)}),e.addEventListener(n,()=>{let t=n===`change`?e.checked:e.value;typeof r==`function`&&r(t)})}else if(o===`?`){let t=i.slice(1);a(()=>{let n=typeof r==`function`?r():r;e.toggleAttribute(t,!!n)})}else if(o===`.`){let t=i.slice(1);a(()=>{let n=typeof r==`function`?r():r;e[t]=n,n!=null&&typeof n!=`object`&&typeof n!=`boolean`&&e.setAttribute(t,n)})}else typeof r==`function`?a(()=>e.setAttribute(i,r())):e.setAttribute(i,r)});else if(n.type===3){let n=e.textContent.split(`{{part}}`).length-1;i(e,t.slice(l,l+n)),l+=n}}}),c},c=(e,t,n=[])=>{customElements.get(e)||customElements.define(e,class extends HTMLElement{static get observedAttributes(){return n}constructor(){super(),this._propertySignals={},this.cleanupFunctions=[],n.forEach(e=>this._propertySignals[e]=i(void 0))}connectedCallback(){let e=[...this.childNodes];this.innerHTML=``,n.forEach(e=>{let t=this.hasOwnProperty(e)?this[e]:this.getAttribute(e);Object.defineProperty(this,e,{get:()=>this._propertySignals[e](),set:t=>{let n=t===`false`?!1:t===``&&e!==`value`?!0:t;this._propertySignals[e](n)},configurable:!0}),t!=null&&(this[e]=t)});let r={select:e=>this.querySelector(e),slot:t=>e.filter(e=>{let n=e.nodeType===1?e.getAttribute(`slot`):null;return t?n===t:!n}),emit:(e,t)=>this.dispatchEvent(new CustomEvent(e,{detail:t,bubbles:!0,composed:!0})),host:this,onUnmount:e=>this.cleanupFunctions.push(e)},i=t(this._propertySignals,r);i instanceof Node&&this.appendChild(i)}attributeChangedCallback(e,t,n){this[e]!==n&&(this[e]=n)}disconnectedCallback(){this.cleanupFunctions.forEach(e=>e()),this.cleanupFunctions=[]}})},l=async(e,t,n)=>{n&&n(!0);try{return await(await fetch(e,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)})).json()}catch{return null}finally{n&&n(!1)}},u=t=>{let n=()=>window.location.hash.replace(/^#/,``)||`/`,r=i(n()),o=document.createElement(`div`);return o.style.display=`contents`,window.addEventListener(`hashchange`,()=>{let e=n();r()!==e&&r(e)}),a(()=>{let n=r(),i=null,a={};for(let e of t)if(e.path instanceof RegExp){let t=n.match(e.path);if(t){i=e,a=t.groups||{id:t[1]};break}}else if(e.path===n){i=e;break}let s=e;e=null;try{let e=i?i.component(a):Object.assign(document.createElement(`h1`),{textContent:`404`});o.replaceChildren(e instanceof Node?e:document.createTextNode(e??``))}finally{e=s}}),o};u.go=e=>{let t=e.startsWith(`/`)?e:`/${e}`;window.location.hash!==`#${t}`&&(window.location.hash=t)},i.effect=a,i.component=c,i.fetch=l,i.router=u,i.ws=(e,t={})=>{let{reconnect:n=!0,maxReconnect:r=5,reconnectInterval:o=1e3}=t,s=i(`disconnected`),c=i([]),l=i(null),u=null,d=0,f=null,p=()=>{s(`connecting`),u=new WebSocket(e),u.onopen=()=>{s(`connected`),d=0,l(null)},u.onmessage=e=>{c([...c(),e.data])},u.onerror=e=>{l(e),s(`error`)},u.onclose=()=>{s(`disconnected`),n&&d<r&&(f=setTimeout(()=>{d++,p()},o*2**d))}},m=e=>{u?.readyState===WebSocket.OPEN&&u.send(typeof e==`string`?e:JSON.stringify(e))},h=()=>{f&&clearTimeout(f),u&&=(u.close(),null)};return p(),a?.onCleanup&&a.onCleanup(h),{status:s,messages:c,error:l,send:m,close:h}},i.storage=o;export{s as a,u as i,c as n,a as r,i as t};
|