-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
104 lines (100 loc) · 3.42 KB
/
storage.js
File metadata and controls
104 lines (100 loc) · 3.42 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
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
// slactac/storage.js
const SYNC_KEY = "chatRoomOverrides";
/**
* A wrapper for chrome.storage that gracefully falls back to local storage
* if sync is unavailable or fails.
*/
const storage = {
/**
* Retrieves the chat room overrides. It first tries sync storage,
* and if that fails, it tries local storage.
* @returns {Promise<object>} A promise that resolves to the overrides object.
*/
get: () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(SYNC_KEY, (data) => {
if (chrome.runtime.lastError) {
console.warn(
"SLACTAC Storage: Could not read from sync storage, falling back to local.",
chrome.runtime.lastError.message
);
// If sync fails, try getting from local storage.
chrome.storage.local.get(SYNC_KEY, (localData) => {
if (chrome.runtime.lastError) {
console.error(
"SLACTAC Storage: Critical - Failed to read from local storage.",
chrome.runtime.lastError.message
);
reject(chrome.runtime.lastError);
} else {
resolve(localData[SYNC_KEY] || {});
}
});
} else {
resolve(data[SYNC_KEY] || {});
}
});
});
},
/**
* Saves the chat room overrides. It attempts to save to sync storage first,
* but if that fails, it saves to local storage as a reliable backup.
* @param {object} overrides The overrides object to save.
* @returns {Promise<void>} A promise that resolves when the save is complete.
*/
set: (overrides) => {
return new Promise((resolve, reject) => {
chrome.storage.sync.set({ [SYNC_KEY]: overrides }, () => {
if (chrome.runtime.lastError) {
console.warn(
"SLACTAC Storage: Could not save to sync storage, saving to local instead.",
chrome.runtime.lastError.message
);
// If sync fails, save to local storage.
chrome.storage.local.set({ [SYNC_KEY]: overrides }, () => {
if (chrome.runtime.lastError) {
console.error(
"SLACTAC Storage: Critical - Failed to save to local storage.",
chrome.runtime.lastError.message
);
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
} else {
resolve();
}
});
});
},
/**
* Removes all overrides. It attempts to clear from sync storage,
* and always clears from local storage as well to ensure a clean state.
* @returns {Promise<void>} A promise that resolves when the removal is complete.
*/
clear: () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.remove(SYNC_KEY, () => {
if (chrome.runtime.lastError) {
console.warn(
"SLACTAC Storage: Could not clear sync storage.",
chrome.runtime.lastError.message
);
}
// Always attempt to clear local storage as well.
chrome.storage.local.remove(SYNC_KEY, () => {
if (chrome.runtime.lastError) {
console.error(
"SLACTAC Storage: Critical - Failed to clear local storage.",
chrome.runtime.lastError.message
);
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
});
});
},
};