Skip to content

Commit

Permalink
move try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
dew326 committed Dec 6, 2024
1 parent 84679a0 commit ae898fe
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/bundle/Resources/public/js/scripts/core/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,23 @@
this.eventName = config.eventName;
}

stringifyData(data) {
try {
return JSON.stringify(data);
} catch (error) {
console.warn('Error stringifying data', error);
}
}

parseData(data) {
try {
return JSON.parse(data);
} catch (error) {
console.warn('Error parsing data', error);

return null;
}
}

setItem(data) {
const stringifiedData = this.stringifyData(data);
const stringifiedData = JSON.stringify(data);

global.localStorage.setItem(this.key, stringifiedData);

this.fireStorageChangeEvent(stringifiedData);
}

getItem() {
return this.parseData(global.localStorage.getItem(this.key));
return JSON.parse(global.localStorage.getItem(this.key));
}

fireStorageChangeEvent(data) {
if (this.eventName) {
const storageChangeEvent = new CustomEvent(this.eventName, {
cancelable: true,
detail: { content: this.parseData(data) },
detail: { content: JSON.parse(data) },
});

doc.body.dispatchEvent(storageChangeEvent);
Expand Down

0 comments on commit ae898fe

Please sign in to comment.