-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathbackup-database.ts
More file actions
49 lines (38 loc) · 1.45 KB
/
backup-database.ts
File metadata and controls
49 lines (38 loc) · 1.45 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
/**
* Copyright (c) 2022 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 PREFIX = REMAIN_WORD_PREFIX + "backup"
const SNAPSHOT_KEY = PREFIX + "_snap"
const CACHE_KEY = PREFIX + "_cache"
function cacheKeyOf(type: timer.backup.Type) {
return CACHE_KEY + "_" + type
}
class BackupDatabase extends BaseDatabase {
constructor(storage: chrome.storage.StorageArea) {
super(storage)
}
async getSnapshot(type: timer.backup.Type): Promise<timer.backup.Snapshot | undefined> {
const cache = await this.storage.getOne<timer.backup.SnapshotCache>(SNAPSHOT_KEY)
return cache?.[type]
}
async updateSnapshot(type: timer.backup.Type, snapshot: timer.backup.Snapshot): Promise<void> {
const cache = await this.storage.getOne<timer.backup.SnapshotCache>(SNAPSHOT_KEY) || {}
cache[type] = snapshot
await this.storage.put(SNAPSHOT_KEY, cache)
}
async getCache(type: timer.backup.Type): Promise<unknown> {
return (await this.storage.getOne(cacheKeyOf(type))) || {}
}
async updateCache(type: timer.backup.Type, newVal: unknown): Promise<void> {
return this.storage.put(cacheKeyOf(type), newVal as Object)
}
async importData(_data: any): Promise<void> {
// Do nothing
}
}
export default BackupDatabase