UNPKG

datadog-ux-utils

Version:

Datadog RUM focused UX & performance toolkit: API guards (retry, breaker, rate), React telemetry (error boundary, profiler, Suspense), web vitals & resource observers, offline queues.

3 lines (2 loc) 3.45 kB
"use strict";const a=require("./datadog-DV4lp5AO.cjs");let s=[],E=200,l=!1;function T(e=200){return l||(l=!0,E=e,globalThis.__dd_addAction=a.addAction,globalThis.__dd_addError=a.addError,globalThis.addAction=function(n,r,t){navigator.onLine||_({type:"action",name:n,attrs:r,sampleRate:t})},globalThis.addError=function(n,r,t){navigator.onLine||_({type:"error",err:n,context:r,sampleRate:t})},window.addEventListener("online",y)),g}function g(){l&&(l=!1,globalThis.addAction=globalThis.__dd_addAction,globalThis.addError=globalThis.__dd_addError,window.removeEventListener("online",y),s=[])}function _(e){s.length>=E&&s.shift(),s.push(e)}function y(){if(!navigator.onLine||!s.length)return;const e=s.slice();s=[];for(const n of e)n.type==="action"?a.addAction(n.name,n.attrs,n.sampleRate):a.addError(n.err,n.context,n.sampleRate)}const p={storageKey:"dd_offline_queue_v1",maxBuffered:400,byteCap:15e5,flushOnInit:!0,writeDebounceMs:150};let o,i=[],u=!1,f=null;function d(){const e=globalThis;return{sendAction:e.__DD_SEND_ACTION__,sendError:e.__DD_SEND_ERROR__}}function D(e={}){if(u)return h;u=!0,o={...p,...e},i=v(o.storageKey);const n=globalThis;return n.__DD_ENQUEUE_ACTION__=S,n.__DD_ENQUEUE_ERROR__=N,window.addEventListener("online",c),document.addEventListener("visibilitychange",m),o.flushOnInit&&c(),h}function h(){if(!u)return;u=!1;const e=globalThis;delete e.__DD_ENQUEUE_ACTION__,delete e.__DD_ENQUEUE_ERROR__,window.removeEventListener("online",c),document.removeEventListener("visibilitychange",m)}function m(){document.visibilityState==="visible"&&c()}function S(e,n,r){if(navigator.onLine){const{sendAction:t}=d();if(t)return t(e,n,r)}w({t:"a",n:e,a:n,s:r,ts:Date.now()})}function N(e,n,r){if(navigator.onLine){const{sendError:t}=d();if(t)return t(L(e),n,r)}w({t:"e",e:U(e),c:n,s:r,ts:Date.now()})}function w(e){i.push(e),i.length>o.maxBuffered&&i.splice(0,i.length-o.maxBuffered),A(),O()}function O(){f==null&&(f=setTimeout(()=>{f=null,b(o.storageKey,i)},o.writeDebounceMs))}function c(){if(!navigator.onLine||i.length===0)return;const{sendAction:e,sendError:n}=d();if(!e||!n)return;const r=i.slice();for(const t of r){try{t.t==="a"?e(t.n,t.a,t.s):n(J(t.e),t.c,t.s)}catch{break}i.shift()}b(o.storageKey,i)}function v(e){try{const n=localStorage.getItem(e);if(!n)return[];const r=JSON.parse(n);return Array.isArray(r)?r.filter(t=>t&&(t.t==="a"||t.t==="e")):[]}catch{return[]}}function b(e,n){try{localStorage.setItem(e,JSON.stringify(n))}catch{if(n.length>0){n.splice(0,Math.ceil(n.length*.2));try{localStorage.setItem(e,JSON.stringify(n))}catch{}}}}function A(){try{let e=JSON.stringify(i);if(e.length<=o.byteCap)return;for(;i.length&&e.length>o.byteCap;)i.splice(0,Math.max(1,Math.floor(i.length*.1))),e=JSON.stringify(i)}catch{i=[]}}function L(e){if(e instanceof Error)return e;try{return new Error(typeof e=="string"?e:JSON.stringify(e))}catch{return new Error("Unknown error")}}function U(e){if(e instanceof Error)return JSON.stringify({n:e.name,m:e.message,s:e.stack});try{return JSON.stringify(e)}catch{return'"Unknown error"'}}function J(e){try{const n=JSON.parse(e);if(n&&typeof n=="object"&&("m"in n||"message"in n)){const r=new Error(n.m??n.message??"Error");return n.n&&(r.name=n.n),n.s&&(r.stack=n.s),r}return new Error(typeof n=="string"?n:"Error")}catch{return new Error("Error")}}exports.installPersistentTelemetryQueue=D;exports.installTelemetryQueue=T; //# sourceMappingURL=offlineQueue.persistent-CDVTmE70.cjs.map