UNPKG

svelte-statusable

Version:

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

3 lines (2 loc) 2.24 kB
function e(){}Promise.resolve();const n=[];function t(t,r=e){let o;const i=new Set;function d(e){if(d=e,((r=t)!=r?d==d:r!==d||r&&"object"==typeof r||"function"==typeof r)&&(t=e,o)){const e=!n.length;for(const e of i)e[1](),n.push(e,t);if(e){for(let e=0;e<n.length;e+=2)n[e][0](n[e+1]);n.length=0}}var r,d}return{set:d,update:function(e){d(e(t))},subscribe:function(n,s=e){const a=[n,s];return i.add(a),1===i.size&&(o=r(d)||e),n(t),()=>{i.delete(a),0===i.size&&(o(),o=null)}}}}const r="undefined"!=typeof window,o="undefined"!=typeof navigator,i="undefined"!=typeof document,d="undefined"!=typeof EventSource,s="undefined"!=typeof AbortController;function a({ping:e,sse:n}){let a={online:!o||navigator.onLine,hidden:!!i&&document.hidden,heartbeat:!r,stream:!r};return"string"==typeof e&&(e={url:e,method:"HEAD",cache:"no-cache",credentials:"omit",referrerPolicy:"no-referrer"}),"string"==typeof n&&(n={url:n,withCredentials:!1}),function(e,n){return{subscribe:t(e,n).subscribe}}(a,(t=>{if(!r||!o||!i)return;let u,c;function f(e,n){a[e]!==n&&t(a={...a,[e]:n})}function l(){f("online",navigator.onLine)}function v(){f("hidden",document.hidden)}function h(e){f("stream",e.target.readyState===EventSource.OPEN)}return n&&d&&(u=new EventSource(n.url,{withCredentials:n.withCredentials}),u.addEventListener("open",h),u.addEventListener("error",h),n.event&&u.addEventListener(n.event,h),f("stream",u.readyState===EventSource.OPEN)),e&&(c=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&&s){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))}(e).then((e=>f("heartbeat",e)))}),e.retry||1e4)),window.addEventListener("online",l),window.addEventListener("offline",l),window.addEventListener("visibilitychange",v),()=>{window.removeEventListener("online",l),window.removeEventListener("offline",l),window.removeEventListener("visibilitychange",v),u&&(u.removeEventListener("open",h),u.removeEventListener("error",h),n.event&&u.removeEventListener(n.event,h)),clearInterval(c)}}))}export{a as statusable}; //# sourceMappingURL=statusable.min.mjs.map