| 17 | } |
| 18 | |
| 19 | class PersistentStorageWrapper implements DevToolsStorage { |
| 20 | // Cache information within background context for future use without waiting. |
| 21 | private cache: {[key: string]: string | null} = {}; |
| 22 | |
| 23 | async get(key: string) { |
| 24 | if (key in this.cache) { |
| 25 | return this.cache[key]; |
| 26 | } |
| 27 | return new Promise<string | null>((resolve) => { |
| 28 | chrome.storage.local.get<Record<string, any>>(key, (result) => { |
| 29 | // If cache received a new value (from call to set()) |
| 30 | // before we retrieved the old value from storage, |
| 31 | // return the new value. |
| 32 | if (key in this.cache) { |
| 33 | logInfo(`Key ${key} was written to during read operation.`); |
| 34 | resolve(this.cache[key]); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (chrome.runtime.lastError) { |
| 39 | console.error('Failed to query DevTools data', chrome.runtime.lastError); |
| 40 | resolve(null); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | this.cache[key] = result[key]; |
| 45 | resolve(result[key]); |
| 46 | }); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | async set(key: string, value: string) { |
| 51 | this.cache[key] = value; |
| 52 | return new Promise<void>((resolve) => chrome.storage.local.set({[key]: value}, () => { |
| 53 | if (chrome.runtime.lastError) { |
| 54 | console.error('Failed to write DevTools data', chrome.runtime.lastError); |
| 55 | } |
| 56 | resolve(); |
| 57 | })); |
| 58 | } |
| 59 | |
| 60 | async remove(key: string) { |
| 61 | this.cache[key] = null; |
| 62 | return new Promise<void>((resolve) => chrome.storage.local.remove(key, () => { |
| 63 | if (chrome.runtime.lastError) { |
| 64 | console.error('Failed to delete DevTools data', chrome.runtime.lastError); |
| 65 | } |
| 66 | resolve(); |
| 67 | })); |
| 68 | } |
| 69 | |
| 70 | async has(key: string) { |
| 71 | return Boolean(await this.get(key)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | class TempStorage implements DevToolsStorage { |
| 76 | private map = new Map<string, string>(); |
nothing calls this directly
no outgoing calls
no test coverage detected