-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathoption-database.ts
More file actions
53 lines (46 loc) · 1.54 KB
/
option-database.ts
File metadata and controls
53 lines (46 loc) · 1.54 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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { defaultOption } from "@util/constant/option"
import BaseDatabase from "./common/base-database"
import { REMAIN_WORD_PREFIX } from "./common/constant"
const DB_KEY = REMAIN_WORD_PREFIX + 'OPTION'
/**
* Database of options
*
* @since 0.3.0
*/
class OptionDatabase extends BaseDatabase {
async importData(data: any): Promise<void> {
const newVal = data[DB_KEY]
const exist = await this.getOption()
if (exist) {
Object.entries(exist).forEach(([key, value]) => (exist as any)[key] = value)
}
await this.setOption(newVal)
}
async getOption(): Promise<timer.option.AllOption> {
const option = await this.storage.getOne<timer.option.AllOption>(DB_KEY)
return option || defaultOption()
}
async setOption(option: timer.option.AllOption): Promise<void> {
option && await this.setByKey(DB_KEY, option)
}
/**
* @since 0.3.2
*/
addOptionChangeListener(listener: (newVal: timer.option.AllOption) => void) {
const storageListener = (
changes: { [key: string]: chrome.storage.StorageChange },
_areaName: chrome.storage.AreaName,
) => {
const optionInfo = changes[DB_KEY]
optionInfo && listener(optionInfo.newValue || {} as timer.option.AllOption)
}
chrome.storage.onChanged.addListener(storageListener)
}
}
export default OptionDatabase