Skip to content

Commit

Permalink
Revert implementation, documentation and testing for cancelling befor…
Browse files Browse the repository at this point in the history
…e-Write and -Read
  • Loading branch information
bertmad3400 committed May 23, 2024
1 parent b3b985f commit f89f201
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const preferences = persisted('local-storage-key', 'default-value', {
syncTabs: true, // choose whether to sync localStorage across tabs, default is true
onWriteError: (error) => {/* handle or rethrow */}, // Defaults to console.error with the error object
onParseError: (raw, error) => {/* handle or rethrow */}, // Defaults to console.error with the error object
beforeRead: (value, cancel) => {/* change value after serialization but before setting store to return value. Return cancel to cancel the operation*/},
beforeWrite: (value, cancel) => {/* change value after writing to store, but before writing return value to local storage. Return cancel to cancel the operation*/},
beforeRead: (value) => {/* change value after serialization but before setting store to return value. Return cancel to cancel the operation*/},
beforeWrite: (value) => {/* change value after writing to store, but before writing return value to local storage. Return cancel to cancel the operation*/},
})
```

Expand Down
16 changes: 5 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface Options<StoreType, SerializerType> {
onError?: (e: unknown) => void
onWriteError?: (e: unknown) => void
onParseError?: (newValue: string | null, e: unknown) => void
beforeRead?: <S extends symbol>(val: SerializerType, cancel: S) => StoreType | S
beforeWrite?: <S extends symbol>(val: StoreType, cancel: S) => SerializerType | S
beforeRead?: (val: SerializerType) => StoreType
beforeWrite?: (val: StoreType) => SerializerType
}

function getStorage(type: StorageType) {
Expand Down Expand Up @@ -57,9 +57,7 @@ export function persisted<StoreType, SerializerType>(key: string, initialValue:
const storage = browser ? getStorage(storageType) : null

function updateStorage(key: string, value: StoreType) {
const cancel = Symbol("cancel")
const newVal = beforeWrite(value, cancel)
if (newVal === cancel) return
const newVal = beforeWrite(value)

try {
storage?.setItem(key, serializer.stringify(newVal))
Expand All @@ -82,9 +80,7 @@ export function persisted<StoreType, SerializerType>(key: string, initialValue:
const serialized = serialize(json)
if (serialized == null) return initialValue

const cancel = Symbol("cancel")
const newVal = beforeRead(serialized, cancel)
if (newVal === cancel) return initialValue
const newVal = beforeRead(serialized)
return newVal
}

Expand All @@ -101,9 +97,7 @@ export function persisted<StoreType, SerializerType>(key: string, initialValue:
onParseError(event.newValue, e)
return
}
const cancel = Symbol("cancel")
const processedVal = beforeRead(newVal, cancel)
if (processedVal === cancel) return
const processedVal = beforeRead(newVal)

set(processedVal)
}
Expand Down
40 changes: 0 additions & 40 deletions test/localStorageStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,46 +160,6 @@ describe('persisted()', () => {

expect(JSON.parse(localStorage.getItem("beforeWrite-test") as string)).toEqual(4)
})

it("allows to cancel read operation during initialization", () => {
localStorage.setItem("beforeRead-init-cancel", JSON.stringify(2))
const beforeRead = vi.fn(<S extends symbol>(_: any, cancel: S) => cancel)
const store = persisted("beforeRead-init-cancel", 0, { beforeRead })
expect(beforeRead).toHaveBeenCalledOnce()
expect(get(store)).toEqual(0)
})

it("allows to cancel read operation during event handling", () => {
// Will only call beforeRead on init if key exists, so creates key
localStorage.setItem("beforeRead-cancel", JSON.stringify(2))

const beforeRead = vi.fn(<S extends symbol>(_: any, cancel: S) => cancel)
const store = persisted("beforeRead-cancel", 0, { beforeRead })

const values: number[] = []

const unsub = store.subscribe((val: number) => {
values.push(val)
})

const event = new StorageEvent('storage', { key: 'beforeRead-cancel', newValue: "2" })
window.dispatchEvent(event)

expect(beforeRead).toHaveBeenCalledTimes(2)
expect(values).toEqual([0])

unsub()
})

it("allows to cancel write operation", () => {
const beforeWrite = vi.fn(<S extends symbol>(_: number, cancel: S) => cancel)
const store = persisted<number, number>("beforeWrite-cancel", 0, { beforeWrite })
store.set(2)

expect(JSON.parse(localStorage.getItem("beforeWrite-cancel") as string)).toEqual(null)
expect(get(store)).toEqual(2)
expect(beforeWrite).toHaveBeenCalledOnce()
})
})

describe('handles window.storage event', () => {
Expand Down

0 comments on commit f89f201

Please sign in to comment.