Skip to content

Commit

Permalink
replace storage implementation with nanostores
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Sep 8, 2023
1 parent ddc4fe7 commit 25c6733
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/core/AppConfigurationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ import {
import { coreSettings } from './CoreSettings'
import AppConfigurationContext from './AppConfigurationContext'

import { memoize } from 'lodash-es'
import { atom } from 'nanostores'

const getConfigurationStore = memoize((key: string) => {
const storageKey = `WebMIDICon_${key}`
const store = atom<string | undefined>(localStorage[storageKey])
store.listen((value) => {
if (value !== undefined) {
localStorage[storageKey] = value
} else {
delete localStorage[storageKey]
}
})
return store
})

export default function AppConfigurationProvider({
features,
children,
Expand All @@ -17,36 +33,23 @@ export default function AppConfigurationProvider({
children: ReactNode
}) {
const storage = useMemo((): ConfigurationStorage => {
const subscribers: Record<string, Set<() => void>> = {}
const notify = (key: string) => {
const set = subscribers[key]
if (!set) return
for (const listener of set) listener()
}

return {
get(key) {
return localStorage['WebMIDICon_' + key]
return getConfigurationStore(key).get()
},
has(key) {
return localStorage['WebMIDICon_' + key] !== undefined
return getConfigurationStore(key).get() !== undefined
},
delete(key) {
delete localStorage['WebMIDICon_' + key]
notify(key)
getConfigurationStore(key).set(undefined)
},
set(key, value) {
localStorage['WebMIDICon_' + key] = value
notify(key)
getConfigurationStore(key).set(value)
},
watch(key, callback) {
const set = (subscribers[key] ??= new Set())
const listener = () => callback()
set.add(listener)
const unsubscribe = getConfigurationStore(key).listen(callback)
return {
unsubscribe: () => {
set.delete(listener)
},
unsubscribe: () => unsubscribe(),
}
},
}
Expand Down Expand Up @@ -89,7 +92,7 @@ export default function AppConfigurationProvider({
serialize(key, value) {
const found = properties[key]
if (found.type === 'string') {
return (value as unknown) as string
return value as unknown as string
}
if (found.type === 'boolean') {
return value ? 'true' : 'false'
Expand Down

0 comments on commit 25c6733

Please sign in to comment.