-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathrun-time.ts
More file actions
54 lines (45 loc) · 1.38 KB
/
run-time.ts
File metadata and controls
54 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { onRuntimeMessage, trySendMsg2Runtime } from "@api/chrome/runtime"
class RunTimeTracker {
private start: number = Date.now()
private url: string
// Real host, including builtin hosts
private host: string | undefined
constructor(url: string) {
this.url = url
this.start = Date.now()
}
init(): void {
this.fetchSite()
onRuntimeMessage<void, void>(async req => {
if (req.code === 'siteRunChange') {
this.fetchSite()
return { code: 'success' }
}
return { code: 'ignore' }
})
setInterval(() => this.collect(), 1000)
}
private async fetchSite() {
const site: timer.site.SiteKey | undefined = await trySendMsg2Runtime('cs.getRunSites', this.url)
this.host = site?.host
}
private async collect() {
const now = Date.now()
const lastTime = this.start
try {
if (this.host) {
const event: timer.core.Event = {
start: lastTime,
end: now,
url: this.url,
ignoreTabCheck: false,
host: this.host,
}
await trySendMsg2Runtime('cs.trackRunTime', event)
}
this.start = now
} catch {
}
}
}
export default RunTimeTracker