Skip to content

Commit

Permalink
fix the browser storage event not work right
Browse files Browse the repository at this point in the history
up yarn version and bump deps
  • Loading branch information
zzcwoshizz committed Mar 16, 2023
1 parent 9b1b32a commit 353acc1
Show file tree
Hide file tree
Showing 15 changed files with 7,142 additions and 7,761 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-hounds-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@zcloak/ui-store': patch
---

fix the browser storage event not work right
541 changes: 0 additions & 541 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

This file was deleted.

550 changes: 0 additions & 550 deletions .yarn/plugins/@yarnpkg/plugin-version.cjs

This file was deleted.

807 changes: 0 additions & 807 deletions .yarn/releases/yarn-3.3.0.cjs

This file was deleted.

876 changes: 876 additions & 0 deletions .yarn/releases/yarn-4.0.0-rc.40.cjs

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ enableProgressBars: false

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: "@yarnpkg/plugin-version"

yarnPath: .yarn/releases/yarn-3.3.0.cjs
yarnPath: .yarn/releases/yarn-4.0.0-rc.40.cjs
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"bugs": "https://github.com/zCloak-Network/common-ts/issues",
"homepage": "https://github.com/zCloak-Network/common-ts#readme",
"license": "Apache-2.0",
"packageManager": "yarn@3.3.0",
"packageManager": "yarn@4.0.0-rc.40",
"private": true,
"repository": {
"type": "git",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@types/jest": "^29.2.3",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"@zcloak/dev": "^0.22.0",
"@zcloak/dev": "^0.26.0",
"concurrently": "^7.6.0",
"devtron": "^1.4.0",
"humps": "^2.0.1",
Expand All @@ -45,8 +45,10 @@
"react-dom": "^18.2.0",
"react-router-dom": "^6.4.3",
"source-map-explorer": "^2.5.3",
"typescript": "^4.9.3",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
},
"resolutions": {
"typescript": "^5.0.1-rc"
}
}
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"devDependencies": {
"@types/punycode": "^2.1.0",
"@zcloak/dev": "^0.22.0",
"@zcloak/dev": "^0.26.0",
"assert": "^2.0.0",
"babel-loader": "^9.1.0",
"buffer": "^6.0.3",
Expand Down
3 changes: 0 additions & 3 deletions packages/ui-store/src/BrowserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class BrowserSession extends BaseStore {
constructor() {
super();
this.#session = new SessionStorage();
this.#session.on('clear', this.emit);
this.#session.on('store_changed', this.emit);
}

Expand All @@ -36,14 +35,12 @@ export class BrowserSession extends BaseStore {

public remove(key: string): Promise<void> {
this.#session.remove(key);
this.emit('store_changed', key);

return Promise.resolve();
}

public set(key: string, value: any): Promise<void> {
this.#session.set(key, value);
this.emit('store_changed', key, value);

return Promise.resolve();
}
Expand Down
3 changes: 0 additions & 3 deletions packages/ui-store/src/BrowserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class BrowserStore extends BaseStore {
constructor() {
super();
this.#store = new LocalStorage();
this.#store.on('clear', this.emit);
this.#store.on('store_changed', this.emit);
}

Expand All @@ -36,14 +35,12 @@ export class BrowserStore extends BaseStore {

public remove(key: string): Promise<void> {
this.#store.remove(key);
this.emit('store_changed', key);

return Promise.resolve();
}

public set(key: string, value: any): Promise<void> {
this.#store.set(key, value);
this.emit('store_changed', key, value);

return Promise.resolve();
}
Expand Down
26 changes: 22 additions & 4 deletions packages/ui-store/src/store/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import type { StorageEvent } from '../types';

import Events from 'eventemitter3';

import { deserialize, serialize } from './utils';
import { deserialize, getAllItems, serialize } from './utils';

export class LocalStorage extends Events<StorageEvent> {
#items: Map<string, any> = new Map();

constructor() {
super();

this.#items = getAllItems(localStorage);
window.addEventListener('storage', (event) => {
if (event.storageArea === localStorage) {
if (sessionStorage.length === 0) {
this.emit('clear');
if (localStorage.length === 0) {
// clear storage

for (const [key, value] of this.#items) {
this.#items.delete(key);
this.emit('store_changed', key, value, null);
}
} else {
this.emit('store_changed', event.key, event.oldValue, event.newValue);
if (event.key) {
this.#items.set(event.key, event.newValue);
this.emit('store_changed', event.key, event.oldValue, event.newValue);
}
}
}
});
Expand All @@ -33,10 +45,16 @@ export class LocalStorage extends Events<StorageEvent> {
const val = serialize(value);

localStorage.setItem(key, val);
const oldValue = this.#items.get(key);

this.#items.set(key, val);
this.emit('store_changed', key, oldValue, value);
}

public remove(key: string) {
localStorage.removeItem(key);
this.#items.delete(key);
this.emit('store_changed', key, this.#items.get(key), null);
}

public each(fn: (key: string, val: any) => void): void {
Expand Down
24 changes: 21 additions & 3 deletions packages/ui-store/src/store/SessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import type { StorageEvent } from '../types';

import Events from 'eventemitter3';

import { deserialize, serialize } from './utils';
import { deserialize, getAllItems, serialize } from './utils';

export class SessionStorage extends Events<StorageEvent> {
#items: Map<string, any> = new Map();

constructor() {
super();

this.#items = getAllItems(sessionStorage);
window.addEventListener('storage', (event) => {
if (event.storageArea === sessionStorage) {
if (sessionStorage.length === 0) {
this.emit('clear');
// clear storage

for (const [key, value] of this.#items) {
this.#items.delete(key);
this.emit('store_changed', key, value, null);
}
} else {
this.emit('store_changed', event.key, event.oldValue, event.newValue);
if (event.key) {
this.#items.set(event.key, event.newValue);
this.emit('store_changed', event.key, event.oldValue, event.newValue);
}
}
}
});
Expand All @@ -33,10 +45,16 @@ export class SessionStorage extends Events<StorageEvent> {
const val = serialize(value);

sessionStorage.setItem(key, val);
const oldValue = this.#items.get(key);

this.#items.set(key, val);
this.emit('store_changed', key, oldValue, value);
}

public remove(key: string) {
sessionStorage.removeItem(key);
this.#items.delete(key);
this.emit('store_changed', key, this.#items.get(key), null);
}

public each(fn: (key: string, val: any) => void): void {
Expand Down
14 changes: 14 additions & 0 deletions packages/ui-store/src/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ export function deserialize(strVal: string | null) {

return val;
}

export function getAllItems(storage: Storage): Map<string, any> {
const items: Map<string, any> = new Map();

for (let i = 0; i < storage.length; i++) {
const key = storage.key(i);

if (key) {
items.set(key, storage.getItem(key));
}
}

return items;
}
2 changes: 1 addition & 1 deletion packages/ui-store/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

export type StorageEvent = 'clear' | 'store_changed';
export type StorageEvent = 'store_changed';
Loading

0 comments on commit 353acc1

Please sign in to comment.