UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

39 lines 1.52 kB
import { DependencyContainer } from "../DependencyContainer"; import { Api } from "../Api"; export class LocalStorageService { GetServiceName() { return LocalStorageService.ServiceName; } GetResourceName(inContextOf, objectId) { return `_exalus_${Api.WorksInContextOf}_${inContextOf}_${objectId}`; } Remove(inContextOf, objectId) { localStorage.removeItem(this.GetResourceName(inContextOf, objectId)); } DropStorage(inContextOf) { let prefix = `_exalus_${inContextOf}`; for (const key in localStorage) { if (key === null || key === void 0 ? void 0 : key.startsWith(prefix)) localStorage.removeItem(key); } } Save(inContextOf, objectId, data) { localStorage.setItem(this.GetResourceName(inContextOf, objectId), JSON.stringify(data)); } Read(inContextOf, objectId) { var _a; try { let name = this.GetResourceName(inContextOf, objectId); let obj = localStorage.getItem(name); if (obj == null) return null; return JSON.parse(obj); } catch (ex) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(LocalStorageService.ServiceName, `Exception has occurred while reading ${this.GetResourceName(inContextOf, objectId)}`); return null; } } } LocalStorageService.ServiceName = "LocalStorageService"; //# sourceMappingURL=LocalStorageService.js.map