Skip to content

Commit

Permalink
Merge pull request #200 from joshnuss/fix-storage-event-for-session
Browse files Browse the repository at this point in the history
fix: ignore storage event for session-backed stores
  • Loading branch information
joshnuss authored Oct 2, 2023
2 parents 34365a9 + 1500cd7 commit 1d0335f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
18 changes: 13 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ declare type Updater<T> = (value: T) => T;
declare type StoreDict<T> = { [key: string]: Writable<T> }

/* eslint-disable @typescript-eslint/no-explicit-any */
const stores: StoreDict<any> = {}
interface Stores {
local: StoreDict<any>,
session: StoreDict<any>,
}

const stores : Stores = {
local: {},
session: {}
}

interface Serializer<T> {
parse(text: string): T
Expand Down Expand Up @@ -37,15 +45,15 @@ export function persisted<T>(key: string, initialValue: T, options?: Options<T>)
storage?.setItem(key, serializer.stringify(value))
}

if (!stores[key]) {
if (!stores[storageType][key]) {
const store = internal(initialValue, (set) => {
const json = storage?.getItem(key)

if (json) {
set(<T>serializer.parse(json))
}

if (browser) {
if (browser && storageType == 'local') {
const handleStorage = (event: StorageEvent) => {
if (event.key === key)
set(event.newValue ? serializer.parse(event.newValue) : null)
Expand All @@ -59,7 +67,7 @@ export function persisted<T>(key: string, initialValue: T, options?: Options<T>)

const {subscribe, set} = store

stores[key] = {
stores[storageType][key] = {
set(value: T) {
updateStorage(key, value)
set(value)
Expand All @@ -77,5 +85,5 @@ export function persisted<T>(key: string, initialValue: T, options?: Options<T>)
}
}

return stores[key]
return stores[storageType][key]
}
32 changes: 24 additions & 8 deletions test/localStorageStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ describe('persisted()', () => {

unsub()
})

it('ignores session-backed stores', () => {
const store = persisted('myKey10', 1, { storage: 'session' })
const values = []

const unsub = store.subscribe((value) => {
values.push(value)
})

const event = new StorageEvent('storage', {key: 'myKey10', newValue: '2'})
window.dispatchEvent(event)

expect(values).toEqual([1])

unsub()
})
})

it('allows custom serialize/deserialize functions', () => {
Expand All @@ -210,17 +226,17 @@ describe('persisted()', () => {
})

it('lets you switch storage type', () => {
vi.spyOn(Object.getPrototypeOf(window.sessionStorage), 'setItem')
Object.setPrototypeOf(window.sessionStorage.setItem, vi.fn())
vi.spyOn(Object.getPrototypeOf(window.sessionStorage), 'setItem')
Object.setPrototypeOf(window.sessionStorage.setItem, vi.fn())

const value = 'foo'
const value = 'foo'

const store = persisted('myKey12', value, {
storage: 'session'
})
const store = persisted('myKey12', value, {
storage: 'session'
})

store.set('bar')
store.set('bar')

expect(window.sessionStorage.setItem).toHaveBeenCalled()
expect(window.sessionStorage.setItem).toHaveBeenCalled()
})
})

0 comments on commit 1d0335f

Please sign in to comment.