Skip to content

Commit

Permalink
fix: Storage availability checking method (use random string instead …
Browse files Browse the repository at this point in the history
…of null) (#581)
  • Loading branch information
tarampampam authored May 14, 2024
1 parent 5dc1754 commit 78aa03b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/entrypoints/background/persistent/storage-area.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('storage', () => {
try {
expect(await storage.get()).toStrictEqual({ some: 'data' })
expect(global.chrome.storage.sync.get).toHaveBeenCalledTimes(2)
expect(global.chrome.storage.sync.get).toHaveBeenCalledWith(null)
expect(global.chrome.storage.sync.get).toHaveBeenCalledWith(storage.testKey)
expect(global.chrome.storage.sync.get).toHaveBeenCalledWith('some-key')

// the second shouldn't make an additional call to chrome.storage.sync.get
Expand Down Expand Up @@ -97,11 +97,13 @@ describe('storage', () => {
const localGetMock = vi.spyOn(global.chrome.storage.local, 'get')

try {
expect(await new StorageArea('some-key', 'sync', 'local').get()).toStrictEqual({ some: 'data' })
const storage = new StorageArea('some-key', 'sync', 'local')

expect(await storage.get()).toStrictEqual({ some: 'data' })
expect(global.chrome.storage.sync.get).toHaveBeenCalledTimes(1)
expect(global.chrome.storage.sync.get).toHaveBeenCalledWith(null)
expect(global.chrome.storage.sync.get).toHaveBeenCalledWith(storage.testKey)
expect(global.chrome.storage.local.get).toHaveBeenCalledTimes(2)
expect(global.chrome.storage.local.get).toHaveBeenCalledWith(null)
expect(global.chrome.storage.local.get).toHaveBeenCalledWith(storage.testKey)
expect(global.chrome.storage.local.get).toHaveBeenCalledWith('some-key')
} finally {
syncGetMock.mockRestore()
Expand Down
7 changes: 5 additions & 2 deletions src/entrypoints/background/persistent/storage-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type AreaName = 'sync' | 'local'
export default class<TState extends Record<string, unknown> = Record<string, unknown>> {
/** The key used to store the data */
private readonly key: string
/** The key used to test the storage area availability */
readonly testKey: string
/** The storage area to use and fallback to if the main area is not available */
private readonly areaName: { main: AreaName; fallback?: AreaName }
/** Do not use this property directly, use getStorage() method instead */
Expand All @@ -19,6 +21,7 @@ export default class<TState extends Record<string, unknown> = Record<string, unk
constructor(key: string, mainArea: AreaName, fallbackArea?: AreaName) {
this.key = key
this.areaName = { main: mainArea, fallback: fallbackArea }
this.testKey = crypto.randomUUID()
}

/**
Expand All @@ -34,7 +37,7 @@ export default class<TState extends Record<string, unknown> = Record<string, unk
try {
const main = this.areaName.main === 'sync' ? chrome.storage.sync : chrome.storage.local

await main.get(null) // try to get main storage
await main.get(this.testKey) // try to get main storage

if (!chrome.runtime.lastError) {
this.storage = main
Expand All @@ -49,7 +52,7 @@ export default class<TState extends Record<string, unknown> = Record<string, unk
const fallback = this.areaName.fallback === 'sync' ? chrome.storage.sync : chrome.storage.local

try {
await fallback.get(null) // try to get fallback storage
await fallback.get(this.testKey) // try to get fallback storage
} catch (err) {
throw new Error(String(err))
}
Expand Down

0 comments on commit 78aa03b

Please sign in to comment.