2 lines
7.1 KiB
JavaScript
2 lines
7.1 KiB
JavaScript
var e=null,t=new Set,n=!1,r=0,i=()=>{if(n=!1,r++,r>100)throw t.clear(),r=0,Error(`SigPro: Infinite reactive loop detected.`);try{let e=Array.from(t);t.clear();for(let t of e)t.run()}catch(e){console.error(`SigPro Flush Error:`,e)}finally{setTimeout(()=>{r=0},0)}},a=r=>{let a=new Set;if(typeof r==`function`){let n=!0,i,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{i=r()}finally{e=t,n=!1}}};return()=>(e&&(a.add(e),e.dependencies.add(a)),n&&o.run(),i)}return(...o)=>{if(o.length){let e=typeof o[0]==`function`?o[0](r):o[0];Object.is(r,e)||(r=e,a.forEach(e=>{e.markDirty&&e.markDirty(),t.add(e)}),!n&&t.size&&(n=!0,queueMicrotask(i)))}return e&&(a.add(e),e.dependencies.add(a)),r}},o=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()},s=(e,t,n=localStorage)=>{let r;try{let i=n.getItem(e);r=i===null?t:JSON.parse(i)}catch(i){console.warn(`Error reading ${e} from storage:`,i),r=t,n.removeItem(e)}let i=a(r);return o(()=>{try{let t=i();t==null?n.removeItem(e):n.setItem(e,JSON.stringify(t))}catch(t){console.warn(`Error saving ${e} to storage:`,t)}}),i},c=(e,...t)=>{let n=c._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((a,s)=>{if(a&&r.insertBefore(document.createTextNode(a),e),s<n.length-1){let n=t[i++],a=document.createComment(`s`),s=document.createComment(`e`);r.insertBefore(a,e),r.insertBefore(s,e);let c;o(()=>{let e=typeof n==`function`?n():n;if(e===c)return;if(c=e,typeof e!=`object`&&!Array.isArray(e)){let t=a.nextSibling,n=String(e??``);if(t!==s&&t?.nodeType===3)t.textContent=n;else{for(;a.nextSibling!==s;)r.removeChild(a.nextSibling);r.insertBefore(document.createTextNode(n),s)}return}for(;a.nextSibling!==s;)r.removeChild(a.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()},a=n.get(e);if(!a){let t=document.createElement(`template`);t.innerHTML=e.join(`{{part}}`);let r=[],i=document.createTreeWalker(t.content,133),o=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:o(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,a={template:t,dynamicNodes:r})}let s=a.template.content.cloneNode(!0),l=0;return a.dynamicNodes.map(e=>({node:r(s,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,a=i[0];if(a===`@`){let[t,...n]=i.slice(1).split(`.`),a=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,a),r(i)}};e.addEventListener(t,a,{passive:n.includes(`passive`),capture:n.includes(`capture`)}),o.onCleanup&&o.onCleanup(()=>e.removeEventListener(t,a))}else if(a===`:`){let t=i.slice(1),n=e.type===`checkbox`||e.type===`radio`?`change`:`input`;o(()=>{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(a===`?`){let t=i.slice(1);o(()=>{let n=typeof r==`function`?r():r;e.toggleAttribute(t,!!n)})}else if(a===`.`){let t=i.slice(1);o(()=>{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`?o(()=>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}}}),s},l=(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]=a(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=[]}})},u=async(e,t,n)=>{n&&n(!0);try{let n=await(await fetch(e,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)})).text();try{return JSON.parse(n)}catch{return console.warn(`Invalid JSON response`),null}}catch{return null}finally{n&&n(!1)}},d=e=>String(e).replace(/[<>"']/g,``),f=t=>{let n=()=>window.location.hash.replace(/^#/,``)||`/`,r=a(n()),i=document.createElement(`div`);return i.style.display=`contents`,window.addEventListener(`hashchange`,()=>{let e=d(n());r()!==e&&r(e)}),o(()=>{let n=r(),a=null,o={};for(let e of t)if(e.path instanceof RegExp){let t=n.match(e.path);if(t){a=e,o=t.groups||{id:t[1]};break}}else if(e.path===n){a=e;break}let s=e;e=null;try{let e=a?a.component(o):Object.assign(document.createElement(`h1`),{textContent:`404`});i.replaceChildren(e instanceof Node?e:document.createTextNode(e??``))}finally{e=s}}),i};f.go=e=>{let t=e.startsWith(`/`)?e:`/${e}`;window.location.hash!==`#${t}`&&(window.location.hash=t)},a.effect=o,a.component=l,a.fetch=u,a.router=f,a.ws=(e,t={})=>{let{reconnect:n=!0,maxReconnect:r=5,reconnectInterval:i=1e3}=t,s=a(`disconnected`),c=a([]),l=a(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=>{let t=e.data;c([...c(),t])},u.onerror=e=>{l(e),s(`error`)},u.onclose=()=>{s(`disconnected`),n&&d<r&&(f=setTimeout(()=>{d++,p()},i*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(),o?.onCleanup&&o.onCleanup(h),{status:s,messages:c,error:l,send:m,close:h}},a.storage=s;export{c as n,a as t};
|