svelte-statusable
Version:
Super tiny, simple to use SvelteJS store to control your application status.
3 lines (2 loc) • 2.05 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("svelte/store")):"function"==typeof define&&define.amd?define(["exports","svelte/store"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).statusable={},e.store)}(this,(function(e,n){"use strict";const t="undefined"!=typeof window,r="undefined"!=typeof navigator,o="undefined"!=typeof document,i="undefined"!=typeof EventSource,d="undefined"!=typeof AbortController;e.statusable=function({ping:e,sse:a}){let s={online:!r||navigator.onLine,hidden:!!o&&document.hidden,heartbeat:!t,stream:!t};return"string"==typeof e&&(e={url:e,method:"HEAD",cache:"no-cache",credentials:"omit",referrerPolicy:"no-referrer"}),"string"==typeof a&&(a={url:a,withCredentials:!1}),n.readable(s,(n=>{if(!t||!r||!o)return;let u,f;function l(e,t){s[e]!==t&&n(s={...s,[e]:t})}function v(){l("online",navigator.onLine)}function c(){l("hidden",document.hidden)}function h(e){l("stream",e.target.readyState===EventSource.OPEN)}return a&&i&&(u=new EventSource(a.url,{withCredentials:a.withCredentials}),u.addEventListener("open",h),u.addEventListener("error",h),a.event&&u.addEventListener(a.event,h),l("stream",u.readyState===EventSource.OPEN)),e&&(f=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&&d){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=>l("heartbeat",e)))}),e.retry||1e4)),window.addEventListener("online",v),window.addEventListener("offline",v),window.addEventListener("visibilitychange",c),()=>{window.removeEventListener("online",v),window.removeEventListener("offline",v),window.removeEventListener("visibilitychange",c),u&&(u.removeEventListener("open",h),u.removeEventListener("error",h),a.event&&u.removeEventListener(a.event,h)),clearInterval(f)}}))},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.min.js.map