svelte-viewpoint
Version:
Super tiny, loadable component for SvelteJS with blackjack and data preloading.
1 lines • 2.78 kB
Source Map (JSON)
{"version":3,"file":"index.min.mjs","sources":["../src/Viewpoint.svelte"],"sourcesContent":["{#await ($$restProps, Promise.resolve().then(wait).then(load).then(preload))}\n\t{#if timeout && !timeoutTimer}\n\t\t<slot name=\"waiting\" />\n\t{:else if !delayTimer}\n\t\t<slot name=\"loading\" />\n\t{/if}\n{:then comp}\n\t{#if comp}\n\t\t<svelte:component this={comp} {...$$restProps} {...state}>\n\t\t\t<slot />\n\t\t</svelte:component>\n\t{/if}\n{:catch error}\n\t<slot name=\"error\" {error} />\n{/await}\n\n<script>\n\tlet component = null,\n\t\tpreloading = true,\n\t\tstate = null,\n\t\ttimeoutTimer,\n\t\tdelayTimer,\n\t\tdelay = 200,\n\t\ttimeout = 0,\n\t\tabort = 0;\n\n\t$: load = function () {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tlet abortTimer;\n\n\t\t\tPromise.resolve(\n\t\t\t\ttypeof component === 'function' && !/^\\s*class\\s+/.test(component.toString()) // class, not a plain function\n\t\t\t\t\t? component()\n\t\t\t\t\t: component\n\t\t\t).then((m) => {\n\t\t\t\tclearTimeout(abortTimer);\n\t\t\t\tresolve(m);\n\t\t\t});\n\n\t\t\tabort &&\n\t\t\t\t(abortTimer = setTimeout(() => {\n\t\t\t\t\treject(new Error('Aborted by timeout.'));\n\t\t\t\t}, abort));\n\t\t});\n\t};\n\n\tfunction preload(m) {\n\t\treturn (\n\t\t\tm &&\n\t\t\tPromise.resolve(\n\t\t\t\tpreloading && typeof m.preload === 'function' ? m.preload($$restProps) : undefined\n\t\t\t).then((data = {}) => {\n\t\t\t\tstate = data;\n\t\t\t\treturn m.default || m;\n\t\t\t})\n\t\t);\n\t}\n\n\tfunction wait() {\n\t\tdelay &&\n\t\t\t(delayTimer = setTimeout(() => {\n\t\t\t\tdelayTimer = clearTimeout(delayTimer);\n\t\t\t}, delay));\n\n\t\ttimeout &&\n\t\t\t(timeoutTimer = setTimeout(() => {\n\t\t\t\ttimeoutTimer = clearTimeout(timeoutTimer);\n\t\t\t}, timeout));\n\t}\n\n\texport { preloading, component, timeout, delay, abort };\n</script>\n"],"names":["ctx","Promise","resolve","then","timeoutTimer","delayTimer","component","preloading","state","delay","timeout","abort","load","reject","abortTimer","test","toString","m","clearTimeout","setTimeout","Error","preload","$$restProps","undefined","data","default"],"mappings":"kuBAOMA,gFAAAA,gNAC8BA,KAAiBA,YAA3BA,yPAAUA,aAAiBA,yDAA3BA,qiBAPpBA,OAAYA,OAENA,23BAHHA,OAAaC,QAAQC,UAAUC,KAAKH,MAAMG,KAAKH,MAAMG,KAAKH,wJAA1DA,OAAaC,QAAQC,UAAUC,KAAKH,MAAMG,KAAKH,MAAMG,KAAKH,6SAoBjEI,EACAC,iDAJGC,EAAY,oBACfC,GAAa,KACbC,EAAQ,YAGRC,EAAQ,gBACRC,EAAU,YACVC,EAAQ,oSAENC,wBACSX,UAASC,EAASW,SACxBC,EAEJb,QAAQC,QACc,mBAAdI,kBAA4CS,KAAKT,EAAUU,YAE/DV,EADAA,KAEFH,MAAMc,IACPC,aAAaJ,GACbZ,EAAQe,MAGTN,IACEG,EAAaK,iBACbN,MAAWO,MAAM,0BACfT,8BAIWM,UAEfA,GACAhB,QAAQC,QACPK,GAAmC,mBAAdU,EAAEI,QAAyBJ,EAAEI,QAAQC,QAAeC,GACxEpB,OAAMqB,YACPhB,EAAQgB,GACDP,EAAEQ,SAAWR,iBAMtBR,OACEJ,EAAac,qBACbd,EAAaa,aAAab,MACxBI,IAEJC,OACEN,EAAee,qBACff,EAAec,aAAad,MAC1BM"}