UNPKG

@merkur/cli

Version:

Merkur is tiny and extensible library for creating front-end microservices.

2 lines (1 loc) 2.61 kB
"use strict";(()=>{var l=class{#e=new Set;pipe(...r){return S(...r)(this)}next(...r){Array.from(this.#e).forEach(e=>{typeof e=="function"?e(...r):e.next(...r)})}error(...r){Array.from(this.#e).forEach(e=>{e?.error?.(...r),this.unsubscribe(e)})}complete(...r){Array.from(this.#e).forEach(e=>{e?.complete?.(...r),this.unsubscribe(e)})}subscribe(r){return this.#e.add(r),{unsubscribe:()=>{this.unsubscribe(r)}}}unsubscribe(r){this.#e.delete(r)}};function S(...r){return e=>Array.from(r).reduce((i,c)=>c(i),e)}var y=5,m=class extends l{#e=null;#r=0;#t={};constructor(e){super(),this.#t=e}init(){return this.#n(),this}send(e){return this.#e&&this.#e.send(e),this}destroy(){return this.#e&&(this.#e.close(),this.#e=null),this}#n(){this.#e=Reflect.construct(WebSocket,[`${this.#t.protocol}//${this.#t.host}`]),this.#e.onopen=()=>{this.#r=0},this.#e.onmessage=e=>{try{let i=JSON.parse(e.data);this.next(i)}catch(i){console.error(i)}},this.#e.onerror=e=>{console.error(e)},this.#e.onclose=()=>{this.#r>=y||(this.#r+=1,setTimeout(()=>{this.#n()},this.#r*500))}}};async function w({to:r,command:e}){r==="browser"&&e==="reload"&&location.reload()}async function f({to:r,command:e,changed:i}){if(r==="browser"&&e==="refresh"){if(await Promise.all(i.map(async o=>new Promise(a=>{let t=document.querySelector(`[data-name="${o.name}"]`);if(!t||t.nodeName==="STYLE"){location.reload();return}let n=null,u=new URLSearchParams({version:Math.random()});if(t.nodeName==="LINK"){let s=new URL(t.href);n=t.cloneNode(),n.onload=()=>{t.remove(),a()},n.onerror=()=>{t.remove(),a()},n.href=new URL(`${s.origin}${s.pathname}?${u.toString()}`)}if(t.nodeName==="SCRIPT"){let s=new URL(t.src);n=document.createElement("script"),n.setAttribute("data-name",o.name),n.onload=()=>{t.remove(),a()},n.onerror=()=>{t.remove(),a()},n.src=new URL(`${s.origin}${s.pathname}?${u.toString()}`)}t.parentNode.insertBefore(n,t.nextSibling)}))),!i.some(o=>o.name.endsWith(".js")))return;let c=window.__merkur_dev__.widgets;window.__merkur_dev__.widgets=[],c.forEach(async o=>{let{props:a,state:t,$external:n,name:u,version:s,containerSelector:_,container:p,slot:b}=o,k={props:a,state:t,$external:n,name:u,version:s,containerSelector:_,container:p,slot:b},g=await window.__merkur__.create(k);await o.unmount(),await g.mount()})}}var{__merkur_dev__:h}=window,d=new m(h.merkurConfig.socketServer);h.webSocket=d;h.widgets=[];d.init();d.subscribe(w);d.subscribe(f);addEventListener("load",function(){if(window?.__merkur__?.create){let e=window.__merkur__.create;window.__merkur__.create=async function(...c){let o=await e(...c);return h.widgets.push(o),o}}});})();