Packages
lustre
5.3.0
5.7.1
5.7.0
5.6.0
5.5.2
5.5.1
5.5.0
5.4.0
5.3.5
5.3.4
5.3.3
5.3.2
5.3.1
5.3.0
5.2.1
5.2.0
5.1.1
5.1.0
5.0.3
5.0.2
5.0.1
5.0.0
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.0
4.0.0-rc1
4.0.0-rc.2
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.12
3.0.11
3.0.10
3.0.9
3.0.8
3.0.7
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.5
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
2.0.1
2.0.0
1.3.0
1.2.0
1.1.0
1.0.0
Create HTML templates, single page applications, Web Components, and real-time server components in Gleam!
Current section
Files
Jump to
Current section
Files
priv/static/lustre-server-component.min.mjs
var a=class{withFields(e){let n=Object.keys(this).map(t=>t in e?e[t]:this[t]);return new this.constructor(...n)}};var qt=5,ne=Math.pow(2,qt),Bn=ne-1,Cn=ne/2,On=ne/4;var Fe=[" "," ",`
`,"\v","\f","\r","\x85","\u2028","\u2029"].join(""),gr=new RegExp(`^[${Fe}]*`),vr=new RegExp(`[${Fe}]*$`);var y=()=>globalThis?.document,oe="http://www.w3.org/1999/xhtml",le=1,ae=3,ce=11,We=!!globalThis.HTMLElement?.prototype?.moveBefore;var it=0;var st=1;var ut=2;var ot=0;var de=2;var z=class extends a{constructor(e,n,t,r,s,u){super(),this.kind=e,this.key=n,this.mapper=t,this.children=r,this.keyed_children=s,this.children_count=u}};function A(i){return i instanceof z?1+i.children_count:1}var lt=0;var at=1;var ct=2;var ft=3;var pt=" ";var mt=0;var xt=1;var wt=2;var he=3;var bt=4;var me=5;var xe=6;var we=7;var P=class{offset=0;#r=null;#e=()=>{};#t=!1;#n=!1;constructor(e,n,{useServerEvents:t=!1,exposeKeys:r=!1}={}){this.#r=e,this.#e=n,this.#t=t,this.#n=r}mount(e){X(this.#r,this.#p(this.#r,0,e))}#i=[];push(e){let n=this.offset;n&&(w(e.changes,t=>{switch(t.kind){case xe:case he:t.before=(t.before|0)+n;break;case we:case me:t.from=(t.from|0)+n;break}}),w(e.children,t=>{t.index=(t.index|0)+n})),this.#i.push({node:this.#r,patch:e}),this.#s()}#s(){let e=this;for(;e.#i.length;){let{node:n,patch:t}=e.#i.pop();w(t.changes,u=>{switch(u.kind){case xe:e.#u(n,u.children,u.before);break;case he:e.#a(n,u.key,u.before,u.count);break;case bt:e.#l(n,u.key,u.count);break;case we:e.#o(n,u.from,u.count);break;case me:e.#f(n,u.from,u.count,u.with);break;case mt:e.#d(n,u.content);break;case xt:e.#_(n,u.inner_html);break;case wt:e.#x(n,u.added,u.removed);break}}),t.removed&&e.#o(n,n.childNodes.length-t.removed,t.removed);let r=-1,s=null;w(t.children,u=>{let o=u.index|0,b=s&&r-o===1?s.previousSibling:D(n,o);e.#i.push({node:b,patch:u}),s=b,r=o})}}#u(e,n,t){let r=vt(),s=t|0;w(n,u=>{let o=this.#p(e,s,u);X(r,o),s+=A(u)}),be(e,r,D(e,t))}#a(e,n,t,r){let s=kt(e,n),u=D(e,t);for(let o=0;o<r&&s!==null;++o){let b=s.nextSibling;We?e.moveBefore(s,u):be(e,s,u),s=b}}#l(e,n,t){this.#c(e,kt(e,n),t)}#o(e,n,t){this.#c(e,D(e,n),t)}#c(e,n,t){for(;t-- >0&&n!==null;){let r=n.nextSibling,s=n[l].key;s&&e[l].keyedChildren.delete(s);for(let[u,{timeout:o}]of n[l].debouncers??[])clearTimeout(o);e.removeChild(n),n=r}}#f(e,n,t,r){this.#o(e,n,t);let s=this.#p(e,n,r);be(e,s,D(e,n))}#d(e,n){e.data=n??""}#_(e,n){e.innerHTML=n??""}#x(e,n,t){w(t,r=>{let s=r.name;e[l].handlers.has(s)?(e.removeEventListener(s,$e),e[l].handlers.delete(s),e[l].throttles.has(s)&&e[l].throttles.delete(s),e[l].debouncers.has(s)&&(clearTimeout(e[l].debouncers.get(s).timeout),e[l].debouncers.delete(s))):(e.removeAttribute(s),Et[s]?.removed?.(e,s))}),w(n,r=>{this.#m(e,r)})}#p(e,n,t){switch(t.kind){case at:{let r=yt(e,n,t);return this.#h(r,t),this.#u(r,t.children),r}case ct:return gt(e,n,t);case lt:{let r=vt(),s=gt(e,n,t);X(r,s);let u=n+1;return w(t.children,o=>{X(r,this.#p(e,u,o)),u+=A(o)}),r}case ft:{let r=yt(e,n,t);return this.#h(r,t),this.#_(r,t.inner_html),r}}}#h(e,{key:n,attributes:t}){this.#n&&n&&e.setAttribute("data-lustre-key",n),w(t,r=>this.#m(e,r))}#m(e,n){let{debouncers:t,handlers:r,throttles:s}=e[l],{kind:u,name:o,value:b,prevent_default:je,stop_propagation:Nt,immediate:Z,include:Mt,debounce:Ee,throttle:Se}=n;switch(u){case it:{let f=b??"";if(o==="virtual:defaultValue"){e.defaultValue=f;return}f!==e.getAttribute(o)&&e.setAttribute(o,f),Et[o]?.added?.(e,b);break}case st:e[o]=b;break;case ut:{if(r.has(o)&&e.removeEventListener(o,$e),e.addEventListener(o,$e,{passive:je.kind===ot}),Se>0){let f=s.get(o)??{};f.delay=Se,s.set(o,f)}else s.delete(o);if(Ee>0){let f=t.get(o)??{};f.delay=Ee,t.set(o,f)}else clearTimeout(t.get(o)?.timeout),t.delete(o);r.set(o,f=>{je.kind===de&&f.preventDefault(),Nt.kind===de&&f.stopPropagation();let g=f.type,ee=f.currentTarget[l].path,te=this.#t?bn(f,Mt??[]):f,v=s.get(g);if(v){let ze=Date.now(),It=v.last||0;ze>It+v.delay&&(v.last=ze,v.lastEvent=f,this.#e(te,ee,g,Z))}let B=t.get(g);B&&(clearTimeout(B.timeout),B.timeout=setTimeout(()=>{f!==s.get(g)?.lastEvent&&this.#e(te,ee,g,Z)},B.delay)),!v&&!B&&this.#e(te,ee,g,Z)});break}}}},w=(i,e)=>{if(Array.isArray(i))for(let n=0;n<i.length;n++)e(i[n]);else if(i)for(i;i.tail;i=i.tail)e(i.head)},X=(i,e)=>i.appendChild(e),be=(i,e,n)=>i.insertBefore(e,n??null),yt=(i,e,{key:n,tag:t,namespace:r})=>{let s=y().createElementNS(r||oe,t);return F(i,s,e,n),s},gt=(i,e,{key:n,content:t})=>{let r=y().createTextNode(t??"");return F(i,r,e,n),r},vt=()=>y().createDocumentFragment(),D=(i,e)=>i.childNodes[e|0],l=Symbol("lustre"),F=(i,e,n=0,t="")=>{let r=`${t||n}`;switch(e.nodeType){case le:case ce:e[l]={key:t,path:r,keyedChildren:new Map,handlers:new Map,throttles:new Map,debouncers:new Map};break;case ae:e[l]={key:t};break}i&&i[l]&&t&&i[l].keyedChildren.set(t,new WeakRef(e)),i&&i[l]&&i[l].path&&(e[l].path=`${i[l].path}${pt}${r}`)};var kt=(i,e)=>i[l].keyedChildren.get(e).deref(),$e=i=>{let n=i.currentTarget[l].handlers.get(i.type);i.type==="submit"&&(i.detail??={},i.detail.formData=[...new FormData(i.target).entries()]),n(i)},bn=(i,e=[])=>{let n={};(i.type==="input"||i.type==="change")&&e.push("target.value"),i.type==="submit"&&e.push("detail.formData");for(let t of e){let r=t.split(".");for(let s=0,u=i,o=n;s<r.length;s++){if(s===r.length-1){o[r[s]]=u[r[s]];break}o=o[r[s]]??={},u=u[r[s]]}}return n},jt=i=>({added(e){e[i]=!0},removed(e){e[i]=!1}}),$n=i=>({added(e,n){e[i]=n}}),Et={checked:jt("checked"),selected:jt("selected"),value:$n("value"),autofocus:{added(i){queueMicrotask(()=>i.focus?.())}},autoplay:{added(i){try{i.play?.()}catch(e){console.error(e)}}}};var St=new WeakMap;async function zt(i){let e=[];for(let t of y().querySelectorAll("link[rel=stylesheet], style"))t.sheet||e.push(new Promise((r,s)=>{t.addEventListener("load",r),t.addEventListener("error",s)}));if(await Promise.allSettled(e),!i.host.isConnected)return[];i.adoptedStyleSheets=i.host.getRootNode().adoptedStyleSheets;let n=[];for(let t of y().styleSheets)try{i.adoptedStyleSheets.push(t)}catch{try{let r=St.get(t);if(!r){r=new CSSStyleSheet;for(let s of t.cssRules)r.insertRule(s.cssText,r.cssRules.length);St.set(t,r)}i.adoptedStyleSheets.push(r)}catch{let r=t.ownerNode.cloneNode();i.prepend(r),n.push(r)}}return n}var At=0;var Bt=1;var Ct=2;var K=0;var Ot=1;var Tt=2;var Q=3;var ye=class extends HTMLElement{static get observedAttributes(){return["route","method"]}#r;#e="ws";#t=null;#n=null;#i=[];#s;#u=new Set;#a=new Set;#l=!1;#o=[];#c=new MutationObserver(e=>{let n=[];for(let t of e){if(t.type!=="attributes")continue;let r=t.attributeName;(!this.#l||this.#u.has(r))&&n.push([r,this.getAttribute(r)])}if(n.length===1){let[t,r]=n[0];this.#n?.send({kind:K,name:t,value:r})}else n.length?this.#n?.send({kind:Q,messages:n.map(([t,r])=>({kind:K,name:t,value:r}))}):this.#o.push(...n)});constructor(){super(),this.internals=this.attachInternals(),this.#c.observe(this,{attributes:!0})}connectedCallback(){for(let e of this.attributes)this.#o.push([e.name,e.value])}attributeChangedCallback(e,n,t){switch(e){case(n!==t&&"route"):{this.#t=new URL(t,location.href),this.#f();return}case"method":{let r=t.toLowerCase();if(r==this.#e)return;["ws","sse","polling"].includes(r)&&(this.#e=r,this.#e=="ws"&&(this.#t.protocol=="https:"&&(this.#t.protocol="wss:"),this.#t.protocol=="http:"&&(this.#t.protocol="ws:")),this.#f());return}}}async messageReceivedCallback(e){switch(e.kind){case At:{for(this.#r??=this.attachShadow({mode:e.open_shadow_root?"open":"closed"}),F(null,this.#r,"");this.#r.firstChild;)this.#r.firstChild.remove();this.#s=new P(this.#r,(t,r,s)=>{this.#n?.send({kind:Ot,path:r,name:s,event:t})},{useServerEvents:!0}),this.#u=new Set(e.observed_attributes);let n=this.#o.filter(([t])=>this.#u.has(t));n.length&&this.#n.send({kind:Q,messages:n.map(([t,r])=>({kind:K,name:t,value:r}))}),this.#o=[],this.#a=new Set(e.observed_properties);for(let t of this.#a)Object.defineProperty(this,t,{get(){return this[`_${t}`]},set(r){this[`_${t}`]=r,this.#n?.send({kind:Tt,name:t,value:r})}});e.will_adopt_styles&&await this.#d(),this.#s.mount(e.vdom),this.dispatchEvent(new CustomEvent("lustre:mount"));break}case Bt:{this.#s.push(e.patch);break}case Ct:{this.dispatchEvent(new CustomEvent(e.name,{detail:e.data}));break}}}#f(){if(!this.#t||!this.#e)return;this.#n&&this.#n.close();let r={onConnect:()=>{this.#l=!0,this.dispatchEvent(new CustomEvent("lustre:connect"),{detail:{route:this.#t,method:this.#e}})},onMessage:s=>{this.messageReceivedCallback(s)},onClose:()=>{this.#l=!1,this.dispatchEvent(new CustomEvent("lustre:close"),{detail:{route:this.#t,method:this.#e}})}};switch(this.#e){case"ws":this.#n=new ge(this.#t,r);break;case"sse":this.#n=new ve(this.#t,r);break;case"polling":this.#n=new ke(this.#t,r);break}}async#d(){for(;this.#i.length;)this.#i.pop().remove(),this.#r.firstChild.remove();this.#i=await zt(this.#r),this.#s.offset=this.#i.length}},ge=class{#r;#e;#t=!1;#n=[];#i;#s;#u;constructor(e,{onConnect:n,onMessage:t,onClose:r}){this.#r=e,this.#e=new WebSocket(this.#r),this.#i=n,this.#s=t,this.#u=r,this.#e.onopen=()=>{this.#i()},this.#e.onmessage=({data:s})=>{try{this.#s(JSON.parse(s))}finally{this.#n.length?this.#e.send(JSON.stringify({kind:Q,messages:this.#n})):this.#t=!1,this.#n=[]}},this.#e.onclose=()=>{this.#u()}}send(e){if(this.#t||this.#e.readyState!==WebSocket.OPEN){this.#n.push(e);return}else this.#e.send(JSON.stringify(e)),this.#t=!0}close(){this.#e.close()}},ve=class{#r;#e;#t;#n;#i;constructor(e,{onConnect:n,onMessage:t,onClose:r}){this.#r=e,this.#e=new EventSource(this.#r),this.#t=n,this.#n=t,this.#i=r,this.#e.onopen=()=>{this.#t()},this.#e.onmessage=({data:s})=>{try{this.#n(JSON.parse(s))}catch{}}}send(e){}close(){this.#e.close(),this.#i()}},ke=class{#r;#e;#t;#n;#i;#s;constructor(e,{onConnect:n,onMessage:t,onClose:r,...s}){this.#r=e,this.#n=n,this.#i=t,this.#s=r,this.#e=s.interval??5e3,this.#u().finally(()=>{this.#n(),this.#t=setInterval(()=>this.#u(),this.#e)})}async send(e){}close(){clearInterval(this.#t),this.#s()}#u(){return fetch(this.#r).then(e=>e.json()).then(this.#i).catch(console.error)}};customElements.define("lustre-server-component",ye);export{ye as ServerComponent};