From c16f269e34069ee7b8360611f99b12fd2959747a Mon Sep 17 00:00:00 2001 From: Zhichao Zhang Date: Thu, 16 Mar 2023 14:52:12 +0800 Subject: [PATCH] deserialize value before emit event --- .changeset/funny-poets-decide.md | 1 + packages/ui-store/src/store/LocalStorage.ts | 13 +++++++++---- packages/ui-store/src/store/SessionStorage.ts | 13 +++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.changeset/funny-poets-decide.md b/.changeset/funny-poets-decide.md index b750957..36c9fcc 100644 --- a/.changeset/funny-poets-decide.md +++ b/.changeset/funny-poets-decide.md @@ -3,3 +3,4 @@ --- fix emit not trigger in browser +deserialize value before emit event diff --git a/packages/ui-store/src/store/LocalStorage.ts b/packages/ui-store/src/store/LocalStorage.ts index 9d282fa..57994d3 100644 --- a/packages/ui-store/src/store/LocalStorage.ts +++ b/packages/ui-store/src/store/LocalStorage.ts @@ -25,8 +25,11 @@ export class LocalStorage extends Events { } } else { if (event.key) { - this.#items.set(event.key, event.newValue); - this.emit('store_changed', event.key, event.oldValue, event.newValue); + const oldValue = deserialize(event.oldValue); + const newValue = deserialize(event.newValue); + + this.#items.set(event.key, newValue); + this.emit('store_changed', event.key, oldValue, newValue); } } } @@ -48,13 +51,15 @@ export class LocalStorage extends Events { const oldValue = this.#items.get(key); this.#items.set(key, val); - this.emit('store_changed', key, oldValue, value); + this.emit('store_changed', key, oldValue, val); } public remove(key: string) { localStorage.removeItem(key); + const oldValue = this.#items.get(key); + this.#items.delete(key); - this.emit('store_changed', key, this.#items.get(key), null); + this.emit('store_changed', key, oldValue, null); } public each(fn: (key: string, val: any) => void): void { diff --git a/packages/ui-store/src/store/SessionStorage.ts b/packages/ui-store/src/store/SessionStorage.ts index f26bb41..a56f82c 100644 --- a/packages/ui-store/src/store/SessionStorage.ts +++ b/packages/ui-store/src/store/SessionStorage.ts @@ -25,8 +25,11 @@ export class SessionStorage extends Events { } } else { if (event.key) { - this.#items.set(event.key, event.newValue); - this.emit('store_changed', event.key, event.oldValue, event.newValue); + const oldValue = deserialize(event.oldValue); + const newValue = deserialize(event.newValue); + + this.#items.set(event.key, newValue); + this.emit('store_changed', event.key, oldValue, newValue); } } } @@ -48,13 +51,15 @@ export class SessionStorage extends Events { const oldValue = this.#items.get(key); this.#items.set(key, val); - this.emit('store_changed', key, oldValue, value); + this.emit('store_changed', key, oldValue, val); } public remove(key: string) { sessionStorage.removeItem(key); + const oldValue = this.#items.get(key); + this.#items.delete(key); - this.emit('store_changed', key, this.#items.get(key), null); + this.emit('store_changed', key, oldValue, null); } public each(fn: (key: string, val: any) => void): void {