forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiod-processor.ts
More file actions
55 lines (48 loc) · 2.12 KB
/
period-processor.ts
File metadata and controls
55 lines (48 loc) · 2.12 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
import { sendMsg2Runtime } from "@api/chrome/runtime"
import { date2Idx } from "@util/limit"
import { MILL_PER_SECOND } from "@util/time"
import { type LimitReason, type ModalContext, type Processor } from "../common"
function processRule(rule: timer.limit.Rule, nowSeconds: number, context: ModalContext): NodeJS.Timeout[] {
const { cond, periods, id } = rule
return periods?.flatMap?.(p => {
const [s, e] = p
const startSeconds = s * 60
const endSeconds = (e + 1) * 60
const reason: LimitReason = { id, cond, type: "PERIOD" }
const timers: NodeJS.Timeout[] = []
if (nowSeconds < startSeconds) {
timers.push(setTimeout(() => context.modal.addReason(reason), (startSeconds - nowSeconds) * MILL_PER_SECOND))
timers.push(setTimeout(() => context.modal.removeReason(reason), (endSeconds - nowSeconds) * MILL_PER_SECOND))
} else if (nowSeconds >= startSeconds && nowSeconds <= endSeconds) {
context.modal.addReason(reason)
timers.push(setTimeout(() => context.modal.removeReason(reason), (endSeconds - nowSeconds) * MILL_PER_SECOND))
}
return timers
}) ?? []
}
class PeriodProcessor implements Processor {
private context: ModalContext
private timers: NodeJS.Timeout[] = []
constructor(context: ModalContext) {
this.context = context
}
async handleMsg(code: timer.mq.ReqCode, data: timer.limit.Item[]): Promise<timer.mq.Response> {
if (code === "limitChanged") {
this.timers?.forEach(clearTimeout)
await this.init0(data)
return { code: "success" }
}
return { code: "ignore" }
}
init(): Promise<void> {
return this.init0()
}
private async init0(rules?: timer.limit.Item[]) {
rules = rules || await sendMsg2Runtime("cs.getRelatedRules", this.context.url)
// Clear first
this.context.modal.removeReasonsByType("PERIOD")
const nowSeconds = date2Idx(new Date())
this.timers = rules?.flatMap?.(r => processRule(r, nowSeconds, this.context)) || []
}
}
export default PeriodProcessor