Skip to content

Commit 61dfcc4

Browse files
committed
feat: semi
1 parent c714a53 commit 61dfcc4

File tree

5 files changed

+272
-211
lines changed

5 files changed

+272
-211
lines changed

src/database/common/indexed-storage.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,28 @@ export function req2Promise<T = unknown>(req: IDBRequest<T>): Promise<T | undefi
3232
})
3333
}
3434

35+
export async function iterateCursor<T = unknown>(
36+
req: IDBRequest<IDBCursorWithValue | null>
37+
): Promise<readonly T[]>
38+
export async function iterateCursor<T = unknown>(
39+
req: IDBRequest<IDBCursorWithValue | null>,
40+
processor: (cursor: IDBCursorWithValue) => void | Promise<void>
41+
): Promise<void>
42+
3543
export async function iterateCursor<T = unknown>(
3644
req: IDBRequest<IDBCursorWithValue | null>,
3745
processor?: (cursor: IDBCursorWithValue) => void | Promise<void>
38-
): Promise<T[]> {
46+
): Promise<readonly T[] | void> {
47+
const collectResults = !processor
3948
const results: T[] = []
4049

4150
return new Promise((resolve, reject) => {
4251
req.onsuccess = async () => {
4352
const cursor = req.result
44-
if (!cursor) return resolve(results)
53+
if (!cursor) return resolve(collectResults ? results : undefined)
4554

4655
try {
47-
results.push(cursor.value as T)
56+
collectResults && results.push(cursor.value as T)
4857
await processor?.(cursor)
4958
cursor.continue()
5059
} catch (error) {

src/database/stat-database/classic.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ import { REMAIN_WORD_PREFIX } from '@db/common/constant'
33
import { log } from '@src/common/logger'
44
import { escapeRegExp } from '@util/pattern'
55
import { isNotZeroResult } from '@util/stat'
6-
import { cvtGroupId2Host, formatDateStr, GROUP_PREFIX, increase } from './common'
7-
import { filterRow, processCondition } from './filter'
6+
import { cvtGroupId2Host, formatDateStr, GROUP_PREFIX, increase, zeroResult } from './common'
7+
import { filterDate, filterHost, filterNumberRange, processCondition, type ProcessedCondition } from './condition'
88
import type { StatCondition, StatDatabase } from './types'
99

10-
function createZeroResult(): timer.core.Result {
11-
return { focus: 0, time: 0 }
12-
}
13-
14-
function mergeMigration(exist: timer.core.Result | undefined, another: any) {
15-
exist = exist || createZeroResult()
16-
return increase(exist, { focus: another.focus ?? 0, time: another.time ?? 0, run: another.run ?? 0 })
10+
function mergeMigration(exist: timer.core.Result | undefined, another: any): timer.core.Result {
11+
return increase({ focus: another.focus ?? 0, time: another.time ?? 0, run: another.run ?? 0 }, exist ?? zeroResult())
1712
}
1813

1914
/**
@@ -36,11 +31,21 @@ function migrate(exists: { [key: string]: timer.core.Result }, data: any): Recor
3631
if (typeof value !== "object") return
3732
const exist = exists[key]
3833
const merged = mergeMigration(exist, value)
39-
merged && isNotZeroResult(merged) && (result[key] = mergeMigration(exist, value))
34+
isNotZeroResult(merged) && (result[key] = merged)
4035
})
4136
return result
4237
}
4338

39+
function filterRow(row: timer.core.Row, condition: ProcessedCondition): boolean {
40+
const { host, date, focus, time } = row
41+
const { timeStart, timeEnd, focusStart, focusEnd } = condition
42+
43+
return filterHost(host, condition)
44+
&& filterDate(date, condition)
45+
&& filterNumberRange(time, [timeStart, timeEnd])
46+
&& filterNumberRange(focus, [focusStart, focusEnd])
47+
}
48+
4449
/**
4550
* Default implementation by `chrome.storage.local`
4651
*/
@@ -73,10 +78,10 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
7378
}
7479

7580
private async accumulateInner(key: string, item: timer.core.Result): Promise<timer.core.Result> {
76-
let exist = await this.storage.getOne<timer.core.Result>(key)
77-
exist = increase(exist || createZeroResult(), item)
78-
await this.setByKey(key, exist)
79-
return exist
81+
const exist = await this.storage.getOne<timer.core.Result>(key)
82+
const value = increase(item, exist)
83+
await this.setByKey(key, value)
84+
return value
8085
}
8186

8287
/**
@@ -99,7 +104,7 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
99104
const afterUpdated: Record<string, timer.core.Result> = {}
100105
Object.entries(keys).forEach(([host, key]) => {
101106
const item = data[host]
102-
const exist: timer.core.Result = increase(items[key] as timer.core.Result || createZeroResult(), item)
107+
const exist: timer.core.Result = increase(item, items[key] as timer.core.Result)
103108
toUpdate[key] = afterUpdated[host] = exist
104109
})
105110
await this.storage.set(toUpdate)
@@ -127,12 +132,8 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
127132
}
128133
const { focus, time, run } = value as timer.core.Result
129134
const row: timer.core.Row = { host, date, focus, time }
130-
if (run !== undefined) {
131-
row.run = run
132-
}
133-
if (filterRow(row, cond)) {
134-
result.push(row)
135-
}
135+
run !== undefined && (row.run = run)
136+
filterRow(row, cond) && result.push(row)
136137
})
137138
return result
138139
}
@@ -159,7 +160,7 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
159160
async get(host: string, date: Date | string): Promise<timer.core.Result> {
160161
const key = generateKey(host, date)
161162
const exist = await this.storage.getOne<timer.core.Result>(key)
162-
return exist || createZeroResult()
163+
return exist ?? zeroResult()
163164
}
164165

165166
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { judgeVirtualFast } from "@util/pattern"
2+
import { formatTimeYMD } from "@util/time"
3+
import type { StatCondition } from './types'
4+
5+
export type ProcessedCondition = StatCondition & {
6+
useExactDate?: boolean
7+
exactDateStr?: string
8+
startDateStr?: string
9+
endDateStr?: string
10+
timeStart?: number
11+
timeEnd?: number
12+
focusStart?: number
13+
focusEnd?: number
14+
}
15+
16+
export function filterHost(host: string, condition: ProcessedCondition): boolean {
17+
const { keys, virtual } = condition
18+
const keyArr = typeof keys === 'string' ? [keys] : keys
19+
if (!virtual && judgeVirtualFast(host)) return false
20+
if (keyArr?.length && !keyArr.includes(host)) return false
21+
return true
22+
}
23+
24+
export function filterDate(
25+
date: string,
26+
{ useExactDate, exactDateStr, startDateStr, endDateStr }: ProcessedCondition
27+
): boolean {
28+
if (useExactDate) {
29+
if (exactDateStr !== date) return false
30+
} else {
31+
if (startDateStr && startDateStr > date) return false
32+
if (endDateStr && endDateStr < date) return false
33+
}
34+
return true
35+
}
36+
37+
export function filterNumberRange(val: number, [start, end]: [start?: number, end?: number]): boolean {
38+
if (start !== null && start !== undefined && start > val) return false
39+
if (end !== null && end !== undefined && end < val) return false
40+
return true
41+
}
42+
43+
export function processCondition(condition?: StatCondition): ProcessedCondition {
44+
const result: ProcessedCondition = { ...condition }
45+
46+
const paramDate = condition?.date
47+
if (paramDate) {
48+
if (paramDate instanceof Date) {
49+
result.useExactDate = true
50+
result.exactDateStr = formatTimeYMD(paramDate)
51+
} else {
52+
const [startDate, endDate] = paramDate
53+
result.useExactDate = false
54+
startDate && (result.startDateStr = formatTimeYMD(startDate))
55+
endDate && (result.endDateStr = formatTimeYMD(endDate))
56+
}
57+
}
58+
59+
const paramTime = condition?.timeRange
60+
if (paramTime) {
61+
paramTime.length >= 2 && (result.timeEnd = paramTime[1])
62+
paramTime.length >= 1 && (result.timeStart = paramTime[0])
63+
}
64+
65+
const paramFocus = condition?.focusRange
66+
if (paramFocus) {
67+
paramFocus.length >= 2 && (result.focusEnd = paramFocus[1])
68+
paramFocus.length >= 1 && (result.focusStart = paramFocus[0])
69+
}
70+
71+
return result
72+
}

src/database/stat-database/filter.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)