Skip to content

Commit

Permalink
Remove key from storage if default value
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Jun 30, 2024
1 parent b4ab02a commit 97c15f1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/infrastructure/chrome.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const topSites: TopSites = Object.freeze({
const storageArea: StorageArea = Object.freeze({
get: async () => ({}),
set: async () => undefined,
remove: async () => undefined,
onChanged: nullEvent,
})

Expand Down
1 change: 1 addition & 0 deletions src/infrastructure/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type StorageAreaName = 'sync'
export type StorageArea = {
get(key: string): Promise<Record<string, unknown>>
set(items: Record<string, unknown>): Promise<void>
remove(keys: string | string[]): Promise<void>
onChanged: Pick<chrome.storage.StorageArea['onChanged'], 'addListener' | 'removeListener'>
}

Expand Down
4 changes: 4 additions & 0 deletions src/infrastructure/chromeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const loadValue = async <T>(storageArea: StorageArea, spec: Spec<T>, setStoredVa
}

const saveValue = async <T>(storageArea: StorageArea, spec: Spec<T>, newValue: T) => {
if (newValue === spec.defaultValue) {
await storageArea.remove(spec.key)
return
}
await storageArea.set({ [spec.key]: newValue })
}

Expand Down
1 change: 1 addition & 0 deletions src/infrastructure/localStorage.mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PropsWithChildren } from 'react'
const nullLocalStorage: LocalStorage = {
setItem: () => {},
getItem: () => null,
removeItem: () => {},
}

export const NullLocalStorageProvider = (props: PropsWithChildren) => (
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext } from 'react'

export type LocalStorage = Pick<Storage, 'setItem' | 'getItem'>
export type LocalStorage = Pick<Storage, 'setItem' | 'getItem' | 'removeItem'>

export const LocalStorageContext = createContext<LocalStorage>(window.localStorage)
6 changes: 5 additions & 1 deletion src/infrastructure/localStorageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ export const useLocalStorageCache = <T extends string>(spec: Spec<T>): [T, Dispa
const localStorage = useContext(LocalStorageContext)
const [value, setValue] = useState<T>(getLocalStorageCacheOrDefaultValue(localStorage, spec))
useEffect(() => {
if (value === spec.defaultValue) {
localStorage.removeItem(spec.key)
return
}
localStorage.setItem(spec.key, value)
}, [spec.key, value])
}, [spec.key, spec.defaultValue, value])
return [value, setValue]
}

Expand Down
1 change: 1 addition & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const chrome: Chrome = {
sync: {
get: vi.fn().mockResolvedValue({}),
set: vi.fn(),
remove: vi.fn(),
onChanged: {
addListener: vi.fn(),
removeListener: vi.fn(),
Expand Down

0 comments on commit 97c15f1

Please sign in to comment.