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