forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage-adaptor.ts
More file actions
42 lines (34 loc) · 1.74 KB
/
Copy pathmessage-adaptor.ts
File metadata and controls
42 lines (34 loc) · 1.74 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
import { trySendMsg2Runtime } from '@api/sw/common'
import { hasDailyLimited, hasWeeklyLimited, matches } from "@util/limit"
import type { LimitReason, ModalContext, Processor } from '../types'
const cvtItem2AddReason = (item: tt4b.limit.Item, delayDuration: number): LimitReason[] => {
const { cond, allowDelay, id, delayCount, weeklyDelayCount } = item
const reasons2Add: LimitReason[] = []
hasDailyLimited(item, delayDuration) && reasons2Add.push({ type: "DAILY", cond, allowDelay, id, delayCount })
hasWeeklyLimited(item, delayDuration) && reasons2Add.push({ type: 'WEEKLY', cond, allowDelay, id, delayCount: weeklyDelayCount })
return reasons2Add
}
class MessageAdaptor implements Processor {
constructor(private readonly context: ModalContext, private readonly delayDuration: number) { }
onLimitChanged(): void {
this.initRules()
}
onLimitTimeMeet(items: tt4b.limit.Item[]): void {
if (!items.length) return
items.filter(({ cond }) => matches(cond, this.context.url))
.flatMap(item => cvtItem2AddReason(item, this.delayDuration))
.forEach(reason => this.context.modal.addReason(reason))
}
async init(): Promise<void> {
this.initRules()
this.context.modal.addDelayHandler(() => this.initRules())
}
async initRules(): Promise<void> {
this.context.modal.removeReasonsByType('DAILY', 'WEEKLY')
const limitedRules = await trySendMsg2Runtime('limit.list', { limited: true, effective: true, url: this.context.url })
if (!limitedRules?.length) return
const reasons = limitedRules.flatMap(item => cvtItem2AddReason(item, this.delayDuration))
this.context.modal.addReason(...reasons)
}
}
export default MessageAdaptor