Skip to content

Commit e5a7499

Browse files
authored
Merge pull request #265 from codex-team/json-parse-decorated-with-try-catch
fix(persistant store): json parse decorated with try catch
2 parents 5747e14 + 8c092d5 commit e5a7499

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/infrastructure/storage/abstract/persistant.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* eslint-disable-next-line boundaries/element-types */
22
import { SubscribableStore } from './subscribable';
33

4+
type StoreDataWithOptionalFields<StoreData> = {
5+
[Property in keyof StoreData]: StoreData[Property] | undefined
6+
};
7+
48
export class PersistantStore<StoreData extends Record<string, unknown>> extends SubscribableStore<StoreData> {
59
/**
610
* Keys of the StoreData type
@@ -11,7 +15,7 @@ export class PersistantStore<StoreData extends Record<string, unknown>> extends
1115
/**
1216
* Proxy for data stored
1317
*/
14-
protected data: StoreData;
18+
protected data: StoreDataWithOptionalFields<StoreData>;
1519

1620
/**
1721
* Storage that would retain information when proxy is cleared
@@ -21,20 +25,29 @@ export class PersistantStore<StoreData extends Record<string, unknown>> extends
2125
constructor(keysStored: string[]) {
2226
super();
2327
this.keysStored = keysStored;
24-
this.data = new Proxy<StoreData>({} as StoreData, this._data);
28+
this.data = new Proxy<StoreDataWithOptionalFields<StoreData>>({} as StoreDataWithOptionalFields<StoreData>, this._data);
2529

2630
/**
2731
* Load data from local store to proxy
2832
*/
2933
this.loadInitialData();
3034
};
3135

32-
protected get _data(): ProxyHandler<StoreData> {
36+
protected get _data(): ProxyHandler<StoreDataWithOptionalFields<StoreData>> {
3337
return {
3438
get: (target, prop) => {
3539
const item = this.storage.getItem(prop as string);
3640

37-
return item !== null ? JSON.parse(item) : undefined;
41+
try {
42+
return item !== null ? JSON.parse(item) : undefined;
43+
} catch {
44+
console.warn(`Persistant store: Cannot parse ${item}`);
45+
46+
/**
47+
* Delete item that cannot be parsed
48+
*/
49+
this.storage.removeItem(prop as string);
50+
}
3851
},
3952

4053
set: (target, prop, value, receiver) => {
@@ -74,7 +87,16 @@ export class PersistantStore<StoreData extends Record<string, unknown>> extends
7487
const storedValue = this.storage.getItem(key);
7588

7689
if (storedValue !== null) {
77-
this.data[key as keyof StoreData] = JSON.parse(storedValue);
90+
try {
91+
this.data[key as keyof StoreData] = JSON.parse(storedValue);
92+
} catch {
93+
console.warn(`Persistant store: Cannot parse ${storedValue}`);
94+
95+
/**
96+
* Delete item that cannot be parsed
97+
*/
98+
this.storage.removeItem(key);
99+
}
78100
}
79101
}
80102
}

src/infrastructure/storage/openedPage.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ export class OpenedPagesStore extends PersistantStore<OpenedPagesStoreData> {
2121
* @param page - page that had beed opened by user
2222
*/
2323
public addOpenedPage(page: OpenedPage): void {
24-
const uniquePageUrls = this.data.openedPages.map(currentPage => currentPage.url);
24+
if (this.data.openedPages !== undefined) {
25+
const uniquePageUrls = this.data.openedPages?.map(currentPage => currentPage.url);
2526

26-
if (!uniquePageUrls.includes(page.url) && page.url !== '/') {
27+
if (!uniquePageUrls.includes(page.url) && page.url !== '/') {
28+
this.data.openedPages = [
29+
...this.data.openedPages,
30+
page,
31+
];
32+
}
33+
} else {
2734
this.data.openedPages = [
28-
...this.data.openedPages,
2935
page,
3036
];
3137
}
@@ -36,7 +42,7 @@ export class OpenedPagesStore extends PersistantStore<OpenedPagesStoreData> {
3642
* @param url - url of closed page
3743
*/
3844
public deleteOpenedPageByUrl(url: OpenedPage['url']): void {
39-
this.data.openedPages = this.data.openedPages.filter(currentPage => !(currentPage.url == url));
45+
this.data.openedPages = this.data.openedPages?.filter(currentPage => !(currentPage.url == url));
4046
}
4147

4248
/**
@@ -46,7 +52,7 @@ export class OpenedPagesStore extends PersistantStore<OpenedPagesStoreData> {
4652
* @param page - new data for opened page with certain url
4753
*/
4854
public patchOpenedPageByUrl(url: OpenedPage['url'], page: OpenedPage): void {
49-
this.data.openedPages = this.data.openedPages.map((currentPage) => {
55+
this.data.openedPages = this.data.openedPages?.map((currentPage) => {
5056
if (currentPage.url == url) {
5157
currentPage.title = page.title;
5258
}

0 commit comments

Comments
 (0)