|
- export default class LocalStorage<T> {
- constructor(
- private readonly source: Storage,
- private readonly serializer: (t: T) => string,
- private readonly deserializer: (s: string) => T
- ) {}
-
- getItem(id: string, fallback: T = null) {
- const raw = this.source.getItem(id)
- if (raw === null) {
- return fallback
- }
- return this.deserializer(raw)
- }
-
- setItem(id: string, item: T) {
- this.source.setItem(id, this.serializer(item))
- }
-
- removeItem(id: string) {
- this.source.removeItem(id)
- }
- }
|