forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit-database.ts
More file actions
151 lines (137 loc) · 3.8 KB
/
limit-database.ts
File metadata and controls
151 lines (137 loc) · 3.8 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import BaseDatabase from "./common/base-database"
import { REMAIN_WORD_PREFIX } from "./common/constant"
const KEY = REMAIN_WORD_PREFIX + 'LIMIT'
type ItemValue = {
/**
* Limited time, second
*/
t: number
/**
* Limited time per visit, second
*/
v?: number
/**
* Forbiden periods
*/
p?: [number, number][]
/**
* Enabled flag
*/
e: boolean
/**
* Latest date
*/
d: string
/**
* Wasted time, milliseconds
*/
w: number
/**
* Allow to delay
* @since 0.4.0
*/
ad: boolean
}
type Item = {
[cond: string]: ItemValue
}
function migrate(exist: Item, toMigrate: any) {
Object.entries(toMigrate).forEach(([cond, value]) => {
// Not rewrite
if (exist[cond]) return
const itemValue: ItemValue = value as ItemValue
const { t, e, ad, d, w, v, p } = itemValue
exist[cond] = { t, e: !!e, ad: !!ad, d, w: w || 0, v, p }
})
}
/**
* Time limit
*
* @since 0.2.2
*/
class LimitDatabase extends BaseDatabase {
private async getItems(): Promise<Item> {
const result = await this.storage.get(KEY)
const items: Item = result[KEY] || {}
return items
}
private update(items: Item): Promise<void> {
return this.setByKey(KEY, items)
}
async all(): Promise<timer.limit.Record[]> {
const items = await this.getItems()
return Object.entries(items).map(([cond, info]) => {
const item: ItemValue = info as ItemValue
return {
cond,
time: item.t,
visitTime: item.v,
periods: item.p,
enabled: item.e,
allowDelay: !!item.ad,
wasteTime: item.w,
latestDate: item.d,
} as timer.limit.Record
})
}
async save(data: timer.limit.Rule, rewrite?: boolean): Promise<void> {
const items = await this.getItems()
const { cond, time, enabled, allowDelay, visitTime, periods } = data
const existItem = items[cond]
if (existItem) {
if (!rewrite) {
// Not rewrite
return
}
// Rewrite
existItem.t = time
existItem.e = enabled
existItem.ad = allowDelay
existItem.v = visitTime
existItem.p = periods
} else {
// New one
items[cond] = { t: time, e: enabled, ad: allowDelay, w: 0, d: '', v: visitTime, p: periods }
}
await this.update(items)
}
async remove(cond: string): Promise<void> {
const items = await this.getItems()
delete items[cond]
this.update(items)
}
async updateWaste(date: string, toUpdate: { [cond: string]: number }): Promise<void> {
const items = await this.getItems()
Object.entries(toUpdate).forEach(([cond, waste]) => {
const entry = items[cond]
if (!entry) return
entry.d = date
entry.w = waste
})
this.update(items)
}
async updateDelay(cond: string, allowDelay: boolean) {
const items = await this.getItems()
if (!items[cond]) {
return
}
items[cond].ad = allowDelay
await this.update(items)
}
async importData(data: any): Promise<void> {
const toImport = data[KEY]
// Not import
if (typeof toImport !== 'object') return
const result = await this.storage.get(KEY)
const exists: Item = result[KEY] || {}
migrate(exists, toImport)
this.setByKey(KEY, exists)
}
}
export default LimitDatabase