1 | !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(n="undefined"!=typeof globalThis?globalThis:n||self).stagnant=e()}(this,(function(){"use strict";function n(n={}){n={onevent:function(){},onerror:function(){},onsuccess:function(){},onflush:function(){},console:{log(){},error(){},warn(){}},generateId:function(){return Math.random().toString(15).slice(2,8)},...n};const{generateId:e}=n;async function t(e){await n.onevent(e),e.error?await n.onerror(e):await n.onsuccess(e)}function a({parentId:n,id:e,startTime:t,endTime:a,name:r,data:o,error:c}){return{parentId:n,id:e,startTime:t,endTime:a,name:r,data:o,error:c}}function r(o){function c({sync:c},...i){const{data:l,callback:s,name:u}=function(...n){const e=n.findIndex((n=>"function"==typeof n));let t=n[e],a=e>0?n.slice(0,e):n,[r,o]=a;return"string"!=typeof r&&(o=r,t&&(r=o.name||t.toString().replace(/\s/g,"").replace(/(.)*=>/,""))),null==o&&(o={}),{name:r,data:o,callback:t}}(...i),{event:d,childP:f}=s?function({parentEvent:n,name:t,data:o,sync:c}){const i=a({parentId:n.id,id:e(),name:t,startTime:null,endTime:null,data:{...n.data||{},...o},error:null,sync:n.sync||c});return{childP:r(i),event:i}}({parentEvent:o,name:u,data:l,sync:c}):{};return s&&c?function({callback:e,name:a,event:r,childP:o}){try{r.startTime=Date.now();const c=e(o);return r.endTime=Date.now(),null!=c&&"then"in c&&n.console.warn(a,"A call to trace.sync was made but the response was async. This is likely a mistake and should be corrected."),c}catch(n){throw r.endTime=Date.now(),r.error=n,n}finally{t(r).catch((e=>n.console.error("Failed to dispatch event",e)))}}({data:l,callback:s,childP:f,name:u,event:d}):s&&!c?async function({event:e,childP:a,callback:r}){if(o.sync)throw new Error("Cannot use an async trace within a synchronous trace");try{e.startTime=Date.now();const o=await r(a);return e.endTime=Date.now(),o}catch(n){throw e.endTime=Date.now(),e.error=n,n}finally{t(e).catch((e=>n.console.error("Failed to dispatch event",e)))}}({data:l,callback:s,childP:f,name:u,event:d}):function({data:n}){return o.data={...o.data,...n},null}({data:l})}async function i(...n){return await c({sync:!1},...n)}return i.sync=function(...n){return c({sync:!0},...n)},i}let o=function(){const r=a({parentId:null,id:e(),startTime:Date.now(),endTime:Date.now(),error:null,data:{}});return r.flush=async function(){return delete r.flush,r.endTime=Date.now(),await t(r),await n.onflush(r),r},r}(),c=r(o);return c.flush=o.flush,c.config=n,c}function e(n,...t){const a=t.find((n=>"function"==typeof n));return n?n(...t):a?a(((...n)=>e(null,...n))):null}return n.call=e,n.ensure=function(n){return n||((...n)=>e(null,...n))},n}));
|