state/dist/state.min.js

2 lines
16 KiB
JavaScript
Raw Normal View History

!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){"use strict";!function(e){const t={clone:window.structuredClone||(e=>JSON.parse(JSON.stringify(e))),base64:e=>btoa(String.fromCharCode(...(new TextEncoder).encode(e))),unbase64:e=>(new TextDecoder).decode(Uint8Array.from(atob(e),e=>e.charCodeAt(0))),urlbase64:e=>t.base64(e).replace(/[+/=]/g,e=>({"+":"-","/":"","=":""}[e])),unurlbase64:e=>t.unbase64(e.replace(/[-_.]/g,e=>({"-":"+",_:"/",".":"="}[e])).padEnd(4*Math.ceil(e.length/4),"=")),safeJson:e=>{try{return JSON.parse(e)}catch{return null}},updateDefaults:(e,t)=>{for(const n in t)void 0===e[n]&&(e[n]=t[n])},copyFunction:(e,t,...n)=>{n.forEach(n=>e[n]=t[n].bind(t))},getFunctionBody:e=>{const t=e.toString();return t.slice(t.indexOf("{")+1,t.lastIndexOf("}")).trim()},makeDom:e=>{e.includes(">\n")&&(e=e.replace(/>\s+</g,"><").trim());const t=document.createElement("div");return t.innerHTML=e,t.children[0]},newAvg:()=>{let e=0,t=0,n=0;return{add:s=>(e+=s,t++,n=e/t),get:()=>n,clear:()=>{e=0,t=0,n=0}}},newTimeCount:()=>{let e=0,t=0,n=0;return{start:()=>e=(new Date).getTime(),end:()=>{const s=(new Date).getTime(),a=s-e;return e=s,t+=a,n++,a},avg:()=>t/n}}};e.Util=t,e.$=(e,t)=>t?e.querySelector(t):document.querySelector(e),e.$$=(e,t)=>t?e.querySelectorAll(t):document.querySelectorAll(e)}(globalThis),function(e){var t;let n=null,s=null;const a=new Set;function r(e={},t=null,r=null){const i={},o=new Map,c=new Map,d=(e,t)=>(c.has(e)||c.set(e,new Set),t?c.get(e).add(t):c.get(e).clear(),()=>c.get(e).delete(t)),l=(e,t)=>{c.has(e)&&c.set(e,new Set),c.get(e).delete(t)},h=t||(e=>i[e]),u=r||((e,t)=>i[e]=t);return Object.assign(i,e),new Proxy(i,{get:(e,t)=>"__watch"===t?d:"__unwatch"===t?l:"__isProxy"===t||(n&&(o.has(t)||o.set(t,new Set),o.get(t).add(n),n.node._states||(n.node._states=new Set),n.node._states.add(o)),h(t)),set(e,t,n){if(h(t)!==n&&u(t,n),c.has(t)&&c.get(t).forEach(s=>{const a=s(n);void 0!==a&&(n=a,e[t]=n)}),c.has(null)&&c.get(null).forEach(e=>e(n)),o.has(t)){const e=o.get(t);for(const t of e)t.node.isConnected?s!==t.node&&a.forEach(e=>e(t)):e.delete(t)}return!0}})}let i=new URLSearchParams((null==(t=window.location.hash)?void 0:t.substring(1))||"");const o=r({},t=>e.Util.safeJson(i.get(t)),(e,t)=>{const n=i.get(e),s=void 0===t?void 0:JSON.stringify(t);n===s||null===n&&void 0===s||(void 0===t?i.delete(e):i.set(e,s),window.location.hash="#"+i.toString())});"undefined"!=typeof window&&window.addEventListener("hashchange",()=>{var e;const t=new URLSearchParams((null==(e=window.location.hash)?void 0:e.substring(1))||""),n=new Set([...i.keys(),...t.keys()]);i=t,n.forEach(e=>o[e]=o[e])});const c=r({},t=>e.Util.safeJson(localStorage.getItem(t)),(e,t)=>{const n=localStorage.getItem(e),s=void 0===t?void 0:JSON.stringify(t);n===s||null===n&&void 0===s||(void 0===t?localStorage.removeItem(e):localStorage.setItem(e,s))}),d=r({exitBlocks:0});e.NewState=r,e.Hash=o,e.LocalStorage=c,e.State=d,e._onNotifyUpdate=e=>a.add(e),e._setActiveBinding=e=>n=e,e._getActiveBinding=()=>n,e._setNoWriteBack=e=>s=e,e._getNoWriteBack=()=>s,e._reactiveBridge={get activeBinding(){return n},set activeBinding(e){n=e},get noWriteBack(){return s},set noWriteBack(e){s=e},onNotifyUpdate:e._onNotifyUpdate}}(globalThis),function(e){const{Hash:t,LocalStorage:n,State:s,_reactiveBridge:a,$$:r}=e,{onNotifyUpdate:i}=a;let o=!1;const c=new Map;function d(e){o=e}function l(a,r,i,d){e.__DEBUG&&console.log("DEBUG _runCode:",a,"vars:",r,"extendVars:",d);const l=[...Object.keys(d||{}),...Object.keys(r||{})],h=[...Object.values(d||{}),...Object.values(r||{})],u=a+l.join(",");try{let e=c.get(u);return e||(e=new Function("Hash","LocalStorage","State",...l,a),c.set(u,e)),e.apply(i,[t,n,s,...h])}catch(e){return o||console.error(e,d,[a,d,r,i]),null}}function h(e,t,n,s){return e.includes("${")?l("return `"+e+"`",t,n,s):l("return "+e,t,n,s)}const u=new Map,f=[],b={getTemplate:t=>{const n=`template[component="${t.toUpperCase()}"]`,s=document.querySelector(n);return e.__DEBUG&&console.log("DEBUG getTemplate:",t,"selector:",n,"found:",!!s),s},register:(t,n,s=null