-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageHandler.mjs
334 lines (290 loc) · 10.6 KB
/
storageHandler.mjs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Manage Extension Local storage
*
* Mainly for dashboard and storing settings
*/
let instance;
// singleton messing up debug function here - hack to use the first one for now
// until I figure out a better global debug function
let storageDebug = Function.prototype.bind.call(console.debug, console, `vch 🗄️`);
/**
* Class to handle chrome storage
*/
export class StorageHandler {
area = "local"; // force local storage
#listeners = [];
/**
* Initialize storage with a key and default settings
* - needed if the key doesn't exist already, like on a new install
* - only sets the key if it doesn't exist or is empty
* - defaults to using chrome.storage.local
* @param {string} key
* @param {object} settings
* @returns {Promise<void>}
*/
static initStorage = async (key, settings)=>{
const current = await chrome.storage.local.get(key);
if(!current[key] || Object.keys(current[key]).length === 0){
await chrome.storage.local.set({[key]: settings});
}
}
/**
* The current contents of the storage
* @type {{object}}
* @returns {object}
*/
static contents = {};
/**
* Create a new instance of the storage handler
* @param {function} debug - optional custom debug function
* @returns {*|Promise<StorageHandler>} - the storage handler instance
*/
constructor(debug = () => {}) {
// singleton pattern
if (instance) {
storageDebug = typeof storageDebug === "object" ? storageDebug : debug;
// console.info("existing instance");
return instance;
}
instance = this;
this.storage = chrome.storage[this.area];
this.debug = storageDebug || debug;
// this.debug("new instance");
return (async () => {
StorageHandler.contents = await this.storage.get();
// update contents on change
chrome.storage.onChanged.addListener(async (changes, namespace) => {
// debug("storage changed", changes, namespace);
if (namespace !== this.area)
return;
for (let [key, {oldValue, newValue}] of Object.entries(changes)) {
// Q: is the below redundant if set and update always update contents?
// A: no, because the class has several instances that are unaware of each other
Object.assign(StorageHandler.contents, {[key]: newValue});
const changedValues = StorageHandler.#findChangedValues(oldValue, newValue);
// Uncomment if needed for debugging
/*
this.debug(
`Storage key "${key}" in namespace "${namespace}" changed.`,
`\n> Old value was`, oldValue, `\n> New value is`, newValue, `\n> Changed values are`, changedValues);
*/
// returns the newValue and any values that changed (NOT the old value)
this.#listeners.forEach(listener => {
if (listener.key === key)
listener.callback.call(listener.callback, newValue, changedValues)
});
}
// storage = await chrome.storage.local.get(null);
// this.debug("updated storage contents", StorageHandler.contents);
});
return this;
})();
}
/**
* Find and return the changed values between two objects
* @param {object} oldValue
* @param {object} newValue
* @returns {*[]|{}} - the changed values
*/
static #findChangedValues(oldValue, newValue) {
let changedValues = Array.isArray(newValue) ? [] : {};
for (let key in newValue) {
if (newValue.hasOwnProperty(key)) {
if (!oldValue || !oldValue[key]) // handle if the key didn't exist
changedValues[key] = newValue[key];
else if (typeof newValue[key] === "object" && oldValue[key]) {
let nestedChange = StorageHandler.#findChangedValues(oldValue[key], newValue[key]);
if (Object.keys(nestedChange).length > 0) {
changedValues[key] = nestedChange;
}
} else if (oldValue[key] !== newValue[key]) {
changedValues[key] = newValue[key];
}
}
}
return changedValues;
}
/**
* Update an key with changed values in storage
* @param {string} key - key to update
* @param {object} newValue - new values to change or add
* @returns {Promise<*>} - the updated contents for the key
*/
async update(key = "", newValue = {}) {
if (key === "" || !key) {
console.debug("no key provided - can't update");
return;
}
try {
if (StorageHandler.contents[key]) {
const updatedValue = Object.assign(StorageHandler.contents[key], newValue);
Object.assign(StorageHandler.contents, {[key]: updatedValue});
} else
StorageHandler.contents[key] = newValue;
await this.storage.set(StorageHandler.contents)
return StorageHandler.contents[key];
} catch (error) {
if(error.message.match(/context invalidated/i)){
this.#handleDisconnect()
}
else{
this.debug("error updating storage", error);
this.debug("key", key ?? null, "newValue", newValue ?? null);
this.debug("contents", StorageHandler.contents);
this.debug("storage", await this.storage.get());
}
}
}
/**
* Get a stored object from a key
* @param {string }key - key to get
* @returns {Promise<unknown>} - the value of the key
*/
async get(key = "") {
if (key) {
try{
const obj = await this.storage.get(key);
return Object.values(obj)[0];
}
catch (error) {
if(error.message.match(/context invalidated/i)){
// this.debug("Disconnected from background script", error);
this.#handleDisconnect();
}
else
this.debug("error getting storage", error);
}
}
}
/**
* Set a key in storage
* @param {string} key - key to set
* @param {object} value - value to set
* @returns {Promise<*>} - the value set
*/
async set(key = "", value = {}) {
try {
Object.assign(StorageHandler.contents, {[key]: value});
await this.storage.set({[key]: value});
return value;
}
catch (error) {
if(error.message.match(/context invalidated/i)){
this.#handleDisconnect()
}
else
this.debug("error setting key", error);
}
}
/**
* Delete a key from storage
* @param {string} key - key to delete
* @returns {Promise<void>} - void
*/
async delete(key) {
if (!key) {
this.debug("No key provided - can't delete");
return;
}
if (!StorageHandler.contents.hasOwnProperty(key)) {
this.debug(`Key "${key}" not found in storage - can't delete`);
return;
}
try {
await this.storage.remove(key);
delete StorageHandler.contents[key];
this.debug(`Deleted key "${key}" from storage`);
}
catch (error) {
if(error.message.match(/context invalidated/i)){
this.#handleDisconnect()
}
else
this.debug("error deleting key", error);
}
}
/**
* Add a listener for storage changes on a key
* @param {string} key - key to listen to
* @param {function(object, object):void} callback - callback function to run on change with (newValue, changedValue)=>{}
*/
addListener = (key, callback= null) => {
this.#listeners.push({key, callback});
this.debug(`added storage listener "${key}"`);
}
/**
* Return the all contents of the storage
* @returns {{}} - the contents of the storage
*/
get contents() {
return StorageHandler.contents;
}
/**
* Prevent direct setting of contents
* @param value
* @private
* @throws Error
* @returns {void}
*/
set contents(value) {
throw new Error(`Can't set contents directly. Use set or update instead. ${value}`);
}
// ToDo: test removeListener
/**
* Remove a storage listener
* @param {string} key - key to remove listener from
*/
removeListener = (key) => {
if (!key) {
debug("no key provided - can't remove listener");
return;
}
this.#listeners = this.#listeners.filter(listener => listener.key !== key);
this.debug(`removed storage listener "${key}"`);
}
/**
* Disconnect logic
*/
// Keep a map of functions to call when disconnected from the background script
disconnectedCallbackMap = new Map();
disconnected = false;
/**
* Runs a callback when the messageHandler detects the background script is disconnected
* @private
* @returns {void}
*/
#handleDisconnect() {
this.disconnected = true;
if(this.disconnectedCallbackMap.size > 0){
this.debug("running disconnect callbacks: ", this.disconnectedCallbackMap.keys());
this.disconnectedCallbackMap.forEach((cb) => {
cb();
});
}
}
/**
* Adds a callback to run when the messageHandler detects the background script is disconnected
* - multiple callbacks allowed per instance
* - must have a unique name
* @param {string} name - a name used to identify the callback
* @param {function} callback
*/
onDisconnectedHandler(name = 'default', callback){
this.disconnectedCallbackMap.set(name, callback);
}
/**
* Sets a default callback to run when the messageHandler detects the background script is disconnected
* Only one allowed per instance
* @param callback
*/
set onDisconnected(callback){
this.onDisconnectedHandler('default', callback);
}
/**
* Remove the disconnect handler
* @param {string} name
*/
removeDisconnectHandler(name = 'default'){
this.disconnectedCallbackMap.delete(name);
}
}