@cmtlyt/monitor
Version:
2 lines (1 loc) • 3.36 kB
JavaScript
import{pick_ as y}from"@cmtlyt/base/fp/utils";import{createLogger as F}from"@cmtlyt/logger";import{onTTFB as S,onFCP as k,onLCP as B,onCLS as I,onINP as P}from"web-vitals";class ${head=null;tail=null;listenerSet=new Set;push(e){if(h("isUnload"))return console.warn("\u9875\u9762\u5DF2\u5378\u8F7D, \u65E0\u6CD5\u7EE7\u7EED\u8BB0\u5F55\u65E5\u5FD7, \u540E\u7EED\u4F1A\u5BF9\u6B64\u7C7B\u65E5\u5FD7\u8FDB\u884C\u6301\u4E45\u5316\u5904\u7406");const n={data:e,next:null};this.listenerSet.forEach(o=>o({...e,extra:{...e.extra,from:"monitor"}})),!this.listenerSet.size&&(this.head?this.tail&&(this.tail.next=n,this.tail=n):(this.head=n,this.tail=n))}pop(){const e=this.head?.data;return this.head=this.head?.next,e}viewCache(){const e=[];let n=this.head;for(;n;)e.push(n.data),n=n.next;return e}popAll(){const e=[];for(;this.head;)e.push(this.pop());return e.filter(Boolean)}onChange(e){this.listenerSet.add(e)}}const s=new $,m=Object.create(null),c={};function p(t,e){c[t]=e}function h(t){return t?m[t]:m}function v(t,e){m[t]=e}function L(t){s.push({kind:"systemError",message:t.message,extra:{timestamp:Date.now()}})}function w(t){s.push({kind:"systemError",message:t.reason.toString(),extra:{timestamp:Date.now()}})}function b(){window.addEventListener("error",L),window.addEventListener("unhandledrejection",w),p("error",L),p("unhandledrejection",w)}function j(t){const{loggerOptions:e,reportStrategy:n,reportLog:o,formatLogInfo:i=r=>r,formatReportInfo:f=r=>r}=t,{getPrintFunc:u}=e;return F({...e,getPrintFunc:u?.bind(t),onLogBefore(r){try{const a=i.call(t,{config:t,logInfo:r});if(!((n&&n.call(t,a))??!0))return;const d=f.call(t,a);o&&o.call(t,d)}catch(a){if(r.kind==="error")return console.error("formatLogInfo error",a);s.push({kind:"error",message:"formatLogInfo error",extra:{timestamp:Date.now(),error:a}})}}})}function l(t){s.push({kind:"system",message:"performanceInfo",extra:{timestamp:Date.now(),...y(["name","value","rating","delta","navigationType"],t)}})}function A(){S(l),k(l),B(l),I(l),P(l)}function M(t){s.popAll().forEach(e=>{t[e.kind](e.message,e.extra)})}function T(t){s.onChange(e=>{t[e.kind](e.message,e.extra)})}function C(t){if(!t)return"";if(t.id)return`#${t.id}`;let e=t.className;if(e)try{typeof e!="string"?e="":e=`.${e.split(" ").join(".")}`}catch{e=""}else if(t.parentElement){const o=Array.from(t.parentElement.children).indexOf(t),i=t.tagName.toLowerCase();e=`${i}${i==="body"?"":`:nth-child(${o+1})`}`}const n=C(t.parentElement);return n?`${n}>${e}`:e}const x=["click"];function N(t=window,e){const n=h("monitorConfig"),{events:o=x,needListenCapture:i=!1,generateExtra:f}=e;(o||x).forEach(u=>{const r=a=>{const d=g=>{const E={timestamp:Date.now(),isCapture:i,selector:C(g.target)},D=typeof f=="function"?f.call(n,{event:g,systemExtra:E}):{};s.push({kind:"event",message:u,extra:{...E,...D},...a})};return p(u,d),d};i&&t.addEventListener(u,r({capture:!0}),{capture:!0}),t.addEventListener(u,r({capture:!1}))})}window.addEventListener("unload",()=>{v("isUnload",!0);const{rootElement:t}=h("monitorConfig");for(const e in c)t.removeEventListener(e,c[e],{capture:!0}),t.removeEventListener(e,c[e]),window.removeEventListener(e,c[e],{capture:!0}),window.removeEventListener(e,c[e])},{once:!0}),b();function O(t){v("monitorConfig",t),A(),N(t.rootElement,t.listenEventConfig);const e=j(t);return M(e),T(e),e}export{O as createMonitor};