Skip to content

Commit

Permalink
chore: repo overall update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 19, 2024
1 parent f8c3632 commit b7e5400
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/canyon-report/src/canyon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export function init() {
// @ts-ignore
window.__canyon__analytics__dsn__ = `https://collect.canyonjs.org/coverage/client`
setTimeout(()=>{
// @ts-ignore
if (window.__coverage__ && window.__canyon__analytics__dsn__) {
// @ts-ignore
function collectCoverageData(timing) {
// @ts-ignore
const first = Object.values(window.__coverage__)
// @ts-ignore
const {projectID,sha,instrumentCwd} = first[0]

const data = new FormData();
// @ts-ignore
data.append('coverage',JSON.stringify(Object.entries(window.__coverage__).map(([path, {b,f,s}]) => ({
path,
b,
f,
s,
})).reduce((acc, {path, b, f, s}) => {
// @ts-ignore
acc[path] = {b, f, s};
return acc;
}, {})))
// @ts-ignore
data.append('projectID',projectID)
// @ts-ignore
data.append('sha',sha)
data.append('instrumentCwd',instrumentCwd)

navigator.sendBeacon(
// @ts-ignore
window.__canyon__analytics__dsn__,
data,
);
}

// beforeunload 事件:当用户即将离开页面时触发
window.addEventListener("beforeunload", () => {
collectCoverageData("beforeunload");
});

// unload 事件:在页面完全卸载前触发
window.addEventListener("unload", () => {
collectCoverageData("unload");
});

// visibilitychange 事件:当页面变为不可见(如切换到其他标签页)时触发
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
collectCoverageData("visibilitychange");
}
});
}
},500)
}
2 changes: 2 additions & 0 deletions packages/canyon-report/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import {init as canyonInit} from './canyon.ts'

canyonInit()
// @ts-ignore
window.__canyon__ = {
"dsn": "https://canyon-collect-three.vercel.app/coverage/client",
Expand Down

0 comments on commit b7e5400

Please sign in to comment.