UNPKG

svelte-statusable

Version:

Super tiny, simple to use SvelteJS store to control your application status.

3 lines (2 loc) 1.75 kB
import{readable as e}from"svelte/store";const n="undefined"!=typeof window,t="undefined"!=typeof navigator,r="undefined"!=typeof document,o="undefined"!=typeof EventSource,i="undefined"!=typeof AbortController;function d({ping:d,sse:a}){let v={online:!t||navigator.onLine,hidden:!!r&&document.hidden,heartbeat:!n,stream:!n};return"string"==typeof d&&(d={url:d,method:"HEAD",cache:"no-cache",credentials:"omit",referrerPolicy:"no-referrer"}),"string"==typeof a&&(a={url:a,withCredentials:!1}),e(v,(e=>{if(!n||!t||!r)return;let s,u;function l(n,t){v[n]!==t&&e(v={...v,[n]:t})}function c(){l("online",navigator.onLine)}function f(){l("hidden",document.hidden)}function h(e){l("stream",e.target.readyState===EventSource.OPEN)}return a&&o&&(s=new EventSource(a.url,{withCredentials:a.withCredentials}),s.addEventListener("open",h),s.addEventListener("error",h),a.event&&s.addEventListener(a.event,h),l("stream",s.readyState===EventSource.OPEN)),d&&(u=function(e,n){return e(),setInterval(e,n)}((()=>{!document.hidden&&navigator.onLine&&function({url:e,abort:n=0,payload:t=!1,retry:r,...o}){if(n&&i){const e=new AbortController;o.signal=e.signal,setTimeout((()=>e.abort()),n)}return fetch(e,o).then((e=>t?e.json():"opaque"===e.type||e.ok)).catch((()=>!1))}(d).then((e=>l("heartbeat",e)))}),d.retry||1e4)),window.addEventListener("online",c),window.addEventListener("offline",c),window.addEventListener("visibilitychange",f),()=>{window.removeEventListener("online",c),window.removeEventListener("offline",c),window.removeEventListener("visibilitychange",f),s&&(s.removeEventListener("open",h),s.removeEventListener("error",h),a.event&&s.removeEventListener(a.event,h)),clearInterval(u)}}))}export{d as statusable}; //# sourceMappingURL=index.min.mjs.map