feat: add WebStorage to address localStorage or sessionStorage

feat: make LocalStorage use WebStorage
feat: add SessionStorage using WebStorage
feat: make key in WebStorage reactive
feat: add updateKey event in case key changed
This commit is contained in:
Manuel Coenen 2022-03-24 09:53:23 +01:00
commit c54a8b5178
17 changed files with 574 additions and 68 deletions

View file

@ -18,8 +18,9 @@ export interface LocalStorageProps {
export default class LocalStorage extends SvelteComponentTyped<
LocalStorageProps,
{
save: CustomEvent<null>;
update: CustomEvent<{ prevValue: any; value: any }>;
save: WindowEventMap["save"];
update: WindowEventMap["update"];
updateKey: WindowEventMap["updateKey"];
},
{}
> {

View file

@ -0,0 +1,36 @@
/// <reference types="svelte" />
import { SvelteComponentTyped } from "svelte";
export interface SessionStorageProps {
/**
* Specify the local storage key
* @default "session-storage-key"
*/
key?: string;
/**
* Provide a value to persist
* @default ""
*/
value?: any;
}
export default class SessionStorage extends SvelteComponentTyped<
SessionStorageProps,
{
save: WindowEventMap["save"];
update: WindowEventMap["update"];
updateKey: WindowEventMap["updateKey"];
},
{}
> {
/**
* Remove the persisted key value from the browser's local storage
*/
clearItem: () => void;
/**
* Clear all key values from the browser's local storage
*/
clearAll: () => void;
}

View file

@ -0,0 +1,42 @@
/// <reference types="svelte" />
import { SvelteComponentTyped } from "svelte";
export interface WebStorageProps {
/**
* Specify the local storage key
* @default "storage-key"
*/
key?: string;
/**
* Provide a value to persist
* @default ""
*/
value?: any;
/**
* Select WebStorage to use
* @default "local"
*/
storage?: "local" | "session";
}
export default class WebStorage extends SvelteComponentTyped<
WebStorageProps,
{
save: CustomEvent<null>;
updateKey: CustomEvent<{ prevKey: any; key: any }>;
update: CustomEvent<{ prevValue: any; value: any }>;
},
{}
> {
/**
* Remove the persisted key value from the browser's selected WebStorage
*/
clearItem: () => void;
/**
* Clear all key values from the browser's selected WebStorage
*/
clearAll: () => void;
}