workbox-window
Version:
Simplifies communications with Workbox packages running in the service worker
3 lines (2 loc) • 3.34 kB
JavaScript
try{self["workbox:window:6.2.4"]&&_()}catch(t){}function t(t,s){return new Promise((i=>{const e=new MessageChannel;e.port1.onmessage=t=>{i(t.data)},t.postMessage(s,[e.port2])}))}try{self["workbox:core:6.2.4"]&&_()}catch(t){}class s{constructor(){this.promise=new Promise(((t,s)=>{this.resolve=t,this.reject=s}))}}function i(t,s){const{href:i}=location;return new URL(t,i).href===new URL(s,i).href}class e{constructor(t,s){this.type=t,Object.assign(this,s)}}const h={type:"SKIP_WAITING"};class n extends class{constructor(){this.St=new Map}addEventListener(t,s){this.Lt(t).add(s)}removeEventListener(t,s){this.Lt(t).delete(s)}dispatchEvent(t){t.target=this;const s=this.Lt(t.type);for(const i of s)i(t)}Lt(t){return this.St.has(t)||this.St.set(t,new Set),this.St.get(t)}}{constructor(t,h={}){super(),this.It={},this.Bt=0,this.Tt=new s,this.Ut=new s,this.Mt=new s,this.At=0,this.Gt=new Set,this.Kt=()=>{const t=this.Nt,s=t.installing;this.Bt>0||!i(s.scriptURL,this.zt.toString())||performance.now()>this.At+6e4?(this.Dt=s,t.removeEventListener("updatefound",this.Kt)):(this.Ft=s,this.Gt.add(s),this.Tt.resolve(s)),++this.Bt,s.addEventListener("statechange",this.Ht)},this.Ht=t=>{const s=this.Nt,i=t.target,{state:h}=i,n=i===this.Dt,a={sw:i,isExternal:n,originalEvent:t};!n&&this.Jt&&(a.isUpdate=!0),this.dispatchEvent(new e(h,a)),"installed"===h?this.Qt=self.setTimeout((()=>{"installed"===h&&s.waiting===i&&this.dispatchEvent(new e("waiting",a))}),200):"activating"===h&&(clearTimeout(this.Qt),n||this.Ut.resolve(i))},this.Vt=t=>{const s=this.Ft,i=s!==navigator.serviceWorker.controller;this.dispatchEvent(new e("controlling",{isExternal:i,originalEvent:t,sw:s,isUpdate:this.Jt})),i||this.Mt.resolve(s)},this.Xt=async t=>{const{data:s,ports:i,source:h}=t;await this.getSW(),this.Gt.has(h)&&this.dispatchEvent(new e("message",{data:s,originalEvent:t,ports:i,sw:h}))},this.zt=t,this.It=h,navigator.serviceWorker.addEventListener("message",this.Xt)}async register({immediate:t=!1}={}){t||"complete"===document.readyState||await new Promise((t=>window.addEventListener("load",t))),this.Jt=Boolean(navigator.serviceWorker.controller),this.Yt=this.Zt(),this.Nt=await this.ts(),this.Yt&&(this.Ft=this.Yt,this.Ut.resolve(this.Yt),this.Mt.resolve(this.Yt),this.Yt.addEventListener("statechange",this.Ht,{once:!0}));const s=this.Nt.waiting;return s&&i(s.scriptURL,this.zt.toString())&&(this.Ft=s,Promise.resolve().then((()=>{this.dispatchEvent(new e("waiting",{sw:s,wasWaitingBeforeRegister:!0}))})).then((()=>{}))),this.Ft&&(this.Tt.resolve(this.Ft),this.Gt.add(this.Ft)),this.Nt.addEventListener("updatefound",this.Kt),navigator.serviceWorker.addEventListener("controllerchange",this.Vt),this.Nt}async update(){this.Nt&&await this.Nt.update()}get active(){return this.Ut.promise}get controlling(){return this.Mt.promise}getSW(){return void 0!==this.Ft?Promise.resolve(this.Ft):this.Tt.promise}async messageSW(s){return t(await this.getSW(),s)}messageSkipWaiting(){this.Nt&&this.Nt.waiting&&t(this.Nt.waiting,h)}Zt(){const t=navigator.serviceWorker.controller;return t&&i(t.scriptURL,this.zt.toString())?t:void 0}async ts(){try{const t=await navigator.serviceWorker.register(this.zt,this.It);return this.At=performance.now(),t}catch(t){throw t}}}export{n as Workbox,e as WorkboxEvent,t as messageSW};
//# sourceMappingURL=workbox-window.prod.mjs.map