25 lines
563 B
TypeScript
25 lines
563 B
TypeScript
|
import { KeyValueStorage } from "./basic";
|
||
|
|
||
|
/**
|
||
|
* Key-value store
|
||
|
*/
|
||
|
export class BrowserLocalStorage implements KeyValueStorage {
|
||
|
constructor() {
|
||
|
if (typeof localStorage == "undefined" || !localStorage) {
|
||
|
throw new Error("localStorage not available");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async get(key: string): Promise<string | null> {
|
||
|
return localStorage.getItem(key);
|
||
|
}
|
||
|
|
||
|
async set(key: string, value: string | null): Promise<void> {
|
||
|
if (value === null) {
|
||
|
localStorage.removeItem(key);
|
||
|
} else {
|
||
|
localStorage.setItem(key, value);
|
||
|
}
|
||
|
}
|
||
|
}
|