UNPKG

subapp-web

Version:

Electrode subapp web support

1 lines 4.17 kB
!function(t){t._wml||(t._wml={});const e=1e6;if(t.xarcV1&&t.xarcV1.version>=e)return t.xarcV1;const n={instId:1,subApps:{},bundles:{},onLoadStart:{},groups:{},started:!1};let s;const o="root",r=(t=o)=>t;t.xarcV1=s={IS_BROWSER:!0,HAS_WINDOW:"undefined"!=typeof window,version:e,rt:n,cdnInit(){},cdnUpdate(){},cdnMap:t=>t,defer(){const t={};return t.promise=new Promise((e,n)=>{t.resolve=e,t.reject=n,t.done=(t,s)=>{t?n(t):e(s)}}),t},asyncMap(t,e,n){const o=[];let r,a=0,d=0,u=t&&t.length;n>0||(n=10);const l=s.defer(),c=()=>{if(r||d>=u||n<=0)return;n--;const s=d++;o[s]=void 0;const i=t=>{n++,a++,o[s]=t,r||a!==u?c():l.resolve(o)},p=t=>{r||(r=t,t.partial=o,l.reject(t))};try{const n=e(t[s],s,t);n&&n.then?(n.then(i,p),c()):i(n)}catch(t){p(t)}};return setTimeout(u>0?c:()=>l.resolve(o),0),l.promise},getBundle(t,e){if(e=r(e),t&&n.bundles[e])return n.bundles[e][t.toLowerCase()]},setBundle(t,e,s){s=r(s),n.bundles[s]=n.bundles[s]||{},n.bundles[s][t.toLowerCase()]=e},getSubApp:t=>n.subApps[t],setSubApp(t,e){n.subApps[t]=e},getOnLoadStart:t=>(n.onLoadStart[t]||(n.onLoadStart[t]=[]),n.onLoadStart[t]),addOnLoadStart(t,e){s.getOnLoadStart(t).push(e)},startSubApp(t,e,n){let s;const o=n=>()=>t[n]&&t[n](s,e,t.info),r=Promise.resolve().then(o("preStart")).then(t=>(s=t)&&t._prepared).then(o("preRender")).then(o("signalReady"));return n?r:r.then(o("start"))},startGroup(t){if(!n.started||t.started||t.queue.length<t.total)return;const e=(e,n)=>()=>s.asyncMap(n||t.queue,t=>{const n=t.subApp;return n[e]&&n[e](t.instance,t.options,n.info)});t.started=!0,console.log("Starting subapp group",t),s.asyncMap(t.queue,t=>t.instance._prepared).then(e("preRender")).then(e("signalReady")).then(e("start",t.queue.filter(t=>!t.options.inline)))},watchSubAppOnLoad(t){n.onLoadWatcher&&(clearTimeout(n.onLoadWatcher),n.onLoadWatcher=void 0);const e=()=>{n.onLoadWatcher=void 0;const t=n.onLoadStart;let e=0;for(const o in t){if(!t[o])continue;const r=s.getSubApp(o);if(r&&r._started)if(r.start&&r.info){const e=t[o];t[o]=void 0,e.forEach(t=>{if(t.id||t.elementId||((t=Object.assign({},t))._genId=t.name+"_inst_id_"+n.instId++),t.getInitialState&&(t.initialState=t.getInitialState()),!t.group)return s.startSubApp(r,t);{const e=n.groups[t.group],o={options:t,subApp:r,instance:r.preStart(null,t)};e.queue.push(o),s.startGroup(e),e.queue.length<e.total&&console.log("subapp group",t.group,"not ready to start: total",e.total,"count",e.queue.length)}})}else console.error(`subApp ${o} exist but missing start or info`);else e++}e?s.watchSubAppOnLoad():n.onLoadStart={}};t&&e(),n.onLoadWatcher=setTimeout(e,10)},startSubAppOnLoad(t){const{name:e}=t,s=n.onLoadStart;s[e]||(s[e]=[]);const o=t.group;if(o){let t=n.groups[o];t||(t=n.groups[o]={group:o,total:0,queue:[]}),t.total++}s[e].push(t)},start(){console.log("xarcV1 start, version:",e),n.started=!0;const t=n.groups;for(let e in t)s.startGroup(t[e]);s.watchSubAppOnLoad(!0)},markBundlesLoaded(t,e){[].concat(t).forEach(t=>{s.setBundle(t.toString(),1,e)})},getBundleAssets(t){if(t=r(t),n.bundleAssets||(n.bundleAssets={}),!n.bundleAssets[t]){const e=t===o?"bundleAssets":`${t}Assets`;n.bundleAssets[t]=s.dyn(e);(s.rt.md?s.cdnUpdate:s.cdnInit)(n.bundleAssets[t])}return n.bundleAssets[t]},loadSubAppBundles(t,e,n){e=e||(()=>{}),n=r(n);const o=[].concat(t).filter(t=>void 0===s.getBundle(t,n));if(0===o.length)return e();const a=s.getBundleAssets(n),d=[],u=o.reduce((t,e)=>a.entryPoints[e].reduce((t,n)=>{const s=a.jsChunksById[n];return s?t.concat({name:e,id:n,assets:[].concat(s)}):t},t),[]).filter(({id:t})=>(t=t.toString(),void 0===s.getBundle(t,n)&&(s.setBundle(t,0,n),!0)));u.forEach(({name:t,id:o,assets:r})=>{const l=r.map(t=>`${a.basePath}${t}`),c=()=>{d.push(r),d.length===u.length&&(console.debug("all assets loaded",u),e())};loadjs(l,o,{success:()=>{console.debug(`loaded asset for ${t} (id: ${o}) - ${r}`),s.setBundle(o,1,n),c()},error:()=>{console.error(`load asset ${t} (id: ${o}) failed`,err),c()}})})},dyn(t){const e="ERROR: fail retrieve dynamic data from element",n=document.getElementById(t);if(n)try{return JSON.parse(n.innerHTML)}catch(t){return console.error(e,"- parse"),{}}else console.error(e,t,"- get")}}}(window);