-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserver.ts
More file actions
64 lines (59 loc) · 2.68 KB
/
server.ts
File metadata and controls
64 lines (59 loc) · 2.68 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
55
56
57
58
59
60
61
62
63
64
import { getTab, listTabs, sendMsg2Tab } from "@api/chrome/tab"
import { getWindow } from "@api/chrome/window"
import limitService from "@service/limit-service"
import periodService from "@service/period-service"
import statService from "@service/stat-service"
import { extractHostname, HostInfo } from "@util/pattern"
import badgeTextManager from "../badge-text-manager"
import MessageDispatcher from "../message-dispatcher"
import optionService from "@service/option-service"
let option = null
optionService.getAllOption().then(opt => option = opt)
optionService.addOptionChangeListener(opt => option = opt)
async function handleTime(hostInfo: HostInfo, url: string, dateRange: [number, number]): Promise<number> {
const host = hostInfo.host
const [start, end] = dateRange
const focusTime = end - start
// 1. Save async
await statService.addFocusTime({ [host]: { [url]: focusTime } })
// 2. Process limit
const meedLimits = await limitService.addFocusTime(host, url, focusTime)
// If time limited after this operation, send messages
meedLimits && meedLimits.length && sendLimitedMessage(meedLimits)
// 3. Add period time
await periodService.add(start, focusTime)
return focusTime
}
async function handleEvent(event: timer.stat.Event, sender: ChromeMessageSender): Promise<void> {
const { url, start, end, ignoreTabCheck } = event
const windowId = sender?.tab?.windowId
const tabId = sender?.tab?.id
if (!ignoreTabCheck) {
if (!windowId || !tabId) return
const window = await getWindow(windowId)
if (!window?.focused) return
const tab = await getTab(tabId)
if (!tab?.active) return
}
const hostInfo = extractHostname(url)
if (hostInfo.protocol === "file" && !option.countLocalFiles) return
await handleTime(hostInfo, url, [start, end])
if (tabId) {
const winTabs = await listTabs({ active: true, windowId })
const firstActiveTab = winTabs?.[0]
// Cause there is no way to determine whether this tab is selected in screen-split mode
// So only show badge for first tab for screen-split mode
// @see #246
firstActiveTab?.id === tabId && badgeTextManager.forceUpdate({ tabId, url })
}
}
async function sendLimitedMessage(item: timer.limit.Item[]) {
const tabs = await listTabs({ status: 'complete' })
tabs.forEach(tab => sendMsg2Tab(tab.id, 'limitTimeMeet', item)
.then(() => console.log(`Processed limit rules: rule=${JSON.stringify(item)}`))
.catch(() => {/*Ignored*/ })
)
}
export default function initServer(messageDispatcher: MessageDispatcher) {
messageDispatcher.register<timer.stat.Event, void>('cs.trackTime', handleEvent)
}