Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore storage event for session-backed stores #200

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})