-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add localStorage and sessionStorage with store_changed event (#13)
* feat: add localStorage and sessionStorage with store_changed event * fix: fix lint * fix: fix import * feat: use storage and onChanged event Co-authored-by: alpers <[email protected]>
- Loading branch information
1 parent
8a9fcf7
commit 856bcfc
Showing
12 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,36 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import storage from 'store/storages/sessionStorage'; | ||
|
||
import { SessionStorage } from './store/SessionStorage'; | ||
import { BaseStore } from './BaseStore'; | ||
|
||
export class BrowserSession extends BaseStore { | ||
#session: SessionStorage; | ||
|
||
constructor() { | ||
super(); | ||
this.#session = new SessionStorage(); | ||
} | ||
|
||
public all(fn: (key: string, value: unknown) => void): void { | ||
storage.each((value: unknown, key: string): void => { | ||
this.#session.each((key: string, value: unknown): void => { | ||
fn(key, value); | ||
}); | ||
} | ||
|
||
public get(key: string, fn: (value: unknown) => void): void { | ||
fn(storage.read(key) as unknown); | ||
fn(this.#session.get(key) as unknown); | ||
} | ||
|
||
public remove(key: string, fn?: () => void): void { | ||
storage.remove(key); | ||
this.#session.remove(key); | ||
fn && fn(); | ||
this.emit('store_changed', key); | ||
} | ||
|
||
public set(key: string, value: unknown, fn?: () => void): void { | ||
storage.write(key, value as string); | ||
this.#session.set(key, value as string); | ||
fn && fn(); | ||
this.emit('store_changed', key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import store from 'store'; | ||
|
||
import { LocalStorage } from './store/LocalStorage'; | ||
import { BaseStore } from './BaseStore'; | ||
|
||
export class BrowserStore extends BaseStore { | ||
#store: LocalStorage; | ||
|
||
constructor() { | ||
super(); | ||
this.#store = new LocalStorage(); | ||
|
||
window.addEventListener('storage', (event) => { | ||
event.key && this.emit('store_changed', event.key, event.newValue); | ||
}); | ||
} | ||
|
||
public all(fn: (key: string, value: unknown) => void): void { | ||
store.each((value: unknown, key: string): void => { | ||
this.#store.each((key: string, value: unknown): void => { | ||
fn(key, value); | ||
}); | ||
} | ||
|
||
public get(key: string, fn: (value: unknown) => void): void { | ||
fn(store.get(key) as unknown); | ||
fn(this.#store.get(key) as unknown); | ||
} | ||
|
||
public remove(key: string, fn?: () => void): void { | ||
store.remove(key); | ||
this.#store.remove(key); | ||
fn && fn(); | ||
this.emit('store_changed', key); | ||
} | ||
|
||
public set(key: string, value: unknown, fn?: () => void): void { | ||
store.set(key, value); | ||
this.#store.set(key, value); | ||
fn && fn(); | ||
this.emit('store_changed', key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { EventEmitter } from 'eventemitter3'; | ||
|
||
import { StorageEvent } from './types'; | ||
|
||
export abstract class Events { | ||
#eventemitter = new EventEmitter(); | ||
|
||
protected emit(type: StorageEvent, key: string, value?: unknown): boolean { | ||
return this.#eventemitter.emit(type, key, value); | ||
} | ||
|
||
public on(type: StorageEvent, handler: (key: string, value?: unknown) => any): this { | ||
this.#eventemitter.on(type, handler); | ||
|
||
return this; | ||
} | ||
|
||
public off(type: StorageEvent, handler: (key: string, value?: unknown) => any): this { | ||
this.#eventemitter.removeListener(type, handler); | ||
|
||
return this; | ||
} | ||
|
||
public once(type: StorageEvent, handler: (key: string, value?: unknown) => any): this { | ||
this.#eventemitter.once(type, handler); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export type StorageEvent = 'store_changed'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { deserialize, serialize } from './utils'; | ||
|
||
export class LocalStorage { | ||
get(key: string | null) { | ||
if (!key) return undefined; | ||
|
||
const val = localStorage.getItem(key); | ||
|
||
return deserialize(val); | ||
} | ||
|
||
set(key: string, value: unknown) { | ||
const val = serialize(value); | ||
|
||
localStorage.setItem(key, val); | ||
} | ||
|
||
remove(key: string) { | ||
localStorage.removeItem(key); | ||
} | ||
|
||
each(fn: (key: string, val: unknown) => void) { | ||
for (let i = localStorage.length - 1; i >= 0; i--) { | ||
const key = localStorage.key(i); | ||
|
||
if (key) fn(key, this.get(key)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { deserialize, serialize } from './utils'; | ||
|
||
export class SessionStorage { | ||
get(key: string | null) { | ||
if (!key) return undefined; | ||
|
||
const val = sessionStorage.getItem(key); | ||
|
||
return deserialize(val); | ||
} | ||
|
||
set(key: string, value: unknown) { | ||
const val = serialize(value); | ||
|
||
sessionStorage.setItem(key, val); | ||
} | ||
|
||
remove(key: string) { | ||
sessionStorage.removeItem(key); | ||
} | ||
|
||
each(fn: (key: string, val: unknown) => void) { | ||
for (let i = sessionStorage.length - 1; i >= 0; i--) { | ||
const key = sessionStorage.key(i); | ||
|
||
if (key) fn(key, this.get(key)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export function serialize(obj: unknown) { | ||
return JSON.stringify(obj); | ||
} | ||
|
||
export function deserialize(strVal: string | null) { | ||
if (!strVal) return undefined; | ||
|
||
let val: any; | ||
|
||
try { | ||
val = JSON.parse(strVal); | ||
} catch (e) { | ||
val = strVal; | ||
} | ||
|
||
return val; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters