storage/local.ts

25 lines
578 B
TypeScript
Raw Normal View History

2021-06-21 22:05:08 +00:00
import { KeyValueStorage } from "./basic.ts";
/**
2019-11-07 21:33:23 +00:00
* Key-value store using localStorage
*/
2021-06-21 22:05:08 +00:00
export class LocalStorage 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);
}
}
}