-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathnormal.ts
More file actions
95 lines (85 loc) · 3.7 KB
/
normal.ts
File metadata and controls
95 lines (85 loc) · 3.7 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { getTab, listTabs, sendMsg2Tab } from "@api/chrome/tab"
import { getWindow } from "@api/chrome/window"
import optionHolder from "@service/components/option-holder"
import itemService, { type ItemIncContext } from "@service/item-service"
import limitService from "@service/limit-service"
import periodThrottler from '@service/throttler/period-throttler'
import whitelistHolder from "@service/whitelist/holder"
import { IS_ANDROID } from "@util/constant/environment"
import { extractHostname } from "@util/pattern"
import badgeManager from "../badge-manager"
async function handleTime(context: ItemIncContext, timeRange: [number, number], tabId: number | undefined): Promise<number> {
const { host, url } = context
const [start, end] = timeRange
const focusTime = end - start
// 1. Save async
await itemService.addFocusTime(context, focusTime)
// 2. Process limit
const { limited, reminder } = await limitService.addFocusTime(host, url, focusTime)
// If time limited after this operation, send messages
limited?.length && sendLimitedMessage(limited)
// If need to reminder, send messages
reminder?.items?.length && tabId && sendMsg2Tab(tabId, 'limitReminder', reminder)
// 3. Add period time
periodThrottler.add(start, focusTime)
return focusTime
}
export async function handleTrackTimeEvent(event: timer.core.Event, senderTab: ChromeTab | undefined): Promise<void> {
const { url, start, end, ignoreTabCheck } = event
const { id: tabId, windowId, groupId } = senderTab ?? {}
if (!ignoreTabCheck) {
if (await windowNotFocused(windowId)) return
if (await tabNotActive(tabId)) return
}
const { protocol, host } = extractHostname(url) || {}
const option = await optionHolder.get()
if (protocol === "file" && !option?.countLocalFiles) return
if (whitelistHolder.contains(host, url)) return
await handleTime({ host, url, groupId }, [start, end], tabId)
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 && badgeManager.updateFocus({ tabId, url })
}
}
async function windowNotFocused(winId: number | undefined): Promise<boolean> {
if (IS_ANDROID) return false
if (!winId) return true
const window = await getWindow(winId)
return !window?.focused
}
async function tabNotActive(tabId: number | undefined): Promise<boolean> {
if (!tabId) return true
const tab = await getTab(tabId)
return !tab?.active
}
async function sendLimitedMessage(items: timer.limit.Item[]) {
const tabs = await listTabs()
if (!tabs?.length) return
for (const tab of tabs) {
try {
const { id } = tab
id && await sendMsg2Tab(id, 'limitTimeMeet', items)
} catch {
/* Ignored */
}
}
}
async function handleVisit(context: ItemIncContext) {
await itemService.increaseVisit(context)
const { host, url } = context
const metLimits = await limitService.incVisit(host, url)
// If time limited after this operation, send messages
metLimits?.length && sendLimitedMessage(metLimits)
}
export async function handleIncVisitEvent(param: { host: string, url: string }, sender: ChromeMessageSender): Promise<void> {
const { host, url } = param
const { groupId } = sender?.tab ?? {}
const { protocol } = extractHostname(url)
const option = await optionHolder.get()
if (protocol === "file" && !option.countLocalFiles) return
await handleVisit({ host, url, groupId })
}