Skip to content

Commit

Permalink
deserialize value before emit event
Browse files Browse the repository at this point in the history
  • Loading branch information
zzcwoshizz committed Mar 16, 2023
1 parent 433d9a7 commit c16f269
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions .changeset/funny-poets-decide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
---

fix emit not trigger in browser
deserialize value before emit event
13 changes: 9 additions & 4 deletions packages/ui-store/src/store/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export class LocalStorage extends Events<StorageEvent> {
}
} 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);
}
}
}
Expand All @@ -48,13 +51,15 @@ export class LocalStorage extends Events<StorageEvent> {
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 {
Expand Down
13 changes: 9 additions & 4 deletions packages/ui-store/src/store/SessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export class SessionStorage extends Events<StorageEvent> {
}
} 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);
}
}
}
Expand All @@ -48,13 +51,15 @@ export class SessionStorage extends Events<StorageEvent> {
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 {
Expand Down

0 comments on commit c16f269

Please sign in to comment.