Commit 0ea00e76 authored by fanxuehui's avatar fanxuehui

refactor:

parent 152a8893
......@@ -2274,7 +2274,8 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
......@@ -2295,12 +2296,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
......@@ -2315,17 +2318,20 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
......@@ -2442,7 +2448,8 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"ini": {
"version": "1.3.5",
......@@ -2454,6 +2461,7 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
......@@ -2468,6 +2476,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
......@@ -2475,12 +2484,14 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
......@@ -2499,6 +2510,7 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
......@@ -2579,7 +2591,8 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
......@@ -2591,6 +2604,7 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
......@@ -2676,7 +2690,8 @@
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
......@@ -2712,6 +2727,7 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
......@@ -2731,6 +2747,7 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
......@@ -2774,12 +2791,14 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
}
}
},
......
export interface MProps {
system: string;
userIdentifier: string;
env: "dev" | "prod";
log: boolean;
}
export declare class Monitor {
constructor(props: MProps);
}
\ No newline at end of file
import Monitor from "./lib/monitor";
export default Monitor;
export default new Monitor();
import * as rrweb from "rrweb";
import TracksWorker from "./tracks.worker";
import { getPureHash } from "./utils";
class Monitor {
rrwebHandler = null;
initialized = false;
listenedRoute = false;
tracksWorker = new TracksWorker();
init({ system, userIdentifier, env = "prod", log = false }) {
init({ system, userIdentifier, env = "prod", log = false, path = "/" }) {
if (!system || !userIdentifier) {
console.error("system,userIdentifier是必传的");
}
......@@ -14,6 +14,7 @@ class Monitor {
this.tracksWorker.postMessage({
type: "init",
payload: {
path,
system,
userIdentifier,
env,
......@@ -51,7 +52,13 @@ class Monitor {
listenOnHash() {
if (this.listenedRoute) return;
let timer = null;
let preHash = "";
window.addEventListener("hashchange", () => {
// 忽略hash后的query
let hash = getPureHash(location.hash);
if (hash === preHash) return;
preHash = hash;
//防抖,防止无效的频繁触发
clearTimeout(timer);
timer = setTimeout(() => {
this.reset({ path: location.hash });
......@@ -80,4 +87,4 @@ class Monitor {
}
}
export default new Monitor();
export default Monitor;
......@@ -10,6 +10,7 @@ class Reporter {
userIdentifier: "",
path: "",
};
recordKey = "";
environmentUrl = ""; // 环境配置
isUploading = false; // 是否在上传cdn
bus = []; // 上传分片数据
......@@ -19,13 +20,13 @@ class Reporter {
this.baseInfo = baseInfo;
this.setEnvironment(env);
}
// 设置参数内容
setData(dataObj) {
// 设置基础信息 系统名 + 用户标示 + 当前访问路径
// 更新数据
updateData(data) {
this.baseInfo = {
...this.baseInfo,
...dataObj,
...data,
};
this.generateKey();
}
// 设置系统环境变量
......@@ -127,8 +128,15 @@ class Reporter {
}
}
generateKey() {
const { path, userIdentifier } = this;
this.recordKey = md5(path + userIdentifier + Date.parse(new Date()));
let { path, userIdentifier } = this.baseInfo;
let userIdentifierStr = "";
if (typeof userIdentifier !== "string") {
userIdentifierStr = JSON.stringify(userIdentifier);
} else {
userIdentifierStr = userIdentifier;
}
let str = path + userIdentifierStr + Date.now().valueOf();
this.recordKey = md5(str);
}
// 重置数据(分条使用)
reset() {
......
......@@ -31,7 +31,7 @@ onmessage = ({ data: { type, payload } }) => {
break;
case "reset":
// todo : 重置参数,重新生成recordKey
reporter.setData(payload);
reporter.updateData(payload);
reporter.reset();
log("path", reporter.baseInfo.path);
log("分片id", reporter.recordKey);
......
......@@ -9,3 +9,10 @@ export const dataWrapper = (extraData, event) => {
track: event,
};
};
export const is = (type, target) => {
return Object.prototype.toString.apply(target) === `[object ${type}]`;
};
export const getPureHash = hash => {
return hash.split("?")[0];
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment