atmx-web 0.60.0 ā 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/atmx.es.js +1 -1
- package/dist/atmx.umd.js +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
package/dist/atmx.es.js
CHANGED
|
@@ -439,7 +439,7 @@ var P = class extends HTMLElement {
|
|
|
439
439
|
typeof window < "u" && !customElements.get("ax-engine-status") && customElements.define("ax-engine-status", P);
|
|
440
440
|
//#endregion
|
|
441
441
|
//#region src/index.ts
|
|
442
|
-
var F = "0.
|
|
442
|
+
var F = "0.62.0", I = new class {
|
|
443
443
|
version = F;
|
|
444
444
|
initialized = !1;
|
|
445
445
|
debug = !1;
|
package/dist/atmx.umd.js
CHANGED
|
@@ -884,4 +884,4 @@
|
|
|
884
884
|
return Object.assign(__wbg_init, { initSync }, exports);
|
|
885
885
|
})({ __proto__: null });
|
|
886
886
|
`})),g=r({allocBytes:()=>_,allocString:()=>v,clearAuthToken:()=>S,initWasm:()=>b,sendStreamMessage:()=>C,setAuthToken:()=>x,wasmEngine:()=>w});function _(e){if(e.length===0)return 0;let t=w.axiom_malloc(e.length);return new Uint8Array(w.memory.buffer).set(e,t),t}function v(e){if(!e)return{ptr:0,len:0};let t=new TextEncoder().encode(e);return{ptr:_(t),len:t.length}}function y(){return`https://atmx.axiomcore.dev/latest/axiom_runtime.wasm`}async function b(e){let t=performance.now(),n={ok:!1,contracts:{},initDurationMs:0};try{if(!window.wasm_bindgen){let e=document.createElement(`script`);e.type=`text/javascript`,e.text=`${m}\nwindow.wasm_bindgen = wasm_bindgen;`,document.head.appendChild(e)}let t=window.wasm_bindgen,r=`${y()}?t=${Date.now()}`;try{w=await t(fetch(r))}catch(e){throw{stage:`ffiBoundary`,category:`network`,code:`WasmFetchFailed`,message:`Failed to download WASM engine: ${e.message}`,retryable:!0}}o(w),setInterval(()=>{w&&w.axiom_process_responses()},16);let i=v(``),a=w.axiom_wasm_initialize(i.ptr,i.len);if(w.axiom_free_memory(i.ptr,i.len),a!==0)throw{stage:`runtime`,category:`server`,code:`InitFailed`,message:`Engine init failed (Code: ${a})`,retryable:!1};for(let[t,r]of Object.entries(e.contracts))try{let i=await fetch(r.contractUrl);if(!i.ok)throw Error(`HTTP ${i.status}`);let a=await i.arrayBuffer(),o=0;try{let e=new TextDecoder().decode(a),n=JSON.parse(e),r=n?.ir?.endpoints;r&&(o=Array.isArray(r)?r.length:Object.keys(r).length),l(t,n)}catch(e){console.error(`ATMX: Failed to parse .axiom contract for '${t}'`,e)}let s=new Uint8Array(a),c=_(s),u=v(t),d=v(r.baseUrl),f=w.axiom_wasm_load_contract(u.ptr,u.len,d.ptr,d.len,c,s.length,0,0,0,0);if(w.axiom_free_memory(c,s.length),w.axiom_free_memory(u.ptr,u.len),w.axiom_free_memory(d.ptr,d.len),f!==0&&f!==-1)throw Error(`Load error (Code: ${f})`);e.debug&&console.log(`%cš Axiom Contract Loaded: ${t} (${o} endpoints)`,`color: #10b981; font-weight: bold;`),n.contracts[t]={loaded:!0,endpointCount:o},document.dispatchEvent(new CustomEvent(`atmx:contract-loaded`,{detail:{namespace:t,endpointCount:o}}))}catch(e){n.contracts[t]={loaded:!1,endpointCount:0,error:e.message},console.error(`ATMX: Contract failed for ${t}`,e)}n.ok=!0}catch(e){n.ok=!1,n.error=e.stage?e:{stage:`runtime`,category:`unknown`,code:`FatalError`,message:e.message||String(e),retryable:!1}}return n.initDurationMs=Math.round(performance.now()-t),n}function x(e,t,n){if(!w)return;let r=v(e),i=v(t),a=v(n);w.axiom_wasm_set_auth_token(r.ptr,r.len,i.ptr,i.len,a.ptr,a.len),w.axiom_free_memory(r.ptr,r.len),w.axiom_free_memory(i.ptr,i.len),w.axiom_free_memory(a.ptr,a.len)}function S(e,t){if(!w)return;let n=v(e),r=v(t);w.axiom_wasm_clear_auth_token(n.ptr,n.len,r.ptr,r.len),w.axiom_free_memory(n.ptr,n.len),w.axiom_free_memory(r.ptr,r.len)}function C(e,t){if(!w)return;let n=new TextEncoder().encode(JSON.stringify(t)),r=_(n);w.axiom_wasm_send_stream_message(e,r,n.length),w.axiom_free_memory(r,n.length)}var w,T=n((()=>{c(),p(),h(),w=null}));T();function E(e,t,n={}){if(e)try{let r=Object.keys(n),i=Object.values(n);return Function(`$data`,`$error`,`$state`,...r,`return (${e});`)(t.data||{},t.error||{},t,...i)}catch(t){console.error(`ATMX Eval Error: ${e}`,t);return}}function D(e,t,n={}){if(e)try{let r=Object.keys(n),i=Object.values(n);Function(`$data`,`$error`,`$state`,...r,e)(t.data||{},t.error||{},t,...i)}catch(t){console.error(`ATMX Action Error: ${e}`,t)}}function O(e,t){k(e,t,!0,{})}function k(e,t,n,r){if(!(!n&&(e.hasAttribute(`ax-query`)||e.hasAttribute(`ax-mutate`)))){if(e.hasAttribute(`ax-for`)){A(e,t,r);return}if(e.hasAttribute(`ax-when`)&&(e.getAttribute(`ax-when`).split(`,`).map(e=>e.trim()).includes(t.state)?(e.removeAttribute(`hidden`),e.style.removeProperty(`display`)):(e.setAttribute(`hidden`,`true`),e.style.setProperty(`display`,`none`,`important`))),e.hasAttribute(`ax-if`)&&(E(e.getAttribute(`ax-if`),t,r)?(e.removeAttribute(`hidden`),e.style.removeProperty(`display`)):(e.setAttribute(`hidden`,``),e.style.setProperty(`display`,`none`,`important`))),e.hasAttribute(`ax-text`)){let n=E(e.getAttribute(`ax-text`),t,r);e.textContent=n==null?``:String(n)}if(e.hasAttribute(`ax-error-for`)){let n=e.getAttribute(`ax-error-for`),r=``;if(t.state===`error`&&t.error?.code===`ValidationError`){let e=(t.error.details||``).split(`
|
|
887
|
-
`);for(let t of e)if(t.startsWith(n+`:`)){r=t.substring(n.length+1).trim();break}}r?(e.textContent=r,e.removeAttribute(`hidden`),e.style.removeProperty(`display`)):(e.setAttribute(`hidden`,``),e.style.setProperty(`display`,`none`,`important`))}Array.from(e.attributes).forEach(n=>{if(n.name.startsWith(`ax-bind:`)){let i=n.name.substring(8),a=E(n.value,t,r);a===!1||a==null?e.removeAttribute(i):e.setAttribute(i,a===!0?i:String(a))}}),Array.from(e.children).forEach(e=>{k(e,t,!1,r)})}}function A(e,t,n){e.style.display=`none`,e.hasAttribute(`data-ax-for-id`)||e.setAttribute(`data-ax-for-id`,Math.random().toString(36).substr(2,9));let r=e.getAttribute(`data-ax-for-id`),i=e.getAttribute(`ax-for`).match(/^\s*(?:(?:\(\s*([\w\$]+)\s*,\s*([\w\$]+)\s*\))|([\w\$]+))\s+in\s+(.+)$/);if(!i)return;let a=i[1]||i[3],o=i[2],s=i[4],c=E(s,t,n),l=e.parentElement;if(!l)return;let u=Array.from(l.querySelectorAll(`[data-ax-clone-of="${r}"]`));if(!c||!Array.isArray(c)){u.forEach(e=>e.remove());return}let d=e.getAttribute(`ax-key`),f=[];c.forEach((i,s)=>{let c={...n,[a]:i};o&&(c[o]=s);let p=String(s);d&&(p=String(E(d,t,c)));let m=u.find(e=>e.getAttribute(`data-ax-key`)===p);m||(m=e.cloneNode(!0),m.removeAttribute(`ax-for`),m.style.removeProperty(`display`),m.style.length===0&&m.removeAttribute(`style`),m.setAttribute(`data-ax-clone-of`,r),m.setAttribute(`data-ax-key`,p)),l.insertBefore(m,e),k(m,t,!1,c),f.push(m)}),u.forEach(e=>{f.includes(e)||e.remove()})}var j=new WeakMap;function M(e){return j.has(e)||j.set(e,{state:`idle`,isFetching:!1,isMutating:!1,source:null,data:null,error:null}),j.get(e)}function N(e){let t=e.parentElement;for(;t;){if(t.hasAttribute(`ax-query`)||t.hasAttribute(`ax-mutate`)){let e=j.get(t);if(e&&e.data)return e.data}t=t.parentElement}return{}}function P(e,t){let n=M(e);Object.assign(n,t),t.state===`loading`&&(n.isFetching=!0),t.state===`mutating`&&(n.isMutating=!0),(t.state===`success`||t.state===`data`&&t.isFetching===!1)&&(n.isFetching=!1,n.isMutating=!1),j.set(e,n),O(e,n)}c(),p(),a();var F=0,I=class{key;route;args;state;listeners=new Set;currentReqId=null;constructor(e,t,n){this.key=e,this.route=t,this.args=n,this.state={state:`idle`,isFetching:!1,source:null,data:null,error:null}}subscribe(e){this.listeners.add(e),e(this.state)}update(e){this.state={...this.state,...e},this.listeners.forEach(e=>e(this.state))}fetch(e=!1){if(this.state.isFetching&&!e)return;this.update({state:this.state.data?`data`:`loading`,isFetching:!0,error:null});let t=++F;this.currentReqId=t;let{path:n,remainingArgs:r}=d(this.route,this.args),a=new Uint8Array;Object.keys(r).length>0&&(a=new TextEncoder().encode(JSON.stringify(r))),Q.debug&&(console.groupCollapsed(`%cā WASM QUERY [#${t}]`,`color: #7c3aed; font-weight: bold;`),console.log(`Namespace:`,this.route.namespace),console.log(`Endpoint ID:`,this.route.id),console.log(`Path:`,n),console.groupEnd()),s.set(t,{isStream:this.route.isStream,onResponse:e=>{if(this.currentReqId===t){if(Q.debug){let n=e.eventType===1?`NetworkSuccess`:e.eventType===2?`CacheHit`:e.eventType===3?`CacheHitAndFetching`:e.eventType===4?`Error`:`Complete`;console.groupCollapsed(`%cā WASM RESP [#${t}]`,`color: #059669; font-weight: bold;`),console.log(`Event Type:`,n),e.data&&console.log(`Data:`,e.data),e.error&&console.log(`Error:`,e.error),console.groupEnd()}if(e.eventType===i.Error){this.update({state:`error`,error:e.error,isFetching:!1});return}if(e.data){let t=`network`,n=!1;e.eventType===i.CacheHit?(t=`cache`,n=!1):e.eventType===i.CacheHitAndFetching?(t=`cache`,n=!0):e.eventType===i.NetworkSuccess&&(t=`network`,n=!1),this.update({state:`data`,data:e.data,source:t,isFetching:n})}}},onComplete:()=>{this.currentReqId===t&&(Q.debug&&console.log(`%cā Stream Complete [#${t}]`,`color: #94a3b8;`),this.update({isFetching:!1}))}});let o=v(this.route.namespace),c=v(this.route.method),l=v(n),u=_(a),f=v(``),p=v(``);w.axiom_wasm_call(t,o.ptr,o.len,this.route.id,c.ptr,c.len,l.ptr,l.len,f.ptr,f.len,p.ptr,p.len,u,a.length),w.axiom_free_memory(o.ptr,o.len),w.axiom_free_memory(c.ptr,c.len),w.axiom_free_memory(l.ptr,l.len),w.axiom_free_memory(f.ptr,f.len),w.axiom_free_memory(p.ptr,p.len)}},L=new Map;T(),c(),p(),a();var R=0;function z(e,t){let n=`ax-on:${t}`;e.hasAttribute(n)&&D(e.getAttribute(n),M(e))}function B(e,t,n){if(!Q.debug)return;let r=e===`OUT`?`#7c3aed`:`#059669`;console.groupCollapsed(`%c${e===`OUT`?`ā WASM CALL`:`ā WASM RESP`} [#${t}]`,`color: ${r}; font-weight: bold;`),console.log(`Details:`,n),console.groupEnd()}function V(e,t){let n=t.trim().match(/^([\w\.]+)(?:\((.*)\))?$/);if(!n)return null;let r=n[1],i={};if(n[2]&&n[2].trim()!==``)try{let t=M(e),r=t.data||N(e);i=Function(`$data`,`$state`,`return (${n[2]});`)(r,t)||{}}catch(e){console.error(`ATMX Scope Error in "${t}":`,e)}return{target:r,args:i}}function H(e){let t=e.getAttribute(`ax-query`);if(!t)return;let n=V(e,t);if(!n)return;let r=u(n.target);if(!r){P(e,{state:`error`,error:{message:`Route not found: ${n.target}`}});return}let i=`${r.namespace}:${r.id}:${JSON.stringify(n.args)}`,a=L.get(i);a||(a=new I(i,r,n.args),L.set(i,a)),e._axListener||(e._axListener=t=>{P(e,t),(t.state===`data`||t.state===`error`)&&(z(e,t.state),e.dispatchEvent(new CustomEvent(`atmx:${t.state}`,{bubbles:!0,detail:t.data||t.error})))},a.subscribe(e._axListener)),a.fetch()}function U(e){let t=e.getAttribute(`ax-mutate`);if(!t)return;let n=V(e,t);if(!n)return;let r=u(n.target);if(!r){P(e,{state:`error`,error:{message:`Route not found: ${n.target}`}}),z(e,`error`);return}P(e,{state:`mutating`,error:null,data:null}),z(e,`mutating`);let i={};e instanceof HTMLFormElement&&new FormData(e).forEach((t,n)=>{let r=e.elements.namedItem(n),a=!1;r instanceof HTMLInputElement?a=r.type===`number`:r instanceof RadioNodeList&&r.length>0&&(a=r[0].type===`number`);let o=a&&t!==``?Number(t):t;if(n.includes(`.`)){let e=n.split(`.`),t=i;for(let n=0;n<e.length-1;n++)t[e[n]]||(t[e[n]]={}),t=t[e[n]];t[e[e.length-1]]=o}else i[n]=o});let{path:a,remainingArgs:o}=d(r,{...i,...n.args}),s=o;r.bodyParam&&o[r.bodyParam]!==void 0&&(s=o[r.bodyParam]);let c={},l=e.getAttribute(`ax-headers`);if(l)try{c=Function(`return (`+l+`);`)()||{}}catch(e){console.error(`ATMX: Invalid ax-headers syntax`,e)}let f=Object.entries(c).some(([e,t])=>e.toLowerCase()===`content-type`&&t.includes(`application/x-www-form-urlencoded`)),p=new Uint8Array;if(Object.keys(s).length>0)if(f){let e=new URLSearchParams;for(let[t,n]of Object.entries(s))e.append(t,String(n));p=new TextEncoder().encode(e.toString())}else p=new TextEncoder().encode(JSON.stringify(s));W(e,r.namespace,r.id,r.method,a,p,r.isStream,!0,c,f)}function W(e,t,n,r,i,a,o,c,l={},u=!1){let d=++R;B(`OUT`,d,{namespace:t,endpointId:n,method:r,path:i,headers:l,payload:a.length>0?u?new TextDecoder().decode(a):JSON.parse(new TextDecoder().decode(a)):null}),s.set(d,{isStream:o,onResponse:t=>G(e,t,c,d),onComplete:()=>{Q.debug&&console.log(`%cā Stream Complete [#${d}]`,`color: #94a3b8;`)}});let f=v(t),p=v(r),m=v(i),h=v(``),g=v(Object.keys(l).length>0?JSON.stringify(l):``),y=_(a);w.axiom_wasm_call(d,f.ptr,f.len,n,p.ptr,p.len,m.ptr,m.len,h.ptr,h.len,g.ptr,g.len,y,a.length),w.axiom_free_memory(f.ptr,f.len),w.axiom_free_memory(p.ptr,p.len),w.axiom_free_memory(m.ptr,m.len),w.axiom_free_memory(h.ptr,h.len),w.axiom_free_memory(g.ptr,g.len),y&&w.axiom_free_memory(y,a.length)}function G(e,t,n,r){if(B(`IN`,r,{eventType:i[t.eventType],data:t.data,error:t.error}),t.eventType===i.Error){P(e,{state:`error`,error:t.error,isFetching:!1,isMutating:!1}),z(e,`error`),e.dispatchEvent(new CustomEvent(`atmx:error`,{bubbles:!0,detail:t.error}));return}if(t.data){let r=`network`,a=!1;t.eventType===i.CacheHit?r=`cache`:t.eventType===i.CacheHitAndFetching?(r=`cache`,a=!0):t.eventType===i.NetworkSuccess&&(r=`network`);let o=n?`success`:`data`;if(P(e,{state:o,data:t.data,source:r,isFetching:a,isMutating:!1}),n){let t=e.getAttribute(`ax-store-token`);if(t){let n=E(t,M(e));if(n){let t=u(V(e,e.getAttribute(`ax-mutate`)).target),r=e.getAttribute(`ax-token-header`)||`Authorization`;Q.setAuthToken(t?.namespace||`default`,r,n)}}if(e.hasAttribute(`ax-clear-token`)){let t=u(V(e,e.getAttribute(`ax-mutate`)).target),n=e.getAttribute(`ax-token-header`)||`Authorization`;Q.clearAuthToken(t?.namespace||`default`,n)}let n=e.getAttribute(`ax-refresh`);if(n){let e=n.split(`,`).map(e=>e.trim());for(let[t,n]of L.entries()){let t=`${n.route.namespace}.${n.route.name}`;e.includes(t)&&n.fetch(!0)}}}z(e,o),e.dispatchEvent(new CustomEvent(n?`atmx:success`:`atmx:data`,{bubbles:!0,detail:t.data}))}}p();var K=new IntersectionObserver(e=>{e.forEach(e=>{if(e.isIntersecting){let t=e.target;t.hasAttribute(`ax-query`)&&H(t),K.unobserve(t)}})});function q(e){e.querySelectorAll(`[\\:ax-query], [\\:ax-mutate]`).forEach(e=>{if(e.hasAttribute(`:ax-query`)){let t=E(e.getAttribute(`:ax-query`),M(e));t&&e.setAttribute(`ax-query`,String(t)),e.removeAttribute(`:ax-query`)}if(e.hasAttribute(`:ax-mutate`)){let t=E(e.getAttribute(`:ax-mutate`),M(e));t&&e.setAttribute(`ax-mutate`,String(t)),e.removeAttribute(`:ax-mutate`)}})}function J(e=document){q(e),e.querySelectorAll(`[ax-query]`).forEach(e=>{let t=e;P(t,{});let n=t.getAttribute(`ax-trigger`)||`load`;n===`load`?H(t):n===`intersect`?K.observe(t):t.addEventListener(n,e=>{e.preventDefault(),H(t)})}),e.querySelectorAll(`[ax-mutate]`).forEach(e=>{let t=e;P(t,{});let n=t.tagName===`FORM`?`submit`:`click`;t.addEventListener(n,e=>{(t.tagName===`A`||t.tagName===`BUTTON`||t.tagName===`FORM`)&&e.preventDefault(),U(t)})}),e.querySelectorAll(`[ax-trigger="refresh"]`).forEach(e=>{let t=e;t.addEventListener(`click`,e=>{e.preventDefault();let n=t.closest(`[ax-query]`);if(n){let e=V(n,n.getAttribute(`ax-query`));if(e){let t=u(e.target);if(t){let r=`${t.namespace}:${t.id}:${JSON.stringify(e.args)}`,i=L.get(r);i?i.fetch(!0):H(n)}}}})})}function Y(){new MutationObserver(e=>{e.forEach(e=>{e.addedNodes.forEach(e=>{e instanceof HTMLElement&&J(e)})})}).observe(document.body,{childList:!0,subtree:!0})}var X=class extends HTMLElement{constructor(){super()}connectedCallback(){let e=this.getAttribute(`loading-text`)||`Initializing...`,t=this.getAttribute(`ready-text`)||`Online`,n=this.getAttribute(`error-text`)||`Failed to Connect`;this.renderState(`loading`,e,`text-amber-500 animate-pulse`),document.addEventListener(`atmx:ready`,()=>{this.renderState(`ready`,t,`text-green-600`)}),document.addEventListener(`atmx:error`,e=>{let t=e;t.detail&&t.detail.stage===`runtime`&&this.renderState(`error`,`${n}: ${t.detail.message}`,`text-red-600`)})}renderState(e,t,n){this.setAttribute(`data-state`,e);let r=this.querySelectorAll(`[data-ax-when]`);r.length>0?r.forEach(t=>{t.getAttribute(`data-ax-when`)===e?t.removeAttribute(`hidden`):t.setAttribute(`hidden`,``)}):(this.innerText=t,this.getAttribute(`class`)||(this.className=`text-xs font-bold ${n}`))}};typeof window<`u`&&!customElements.get(`ax-engine-status`)&&customElements.define(`ax-engine-status`,X),T(),p(),c();var Z=`0.60.0`,Q=new class{version=Z;initialized=!1;debug=!1;async init(e){if(this.initialized)return{ok:!0,contracts:{},initDurationMs:0};let t=await b(e);return t.ok?(this.debug=e.debug||!1,Y(),J(document.body),this.initialized=!0,this.debug&&console.log(`%cš ATMX Debug Mode: Enabled`,`color: #2563eb; font-weight: bold;`),console.log(`ā
ATMX v${this.version} Online (${t.initDurationMs}ms). Reactive Engine Started.`),document.dispatchEvent(new CustomEvent(`atmx:ready`,{detail:t}))):(console.error(`ā ATMX Initialization Failed:`,t.error),document.dispatchEvent(new CustomEvent(`atmx:error`,{detail:t.error}))),t}triggerQuery(e){let t=typeof e==`string`?document.querySelector(e):e;t&&t instanceof HTMLElement&&t.hasAttribute(`ax-query`)&&H(t)}on(e,t){document.addEventListener(`atmx:${e}`,t)}setAuthToken(e,t,n){x(e,t,n)}sendStreamMessage(e,t){Promise.resolve().then(()=>(T(),g)).then(n=>n.sendStreamMessage(e,t))}clearAuthToken(e,t){S(e,t)}};typeof window<`u`&&(window.atmx=Q),e.ATMX_VERSION=Z,e.allocBytes=_,e.allocString=v,e.atmx=Q,e.buildRequestPath=d,e.clearAuthToken=S,e.initWasm=b,e.pendingRequests=s,e.resolveRoute=u,e.setAuthToken=x,Object.defineProperty(e,`wasmEngine`,{enumerable:!0,get:function(){return w}})});
|
|
887
|
+
`);for(let t of e)if(t.startsWith(n+`:`)){r=t.substring(n.length+1).trim();break}}r?(e.textContent=r,e.removeAttribute(`hidden`),e.style.removeProperty(`display`)):(e.setAttribute(`hidden`,``),e.style.setProperty(`display`,`none`,`important`))}Array.from(e.attributes).forEach(n=>{if(n.name.startsWith(`ax-bind:`)){let i=n.name.substring(8),a=E(n.value,t,r);a===!1||a==null?e.removeAttribute(i):e.setAttribute(i,a===!0?i:String(a))}}),Array.from(e.children).forEach(e=>{k(e,t,!1,r)})}}function A(e,t,n){e.style.display=`none`,e.hasAttribute(`data-ax-for-id`)||e.setAttribute(`data-ax-for-id`,Math.random().toString(36).substr(2,9));let r=e.getAttribute(`data-ax-for-id`),i=e.getAttribute(`ax-for`).match(/^\s*(?:(?:\(\s*([\w\$]+)\s*,\s*([\w\$]+)\s*\))|([\w\$]+))\s+in\s+(.+)$/);if(!i)return;let a=i[1]||i[3],o=i[2],s=i[4],c=E(s,t,n),l=e.parentElement;if(!l)return;let u=Array.from(l.querySelectorAll(`[data-ax-clone-of="${r}"]`));if(!c||!Array.isArray(c)){u.forEach(e=>e.remove());return}let d=e.getAttribute(`ax-key`),f=[];c.forEach((i,s)=>{let c={...n,[a]:i};o&&(c[o]=s);let p=String(s);d&&(p=String(E(d,t,c)));let m=u.find(e=>e.getAttribute(`data-ax-key`)===p);m||(m=e.cloneNode(!0),m.removeAttribute(`ax-for`),m.style.removeProperty(`display`),m.style.length===0&&m.removeAttribute(`style`),m.setAttribute(`data-ax-clone-of`,r),m.setAttribute(`data-ax-key`,p)),l.insertBefore(m,e),k(m,t,!1,c),f.push(m)}),u.forEach(e=>{f.includes(e)||e.remove()})}var j=new WeakMap;function M(e){return j.has(e)||j.set(e,{state:`idle`,isFetching:!1,isMutating:!1,source:null,data:null,error:null}),j.get(e)}function N(e){let t=e.parentElement;for(;t;){if(t.hasAttribute(`ax-query`)||t.hasAttribute(`ax-mutate`)){let e=j.get(t);if(e&&e.data)return e.data}t=t.parentElement}return{}}function P(e,t){let n=M(e);Object.assign(n,t),t.state===`loading`&&(n.isFetching=!0),t.state===`mutating`&&(n.isMutating=!0),(t.state===`success`||t.state===`data`&&t.isFetching===!1)&&(n.isFetching=!1,n.isMutating=!1),j.set(e,n),O(e,n)}c(),p(),a();var F=0,I=class{key;route;args;state;listeners=new Set;currentReqId=null;constructor(e,t,n){this.key=e,this.route=t,this.args=n,this.state={state:`idle`,isFetching:!1,source:null,data:null,error:null}}subscribe(e){this.listeners.add(e),e(this.state)}update(e){this.state={...this.state,...e},this.listeners.forEach(e=>e(this.state))}fetch(e=!1){if(this.state.isFetching&&!e)return;this.update({state:this.state.data?`data`:`loading`,isFetching:!0,error:null});let t=++F;this.currentReqId=t;let{path:n,remainingArgs:r}=d(this.route,this.args),a=new Uint8Array;Object.keys(r).length>0&&(a=new TextEncoder().encode(JSON.stringify(r))),Q.debug&&(console.groupCollapsed(`%cā WASM QUERY [#${t}]`,`color: #7c3aed; font-weight: bold;`),console.log(`Namespace:`,this.route.namespace),console.log(`Endpoint ID:`,this.route.id),console.log(`Path:`,n),console.groupEnd()),s.set(t,{isStream:this.route.isStream,onResponse:e=>{if(this.currentReqId===t){if(Q.debug){let n=e.eventType===1?`NetworkSuccess`:e.eventType===2?`CacheHit`:e.eventType===3?`CacheHitAndFetching`:e.eventType===4?`Error`:`Complete`;console.groupCollapsed(`%cā WASM RESP [#${t}]`,`color: #059669; font-weight: bold;`),console.log(`Event Type:`,n),e.data&&console.log(`Data:`,e.data),e.error&&console.log(`Error:`,e.error),console.groupEnd()}if(e.eventType===i.Error){this.update({state:`error`,error:e.error,isFetching:!1});return}if(e.data){let t=`network`,n=!1;e.eventType===i.CacheHit?(t=`cache`,n=!1):e.eventType===i.CacheHitAndFetching?(t=`cache`,n=!0):e.eventType===i.NetworkSuccess&&(t=`network`,n=!1),this.update({state:`data`,data:e.data,source:t,isFetching:n})}}},onComplete:()=>{this.currentReqId===t&&(Q.debug&&console.log(`%cā Stream Complete [#${t}]`,`color: #94a3b8;`),this.update({isFetching:!1}))}});let o=v(this.route.namespace),c=v(this.route.method),l=v(n),u=_(a),f=v(``),p=v(``);w.axiom_wasm_call(t,o.ptr,o.len,this.route.id,c.ptr,c.len,l.ptr,l.len,f.ptr,f.len,p.ptr,p.len,u,a.length),w.axiom_free_memory(o.ptr,o.len),w.axiom_free_memory(c.ptr,c.len),w.axiom_free_memory(l.ptr,l.len),w.axiom_free_memory(f.ptr,f.len),w.axiom_free_memory(p.ptr,p.len)}},L=new Map;T(),c(),p(),a();var R=0;function z(e,t){let n=`ax-on:${t}`;e.hasAttribute(n)&&D(e.getAttribute(n),M(e))}function B(e,t,n){if(!Q.debug)return;let r=e===`OUT`?`#7c3aed`:`#059669`;console.groupCollapsed(`%c${e===`OUT`?`ā WASM CALL`:`ā WASM RESP`} [#${t}]`,`color: ${r}; font-weight: bold;`),console.log(`Details:`,n),console.groupEnd()}function V(e,t){let n=t.trim().match(/^([\w\.]+)(?:\((.*)\))?$/);if(!n)return null;let r=n[1],i={};if(n[2]&&n[2].trim()!==``)try{let t=M(e),r=t.data||N(e);i=Function(`$data`,`$state`,`return (${n[2]});`)(r,t)||{}}catch(e){console.error(`ATMX Scope Error in "${t}":`,e)}return{target:r,args:i}}function H(e){let t=e.getAttribute(`ax-query`);if(!t)return;let n=V(e,t);if(!n)return;let r=u(n.target);if(!r){P(e,{state:`error`,error:{message:`Route not found: ${n.target}`}});return}let i=`${r.namespace}:${r.id}:${JSON.stringify(n.args)}`,a=L.get(i);a||(a=new I(i,r,n.args),L.set(i,a)),e._axListener||(e._axListener=t=>{P(e,t),(t.state===`data`||t.state===`error`)&&(z(e,t.state),e.dispatchEvent(new CustomEvent(`atmx:${t.state}`,{bubbles:!0,detail:t.data||t.error})))},a.subscribe(e._axListener)),a.fetch()}function U(e){let t=e.getAttribute(`ax-mutate`);if(!t)return;let n=V(e,t);if(!n)return;let r=u(n.target);if(!r){P(e,{state:`error`,error:{message:`Route not found: ${n.target}`}}),z(e,`error`);return}P(e,{state:`mutating`,error:null,data:null}),z(e,`mutating`);let i={};e instanceof HTMLFormElement&&new FormData(e).forEach((t,n)=>{let r=e.elements.namedItem(n),a=!1;r instanceof HTMLInputElement?a=r.type===`number`:r instanceof RadioNodeList&&r.length>0&&(a=r[0].type===`number`);let o=a&&t!==``?Number(t):t;if(n.includes(`.`)){let e=n.split(`.`),t=i;for(let n=0;n<e.length-1;n++)t[e[n]]||(t[e[n]]={}),t=t[e[n]];t[e[e.length-1]]=o}else i[n]=o});let{path:a,remainingArgs:o}=d(r,{...i,...n.args}),s=o;r.bodyParam&&o[r.bodyParam]!==void 0&&(s=o[r.bodyParam]);let c={},l=e.getAttribute(`ax-headers`);if(l)try{c=Function(`return (`+l+`);`)()||{}}catch(e){console.error(`ATMX: Invalid ax-headers syntax`,e)}let f=Object.entries(c).some(([e,t])=>e.toLowerCase()===`content-type`&&t.includes(`application/x-www-form-urlencoded`)),p=new Uint8Array;if(Object.keys(s).length>0)if(f){let e=new URLSearchParams;for(let[t,n]of Object.entries(s))e.append(t,String(n));p=new TextEncoder().encode(e.toString())}else p=new TextEncoder().encode(JSON.stringify(s));W(e,r.namespace,r.id,r.method,a,p,r.isStream,!0,c,f)}function W(e,t,n,r,i,a,o,c,l={},u=!1){let d=++R;B(`OUT`,d,{namespace:t,endpointId:n,method:r,path:i,headers:l,payload:a.length>0?u?new TextDecoder().decode(a):JSON.parse(new TextDecoder().decode(a)):null}),s.set(d,{isStream:o,onResponse:t=>G(e,t,c,d),onComplete:()=>{Q.debug&&console.log(`%cā Stream Complete [#${d}]`,`color: #94a3b8;`)}});let f=v(t),p=v(r),m=v(i),h=v(``),g=v(Object.keys(l).length>0?JSON.stringify(l):``),y=_(a);w.axiom_wasm_call(d,f.ptr,f.len,n,p.ptr,p.len,m.ptr,m.len,h.ptr,h.len,g.ptr,g.len,y,a.length),w.axiom_free_memory(f.ptr,f.len),w.axiom_free_memory(p.ptr,p.len),w.axiom_free_memory(m.ptr,m.len),w.axiom_free_memory(h.ptr,h.len),w.axiom_free_memory(g.ptr,g.len),y&&w.axiom_free_memory(y,a.length)}function G(e,t,n,r){if(B(`IN`,r,{eventType:i[t.eventType],data:t.data,error:t.error}),t.eventType===i.Error){P(e,{state:`error`,error:t.error,isFetching:!1,isMutating:!1}),z(e,`error`),e.dispatchEvent(new CustomEvent(`atmx:error`,{bubbles:!0,detail:t.error}));return}if(t.data){let r=`network`,a=!1;t.eventType===i.CacheHit?r=`cache`:t.eventType===i.CacheHitAndFetching?(r=`cache`,a=!0):t.eventType===i.NetworkSuccess&&(r=`network`);let o=n?`success`:`data`;if(P(e,{state:o,data:t.data,source:r,isFetching:a,isMutating:!1}),n){let t=e.getAttribute(`ax-store-token`);if(t){let n=E(t,M(e));if(n){let t=u(V(e,e.getAttribute(`ax-mutate`)).target),r=e.getAttribute(`ax-token-header`)||`Authorization`;Q.setAuthToken(t?.namespace||`default`,r,n)}}if(e.hasAttribute(`ax-clear-token`)){let t=u(V(e,e.getAttribute(`ax-mutate`)).target),n=e.getAttribute(`ax-token-header`)||`Authorization`;Q.clearAuthToken(t?.namespace||`default`,n)}let n=e.getAttribute(`ax-refresh`);if(n){let e=n.split(`,`).map(e=>e.trim());for(let[t,n]of L.entries()){let t=`${n.route.namespace}.${n.route.name}`;e.includes(t)&&n.fetch(!0)}}}z(e,o),e.dispatchEvent(new CustomEvent(n?`atmx:success`:`atmx:data`,{bubbles:!0,detail:t.data}))}}p();var K=new IntersectionObserver(e=>{e.forEach(e=>{if(e.isIntersecting){let t=e.target;t.hasAttribute(`ax-query`)&&H(t),K.unobserve(t)}})});function q(e){e.querySelectorAll(`[\\:ax-query], [\\:ax-mutate]`).forEach(e=>{if(e.hasAttribute(`:ax-query`)){let t=E(e.getAttribute(`:ax-query`),M(e));t&&e.setAttribute(`ax-query`,String(t)),e.removeAttribute(`:ax-query`)}if(e.hasAttribute(`:ax-mutate`)){let t=E(e.getAttribute(`:ax-mutate`),M(e));t&&e.setAttribute(`ax-mutate`,String(t)),e.removeAttribute(`:ax-mutate`)}})}function J(e=document){q(e),e.querySelectorAll(`[ax-query]`).forEach(e=>{let t=e;P(t,{});let n=t.getAttribute(`ax-trigger`)||`load`;n===`load`?H(t):n===`intersect`?K.observe(t):t.addEventListener(n,e=>{e.preventDefault(),H(t)})}),e.querySelectorAll(`[ax-mutate]`).forEach(e=>{let t=e;P(t,{});let n=t.tagName===`FORM`?`submit`:`click`;t.addEventListener(n,e=>{(t.tagName===`A`||t.tagName===`BUTTON`||t.tagName===`FORM`)&&e.preventDefault(),U(t)})}),e.querySelectorAll(`[ax-trigger="refresh"]`).forEach(e=>{let t=e;t.addEventListener(`click`,e=>{e.preventDefault();let n=t.closest(`[ax-query]`);if(n){let e=V(n,n.getAttribute(`ax-query`));if(e){let t=u(e.target);if(t){let r=`${t.namespace}:${t.id}:${JSON.stringify(e.args)}`,i=L.get(r);i?i.fetch(!0):H(n)}}}})})}function Y(){new MutationObserver(e=>{e.forEach(e=>{e.addedNodes.forEach(e=>{e instanceof HTMLElement&&J(e)})})}).observe(document.body,{childList:!0,subtree:!0})}var X=class extends HTMLElement{constructor(){super()}connectedCallback(){let e=this.getAttribute(`loading-text`)||`Initializing...`,t=this.getAttribute(`ready-text`)||`Online`,n=this.getAttribute(`error-text`)||`Failed to Connect`;this.renderState(`loading`,e,`text-amber-500 animate-pulse`),document.addEventListener(`atmx:ready`,()=>{this.renderState(`ready`,t,`text-green-600`)}),document.addEventListener(`atmx:error`,e=>{let t=e;t.detail&&t.detail.stage===`runtime`&&this.renderState(`error`,`${n}: ${t.detail.message}`,`text-red-600`)})}renderState(e,t,n){this.setAttribute(`data-state`,e);let r=this.querySelectorAll(`[data-ax-when]`);r.length>0?r.forEach(t=>{t.getAttribute(`data-ax-when`)===e?t.removeAttribute(`hidden`):t.setAttribute(`hidden`,``)}):(this.innerText=t,this.getAttribute(`class`)||(this.className=`text-xs font-bold ${n}`))}};typeof window<`u`&&!customElements.get(`ax-engine-status`)&&customElements.define(`ax-engine-status`,X),T(),p(),c();var Z=`0.62.0`,Q=new class{version=Z;initialized=!1;debug=!1;async init(e){if(this.initialized)return{ok:!0,contracts:{},initDurationMs:0};let t=await b(e);return t.ok?(this.debug=e.debug||!1,Y(),J(document.body),this.initialized=!0,this.debug&&console.log(`%cš ATMX Debug Mode: Enabled`,`color: #2563eb; font-weight: bold;`),console.log(`ā
ATMX v${this.version} Online (${t.initDurationMs}ms). Reactive Engine Started.`),document.dispatchEvent(new CustomEvent(`atmx:ready`,{detail:t}))):(console.error(`ā ATMX Initialization Failed:`,t.error),document.dispatchEvent(new CustomEvent(`atmx:error`,{detail:t.error}))),t}triggerQuery(e){let t=typeof e==`string`?document.querySelector(e):e;t&&t instanceof HTMLElement&&t.hasAttribute(`ax-query`)&&H(t)}on(e,t){document.addEventListener(`atmx:${e}`,t)}setAuthToken(e,t,n){x(e,t,n)}sendStreamMessage(e,t){Promise.resolve().then(()=>(T(),g)).then(n=>n.sendStreamMessage(e,t))}clearAuthToken(e,t){S(e,t)}};typeof window<`u`&&(window.atmx=Q),e.ATMX_VERSION=Z,e.allocBytes=_,e.allocString=v,e.atmx=Q,e.buildRequestPath=d,e.clearAuthToken=S,e.initWasm=b,e.pendingRequests=s,e.resolveRoute=u,e.setAuthToken=x,Object.defineProperty(e,`wasmEngine`,{enumerable:!0,get:function(){return w}})});
|
package/dist/index.d.ts
CHANGED