Skip to content

Commit

Permalink
add timeout to managed storage function
Browse files Browse the repository at this point in the history
  • Loading branch information
mymindstorm committed Aug 16, 2024
1 parent 9e996f4 commit f788cf9
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/models/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,23 +691,31 @@ export class ManagedStorage {
static get<T>(key: string): T | undefined;
static get<T>(key: string, defaultValue: T): T;
static get<T>(key: string, defaultValue?: T) {
return new Promise((resolve: (result: T | undefined) => void) => {
if (chrome.storage.managed) {
chrome.storage.managed.get((data) => {
if (chrome.runtime.lastError) {
return resolve(defaultValue);
}
if (data) {
if (data[key]) {
return resolve(data[key]);
const managedStoragePromise = new Promise(
(resolve: (result: T | undefined) => void) => {
if (chrome.storage.managed) {
chrome.storage.managed.get((data) => {
if (chrome.runtime.lastError) {
return resolve(defaultValue);
}
}
return resolve(defaultValue);
});
} else {
// no available in Safari
resolve(defaultValue);
if (data) {
if (data[key]) {
return resolve(data[key]);
}
}
return resolve(defaultValue);
});
} else {
// no available in Safari
resolve(defaultValue);
}
}
);

const timeoutPromise = new Promise((resolve) => {
setTimeout(() => resolve(defaultValue), 1500);
});

return Promise.race([managedStoragePromise, timeoutPromise]);
}
}

0 comments on commit f788cf9

Please sign in to comment.