forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassic.ts
More file actions
66 lines (56 loc) · 1.82 KB
/
classic.ts
File metadata and controls
66 lines (56 loc) · 1.82 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
import BaseDatabase from '../common/base-database'
import { REMAIN_WORD_PREFIX } from '../common/constant'
import type { TimelineCondition, TimelineDatabase } from './types'
const DB_KEY = REMAIN_WORD_PREFIX + 'TL'
type Item = {
// start
s: number
// duration
d: number
}
type TimelineData = {
[date: string]: {
[host: string]: Item[]
}
}
function filter(ticks: timer.timeline.Tick[], cond?: TimelineCondition): timer.timeline.Tick[] {
if (!cond) {
return ticks
}
const { start, host } = cond
return ticks.filter(tick => {
if (start && tick.start < start) {
return false
}
if (host && tick.host !== host) {
return false
}
return true
})
}
/**
* @deprecated Use IDBTimelineDatabase instead, this is for old version data migration
*/
export default class ClassicTimelineDatabase extends BaseDatabase implements TimelineDatabase {
private async getData(): Promise<TimelineData> {
const data = await this.storage.getOne<TimelineData>(DB_KEY)
return data ?? {}
}
async batchSave(_ticks: timer.timeline.Tick[]): Promise<void> {
console.warn("ClassicTimelineDatabase is deprecated, data will not be saved to it. This invoking is not expected")
return
}
async select(cond?: TimelineCondition): Promise<timer.timeline.Tick[]> {
const data = await this.getData()
const ticks: timer.timeline.Tick[] = []
Object.values(data).forEach(hostData => {
Object.entries(hostData).forEach(([host, items]) => {
items.forEach(({ s: start, d: duration }) => ticks.push({ host, start, duration }))
})
})
return filter(ticks, cond)
}
async clear(): Promise<void> {
await this.storage.remove(DB_KEY)
}
}