@domisoft/todo-clean-architecture
Version:
todo project following uncle bob clean architecture patterns
43 lines • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var LocalStorageBrowserService = /** @class */ (function () {
function LocalStorageBrowserService(localStorage, keyPrefix) {
if (keyPrefix === void 0) { keyPrefix = 'todo-app'; }
this.localStorage = localStorage;
this.keyPrefix = keyPrefix;
this.cache = {};
window.addEventListener('storage', this.handleStorageEvent.bind(this));
}
LocalStorageBrowserService.prototype.getItem = function (key) {
var normalizedKey = this.normalizeKey(key);
if (normalizedKey in this.cache) {
return this.cache[normalizedKey];
}
var value = JSON.parse(this.localStorage.getItem(normalizedKey));
this.cache[normalizedKey] = value;
return value;
};
LocalStorageBrowserService.prototype.setItem = function (key, value) {
var normalizedKey = this.normalizeKey(key);
this.cache[normalizedKey] = value;
var stringifiedValue = JSON.stringify(value);
this.localStorage.setItem(normalizedKey, stringifiedValue);
};
LocalStorageBrowserService.prototype.handleStorageEvent = function (event) {
if (!event.key.startsWith(this.keyPrefix)) {
return;
}
if (event.newValue === null) {
delete this.cache[event.key];
}
else {
this.cache[event.key] = JSON.parse(event.newValue);
}
};
LocalStorageBrowserService.prototype.normalizeKey = function (key) {
return this.keyPrefix + "-" + key;
};
return LocalStorageBrowserService;
}());
exports.LocalStorageBrowserService = LocalStorageBrowserService;
//# sourceMappingURL=localstorage-browser.service.js.map