Skip to content

Commit 6021a31

Browse files
committed
Parse and cast objects from browser storage to class object
1 parent 24a235c commit 6021a31

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

src/entity/tab.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { todayLocalDate } from "../utils/common";
22

3-
export class Tab {
4-
url: string;
5-
favicon: string;
3+
interface Serializable<T> {
4+
deserialize(input: Object): T;
5+
}
6+
7+
export class Tab implements Serializable<Tab> {
8+
url: string = '';
9+
favicon: string = '';
610
summaryTime: number = 0;
711
counter: number = 0
812
days: TabDay[] = [];
913

10-
constructor(url: string, favicon: string){
14+
init(url: string, favicon: string){
1115
this.url = url;
1216
this.favicon = favicon;
1317
}
@@ -30,17 +34,29 @@ export class Tab {
3034
}
3135

3236
addNewDay() :void {
33-
const newTabDay = new TabDay(todayLocalDate());
37+
const newTabDay = new TabDay();
38+
newTabDay.init(todayLocalDate());
3439
this.days.push(newTabDay);
3540
}
41+
42+
deserialize(input: Tab) {
43+
this.url = input.url;
44+
this.counter = input.counter;
45+
this.favicon = input.favicon;
46+
this.summaryTime = input.summaryTime;
47+
if (input.days?.length > 0)
48+
this.days = input.days.map(x => new TabDay().deserialize(x));
49+
50+
return this;
51+
}
3652
}
3753

38-
export class TabDay {
39-
counter: number;
40-
date: string;
41-
summary: number;
54+
export class TabDay implements Serializable<TabDay> {
55+
counter: number = 0;
56+
date: string = '';
57+
summary: number = 0;
4258

43-
constructor(date: string){
59+
init(date: string){
4460
this.date = date;
4561
this.counter = 1;
4662
this.summary = 1;
@@ -53,4 +69,12 @@ export class TabDay {
5369
incCounter() :void {
5470
this.counter += 1;
5571
}
56-
}
72+
73+
deserialize(input: TabDay): TabDay {
74+
this.counter = input.counter;
75+
this.date = input.date;
76+
this.summary = input.summary;
77+
78+
return this;
79+
}
80+
}

src/repository/tabs-repository.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export class TabsRepository implements ITabsRepository {
4141
if (!favicon) {
4242
favicon = 'chrome://favicon/' + domain;
4343
}
44-
this.tabs.push(new Tab(domain, favicon));
44+
const newTab = new Tab();
45+
newTab.init(domain, favicon);
46+
this.tabs.push(newTab);
4547
}
4648
else {
4749
tabFromStorage.incCounter();

src/storage/local-storage.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ import Browser from 'webextension-polyfill';
55
import { isEmpty } from "../common/utility";
66

77
export class LocalStorage implements IStorage {
8-
getTabs(): Promise<Tab[]> {
8+
async getTabs(): Promise<Tab[]> {
99
return new Promise(async resolve => {
1010
const { tabs } = await Browser.storage.local.get(StorageParams.TABS);
11-
if (tabs != undefined)
12-
return resolve(tabs);
11+
if (tabs != undefined){
12+
let tempTabs: Tab[] = [];
13+
for (let i = 0; i < tabs.length; i++) {
14+
tempTabs.push(new Tab().deserialize(tabs[i]));
15+
}
16+
return resolve(tempTabs);
17+
}
1318
else resolve([]);
1419
});
1520
}
1621

17-
saveTabs(value: Tab[]): Promise<void> {
18-
return Browser.storage.local.set({ tabs: value });
22+
async saveTabs(value: Tab[]): Promise<void> {
23+
return await Browser.storage.local.set({ tabs: value });
1924
}
2025

21-
saveValue(name: StorageParams, value: any): Promise<void> {
22-
return Browser.storage.local.set({
26+
async saveValue(name: StorageParams, value: any): Promise<void> {
27+
return await Browser.storage.local.set({
2328
[name]: value
2429
});
2530
}

0 commit comments

Comments
 (0)