UNPKG

@appmate/wishlist

Version:

Wishlist King SDK

2 lines (1 loc) 7.18 kB
var j=d=>{throw TypeError(d)};var $=(d,e,t)=>e.has(d)||j("Cannot "+t);var m=(d,e,t)=>($(d,e,"read from private field"),t?t.call(d):e.get(d)),W=(d,e,t)=>e.has(d)?j("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(d):e.set(d,t),A=(d,e,t,i)=>($(d,e,"write to private field"),i?i.call(d,t):e.set(d,t),t),a=(d,e,t)=>($(d,e,"access private method"),t);var L,b=d=>(!!localStorage.getItem("debug")&&(L=import("https://cdn.jsdelivr.net/npm/debug@4.3.7/+esm")),(t,...i)=>{L&&L.then(({default:o})=>{o(d)(t,...i)})});var n=b("wk:loader");window.headlessElements=new Map;var f,g,M,E,v,s,F,T,_,C,I,U,J,D,K,O,B,R,H,V,k=class{constructor({sdkHost:e,themeCodeHost:t,themeCodeVersion:i}){W(this,s);W(this,f,new Map);W(this,g,"");W(this,M,"");W(this,E,"");W(this,v,new EventTarget);A(this,g,e),A(this,M,t),A(this,E,i),a(this,s,V).call(this)}getSdkUrl(e){return e?`${m(this,g)}/${e}`:""}getThemeCodeUrl(e){return e?`${m(this,M)}/${e}?v=${m(this,E)}`:""}load(e){var u,p,w;let{name:t,url:i,isDependency:o}=e,l=t!=null?t:i;if(!t&&!i||!l)return Promise.resolve({});!e.type&&e.url&&(e={...e,type:a(this,s,B).call(this,e.url)}),o?n(`Load dependecy: ${l}`,{asset:e}):n(`Load: ${l}`,{asset:e});let h=m(this,f).get(l);if(h)return h;(u=e.dependencies)==null||u.forEach(y=>y.isDependency=!0);let r=(w=(p=e.dependencies)==null?void 0:p.map(y=>this.load(y)))!=null?w:[];e.type==="headless-component"?r.push(this.load({type:"core",name:"wishlist-element-headless",url:this.getSdkUrl("wishlist-element-headless.js"),isDependency:!0})):e.type==="lit-component"&&r.push(this.load({type:"core",name:"wishlist-element",url:this.getSdkUrl("wishlist-element.js"),isDependency:!0}));let c=Promise.all([a(this,s,F).call(this,e),...r]).then(async([y])=>{var S,N,x;if(await((S=e.afterDependencies)==null?void 0:S.call(e)),e.dependencies)for(let P of e.dependencies)await((N=P.afterMainModule)==null?void 0:N.call(P));return(x=e.afterLoad)==null||x.call(e,y),y});return m(this,f).set(l,c),document.dispatchEvent(new CustomEvent(`wk:loader:${l}:load`)),c}async waitFor(e){let t=m(this,f).get(e);return t||(console.warn("Waiting for unknwon module:",e),Promise.resolve({}))}async waitForCore(e){let t=m(this,f).get(e);return t?(n(`Wait for core module: ${e}`),t):(n(`Defer wait until core module available: ${e}`),new Promise(i=>{document.addEventListener(`wk:loader:${e}:load`,()=>{n(`Deferred wait can continue: ${e}`),i(m(this,f).get(e))},{once:!0})}))}async once(e){return new Promise(t=>{m(this,v).addEventListener(e,()=>t(null),{once:!0})})}};f=new WeakMap,g=new WeakMap,M=new WeakMap,E=new WeakMap,v=new WeakMap,s=new WeakSet,F=async function(e){var h;let{url:t,type:i}=e;if(!t)return Promise.resolve({});let o=a(this,s,R).call(this,{url:t,noCache:a(this,s,H).call(this)}),l=!!((h=e.dependencies)!=null&&h.length);switch(a(this,s,H).call(this)&&n(`Load dev asset: ${o}`,{asset:e}),i){case"css":return a(this,s,J).call(this,o,e.order);case"js":return a(this,s,D).call(this,o).then(r=>a(this,s,U).call(this,e,r));case"json":return a(this,s,O).call(this,o).then(r=>a(this,s,T).call(this,r));case"lit-component":return a(this,s,D).call(this,o).then(r=>e.isDependency?(n(`Wait for main module: ${o}`,{asset:e}),e.afterMainModule=async()=>{n(`Main module loaded: ${o}`,{asset:e}),await a(this,s,C).call(this,e,r)},Promise.resolve(r)):l?(n(`Wait for dependencies: ${o}`,{asset:e}),e.afterDependencies=async()=>{n(`Dependencies loaded: ${o}`,{asset:e}),await a(this,s,C).call(this,e,r)},Promise.resolve(r)):a(this,s,C).call(this,e,r));case"headless-component":return a(this,s,D).call(this,o).then(r=>a(this,s,I).call(this,e,r));case"core":return a(this,s,K).call(this,o).then(r=>a(this,s,_).call(this,e,r));default:return Promise.resolve({})}},T=async function(e){return n("Loaded json",{module:e}),e},_=async function(e,t){return n(`Loaded core module: ${e.name}`,{asset:e,module:t}),t.WishlistApp?(n("Create wishlist app",{asset:e,module:t}),window.WishlistKing=new t.WishlistApp(e.props),{...t,artifact:window.WishlistKing}):t},C=async function(e,t){var u;let{setup:i,inject:o,define:l}=t;n(`Loaded lit component: ${e.name}`,{asset:e,module:t});let r=(await this.waitForCore("wishlist-app")).artifact;e.isDependency||(typeof i=="function"?(n(`Setup lit component: ${e.name}`,{asset:e,module:t}),i({app:r,props:e.props})):typeof o=="function"&&(n(`Inject lit component: ${e.name}`,{asset:e,module:t}),o(r,e.props)));let c=await this.waitForCore("wishlist-element");if(typeof l=="function"){let p=e.customElementName||e.name;if(p&&window.customElements.get(p))return t;let w=l({...(u=e.props)!=null?u:{},...c},e.props);n(`Define lit component: ${p}`,{asset:e,module:t,element:w,wishlistElement:c}),w&&p&&window.customElements.define(p,w)}return t},I=async function(e,t){var u;let{setup:i,inject:o,define:l}=t;n(`Loaded headless component: ${e.name}`,{asset:e,module:t});let h=await this.waitForCore("wishlist-element-headless");if(typeof l=="function"){let p=l(h);n(`Define headless component: ${e.name}`,{asset:e,module:t,element:p}),p&&e.name&&((u=window.headlessElements)==null||u.set(e.name,p))}let c=(await this.waitForCore("wishlist-app")).artifact;return typeof i=="function"?(n(`Setup headless component: ${e.name}`,{asset:e,module:t}),i({app:c})):typeof o=="function"&&(n(`Inject headless component: ${e.name}`,{asset:e,module:t}),o(c)),t},U=async function(e,t){let{setup:i}=t;return n(`Loaded js component: ${e.name}`,{asset:e,module:t}),this.waitForCore("wishlist-app").then(o=>{if(typeof i=="function"&&o.artifact){let l=o.artifact;n(`Setup js component: ${e.name}`,{asset:e,module:t}),i({app:l}),e.name&&m(this,v).dispatchEvent(new CustomEvent(`${e.name}:setup`))}}),t},J=async function(e,t=0){return new Promise((i,o)=>{let l=document.createElement("link");l.rel="stylesheet",l.href=e,l.dataset.loaderOrder=t.toString(),l.onerror=o,l.onload=()=>{n("Loaded stylesheet",{url:e}),i({})};let h=Array.from(document.head.querySelectorAll("link[data-loader-order]")).find(r=>{var c;return parseInt((c=r.dataset.loaderOrder)!=null?c:"0")>t});h?document.head.insertBefore(l,h):document.head.appendChild(l)})},D=async function(e){return import(e)},K=async function(e){return import(e)},O=async function(e){return fetch(e).then(t=>t.json())},B=function(e){var i;let t=(i=e.split(".").pop())==null?void 0:i.split(/\#|\?/)[0].toString();if(!(!t||!["js","css","json"].includes(t)))return t},R=function({url:e,noCache:t}){return t&&!e.includes("/themecode-templates/")?e.replace("cdn.appmate.io","storage.googleapis.com/appmate-cdn").replace(/v=\d*/,`v=${Date.now()}`):e},H=function(){return!document.getElementById("preview-bar-iframe")?!1:!!window.localStorage.getItem("wk_dev_mode")},V=function(){window.location.search.includes("wk_dev_mode=on")?(n("Activate dev mode"),window.localStorage.setItem("wk_dev_mode",Date.now().toString())):window.location.search.includes("wk_dev_mode=off")&&(n("Deactivate dev mode"),window.localStorage.removeItem("wk_dev_mode")),a(this,s,H).call(this)&&(n("Dev mode active"),window.addEventListener("message",e=>{e.data==="WK_THEME_RELOAD"&&(window.localStorage.setItem("wk_dev_mode",Date.now().toString()),window.location.reload())},!1))};export{k as AppLoader};