-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
1 lines (1 loc) · 996 Bytes
/
sw.js
File metadata and controls
1 lines (1 loc) · 996 Bytes
1
var i="https://api.hey.xyz/leafwatch/impressions",o="https://api.hey.xyz/leafwatch/events";var t=new Set,a,s=[],r=async()=>{if(t.size===0)return;let e=Array.from(t);t.clear();try{await fetch(i,{body:JSON.stringify({ids:e}),headers:{"Content-Type":"application/json"},keepalive:!0,method:"POST"})}catch(n){console.error("Failed to send visible posts to Leafwatch",n)}},d=async()=>{if(s.length===0)return;let e=[...s];s=[];try{await fetch(o,{body:JSON.stringify({events:e}),headers:{"Content-Type":"application/json","X-Identity-Token":a},keepalive:!0,method:"POST"})}catch(n){console.error("Failed to send recorded events to Leafwatch",n)}};setInterval(r,5e3);setInterval(d,5e3);var c=async()=>{await self.clients.claim()};self.addEventListener("message",e=>{e.data?.type==="POST_IMPRESSION"&&t.add(e.data.id),e.data?.type==="EVENT"&&(a=e.data.identityToken,s.push(e.data.event))});self.addEventListener("install",()=>{self.skipWaiting()});self.addEventListener("activate",e=>{e.waitUntil(c())});