-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstorage.js
117 lines (101 loc) · 3.42 KB
/
storage.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
export const DEFAULT_SETTINGS = {
show_popup_bring: true,
show_popup_send: true,
keep_moved_tabs_selected: true,
discard_minimized_window: false,
discard_minimized_window_delay_mins: 0,
minimize_kick_window: false,
show_badge: false,
badge_show_emoji_first: false,
badge_regex: '',
badge_regex_gflag: false,
set_title_preface: undefined,
title_preface_prefix: '',
title_preface_postfix: ' - ',
assert_title_preface: false,
enable_stash: false,
stash_home_root: 'toolbar_____',
stash_home_folder: 'Stashed Windows',
stash_nameless_with_title: false,
auto_name_unstash: true,
show_popup_stash: true,
theme: '',
};
// Return all stored data, merged with defaults of any unstored settings.
// Migrate/remove legacy data (remnants of past versions) if any.
//@ state -> (Object), state
export async function init() {
const ALLOWED_NON_SETTINGS_KEYS = [
'__version',
'_focused_window_id',
'_stash_home_id',
];
const ENTRIES_TO_MIGRATE = [
// oldKey, newKey, valueGetter
// v2.7.0
[
'unload_minimized_window',
'discard_minimized_window',
dict => dict.unload_minimized_window,
],
];
const ALLOWED_VALUES_DICT = {
// v2.4.1
theme: ['', 'dark', 'light'],
};
// Get all stored entries plus defaults for missing settings
const dict = { ...DEFAULT_SETTINGS, ...await getDict() };
// Migrate old keys to new keys
// Adds new entries to `dict`
const migrationDict = {};
for (const [oldKey, newKey, valueGetter] of ENTRIES_TO_MIGRATE)
if (oldKey in dict)
migrationDict[newKey] = dict[newKey] = valueGetter(dict);
set(migrationDict);
// Remove old/unused keys from `dict`
for (const key in dict) {
if (key in DEFAULT_SETTINGS || ALLOWED_NON_SETTINGS_KEYS.includes(key))
continue;
delete dict[key];
browser.storage.local.remove(key);
}
// Reset any invalid values
for (const [key, allowedValues] of Object.entries(ALLOWED_VALUES_DICT)) {
if (allowedValues.includes(dict[key]))
continue;
const value = allowedValues[0];
dict[key] = value;
set({ [key]: value });
}
return dict;
}
//@ (Object) -> (Boolean), state
export function set(dict) {
return browser.storage.local.set(dict).then(() => true).catch(() => false);
}
// Given a keys array or dict, return a dict of keys mapped to their values.
// If `keys` is undefined, return all stored data.
//@ (Object|[String]|undefined), state -> (Promise: Object)
export function getDict(keys) {
if (Array.isArray(keys))
keys = settingsArrayToDict(keys);
return browser.storage.local.get(keys);
}
// Given a key or array of keys, return the respective value or array of values.
//@ (String|[String]), state -> (Any|[Any])
export async function getValue(key) {
if (Array.isArray(key)) {
const dict = await browser.storage.local.get(settingsArrayToDict(key));
return Object.values(dict);
}
const dict = await browser.storage.local.get({ [key]: DEFAULT_SETTINGS[key] });
return dict[key];
}
// Turn an array of settings keys into a dict of keys and default values.
//@ ([String]) -> (Object)
function settingsArrayToDict(keys) {
const dict = {};
for (const key of keys)
dict[key] = DEFAULT_SETTINGS[key];
return dict;
}