From f89f201f04e1bc94033c35483079d35f5134c552 Mon Sep 17 00:00:00 2001 From: bertmad3400 Date: Thu, 23 May 2024 11:45:25 +0000 Subject: [PATCH] Revert implementation, documentation and testing for cancelling before-Write and -Read --- README.md | 4 ++-- index.ts | 16 +++++--------- test/localStorageStore.test.ts | 40 ---------------------------------- 3 files changed, 7 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 0129da1..2e6b31f 100644 --- a/README.md +++ b/README.md @@ -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*/}, }) ``` diff --git a/index.ts b/index.ts index 0f089db..f5aaffd 100644 --- a/index.ts +++ b/index.ts @@ -28,8 +28,8 @@ export interface Options { onError?: (e: unknown) => void onWriteError?: (e: unknown) => void onParseError?: (newValue: string | null, e: unknown) => void - beforeRead?: (val: SerializerType, cancel: S) => StoreType | S - beforeWrite?: (val: StoreType, cancel: S) => SerializerType | S + beforeRead?: (val: SerializerType) => StoreType + beforeWrite?: (val: StoreType) => SerializerType } function getStorage(type: StorageType) { @@ -57,9 +57,7 @@ export function persisted(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)) @@ -82,9 +80,7 @@ export function persisted(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 } @@ -101,9 +97,7 @@ export function persisted(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) } diff --git a/test/localStorageStore.test.ts b/test/localStorageStore.test.ts index a822ab7..ec00a38 100644 --- a/test/localStorageStore.test.ts +++ b/test/localStorageStore.test.ts @@ -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((_: 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((_: 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((_: number, cancel: S) => cancel) - const store = persisted("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', () => {