@mxtommy/kip
Version:
An advanced and versatile marine instrumentation package to display Signal K data.
2 lines (1 loc) • 2.56 kB
JavaScript
import{h as m}from"./chunk-2TP7C66X.js";import{Ba as u,Cc as d,G as n,Ga as t,J as h,Qe as o,f as a,la as l,qa as c}from"./chunk-LEY6MANN.js";var w=(()=>{class s{connectionStateMachine=c(m);destroyRef=c(u);probeUrl="https://www.gstatic.com/generate_204";probeTimeoutMs=5e3;healthyRecheckMs=6e4;retryIntervalsMs=[5e3,3e4,6e4];useConnectionStateTriggers=!0;isBrowser=typeof window<"u"&&typeof document<"u";started=!1;retryAttempt=0;checkTimer=null;lastConnectionState=null;isChecking=t(!1);isReachable=t(null);lastSuccessAt=t(null);lastCheckedAt=t(null);lastError=t(null);internetAvailable=d(()=>this.isReachable()===!0);start(){this.started||!this.isBrowser||(this.started=!0,h(n(window,"online"),n(window,"offline")).pipe(o(this.destroyRef)).subscribe(()=>this.recheckNow()),n(document,"visibilitychange").pipe(o(this.destroyRef)).subscribe(()=>{document.visibilityState==="visible"&&this.recheckNow()}),this.useConnectionStateTriggers&&this.connectionStateMachine.status$.pipe(o(this.destroyRef)).subscribe(e=>{e?.state&&e.state!==this.lastConnectionState&&(this.lastConnectionState=e.state,this.recheckNow())}),this.recheckNow())}recheckNow(){this.started&&(this.clearTimer(),this.isChecking()||this.runProbe())}runProbe(){return a(this,null,function*(){this.isChecking.set(!0),this.lastCheckedAt.set(new Date);try{let e=yield this.performProbe();this.isReachable.set(e),e?(this.lastSuccessAt.set(new Date),this.lastError.set(null),this.retryAttempt=0,this.scheduleNext(this.healthyRecheckMs)):(this.lastError.set("Internet reachability probe failed"),this.scheduleRetry())}catch(e){this.isReachable.set(!1),this.lastError.set("Internet reachability probe failed"),this.scheduleRetry()}finally{this.isChecking.set(!1)}})}performProbe(){return a(this,null,function*(){let e=new AbortController,i=setTimeout(()=>e.abort(),this.probeTimeoutMs);try{let r=yield fetch(this.probeUrl,{method:"GET",mode:"no-cors",cache:"no-store",signal:e.signal});return r.type==="opaque"?!0:r.status>=200&&r.status<400}catch(r){return!1}finally{clearTimeout(i)}})}scheduleRetry(){let e=Math.min(this.retryAttempt,this.retryIntervalsMs.length-1),i=this.retryIntervalsMs[e];this.retryAttempt++,this.scheduleNext(i)}scheduleNext(e){this.clearTimer(),this.checkTimer=setTimeout(()=>{this.checkTimer=null,this.runProbe()},e)}clearTimer(){this.checkTimer&&(clearTimeout(this.checkTimer),this.checkTimer=null)}ngOnDestroy(){this.clearTimer()}static \u0275fac=function(i){return new(i||s)};static \u0275prov=l({token:s,factory:s.\u0275fac,providedIn:"root"})}return s})();export{w as a};