forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption-database.ts
More file actions
47 lines (39 loc) · 1.3 KB
/
option-database.ts
File metadata and controls
47 lines (39 loc) · 1.3 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
/**
* 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 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)
}
}
const optionDatabase = new OptionDatabase()
export default optionDatabase