forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily-processor.ts
More file actions
52 lines (46 loc) · 2.01 KB
/
daily-processor.ts
File metadata and controls
52 lines (46 loc) · 2.01 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
import { matches } from "@util/limit"
import { LimitReason, ModalContext, Processor } from "./common"
import { sendMsg2Runtime } from "@api/chrome/runtime"
class DailyProcessor implements Processor {
private context: ModalContext
constructor(context: ModalContext) {
this.context = context
}
handleMsg(code: timer.mq.ReqCode, data: unknown): timer.mq.Response | Promise<timer.mq.Response> {
let items = data as timer.limit.Item[]
if (code === "limitTimeMeet") {
if (!items?.length) {
return { code: "fail" }
}
items.filter(item => matches(item, this.context.url))
.forEach(item => {
const reason: LimitReason = { type: "DAILY", cond: item.cond, allowDelay: item.allowDelay }
this.context.modal.addReason(reason)
})
return { code: "success" }
} else if (code === "limitChanged") {
this.context.modal.removeReasonsByType("DAILY")
items?.forEach(item => {
const reason: LimitReason = { type: "DAILY", cond: item.cond, allowDelay: item.allowDelay }
this.context.modal.addReason(reason)
})
return { code: "success" }
} else if (code === "limitWaking") {
items?.forEach(item => {
const reason: LimitReason = { type: "DAILY", cond: item.cond, allowDelay: item.allowDelay }
this.context.modal.removeReason(reason)
})
return { code: "success" }
}
return { code: "ignore" }
}
async init(): Promise<void> {
const limitedRules: timer.limit.Item[] = await sendMsg2Runtime('cs.getLimitedRules', this.context.url)
if (!limitedRules?.length) return
limitedRules?.forEach(item => {
const reason: LimitReason = { type: "DAILY", cond: item.cond, allowDelay: item.allowDelay }
this.context.modal.addReason(reason)
})
}
}
export default DailyProcessor