| 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; |