Skip to content

Commit 9a17910

Browse files
committed
Tab repository
1 parent ddafdd4 commit 9a17910

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/repository/inject-tabs-repository.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ import { ITabsRepository } from "./tabs-repository-interface";
22
import { TabsRepository } from "./tabs-repository";
33

44
export async function injectTabsRepository(): Promise<ITabsRepository> {
5-
return await TabsRepository.Create();
5+
const repo = new TabsRepository();
6+
await repo.initAsync();
7+
return repo;
68
}

src/repository/tabs-repository-interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import Browser from 'webextension-polyfill';
33

44
export interface ITabsRepository {
55
getTab(domain:string): Tab | undefined;
6-
addTab(tab:Browser.Tabs.Tab): Promise<Tab>;
6+
addTab(tab:Browser.Tabs.Tab): void;
77
}

src/repository/tabs-repository.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { ITabsRepository } from "./tabs-repository-interface";
2-
import { Tab } from "../storage/tab";
2+
import { Tab } from "../entity/tab";
33
import Browser from 'webextension-polyfill';
44
import { injecStorage } from "../storage/inject-storage";
55
import { isValidPage } from '../compositions/valid-page';
66
import { isInBlackList } from "../compositions/black-list";
77
import { extractHostname } from "../compositions/extract-hostname";
8+
import { addInterval, closeInterval } from "../compositions/daily-intervals";
89

910
export class TabsRepository implements ITabsRepository {
1011
private tabs: Tab[];
12+
private currentTabDomain: string | null;
1113

1214
constructor() {
1315
this.tabs = [];
16+
this.currentTabDomain = null;
1417
}
1518

1619
async initAsync(){
@@ -22,33 +25,34 @@ export class TabsRepository implements ITabsRepository {
2225
return !tab ? tab : undefined;
2326
}
2427

25-
async addTab(tab: Browser.Tabs.Tab): Promise<Tab> {
28+
async addTab(tab: Browser.Tabs.Tab): Promise<void> {
2629
if (isValidPage(tab)) {
2730
if (tab.id && (tab.id != 0)) {
28-
tabs = tabs || [];
29-
var domain = extractHostname(tab.url);
30-
var isDifferentUrl = false;
31-
if (currentTab !== tab.url) {
32-
isDifferentUrl = true;
33-
}
31+
const domain = extractHostname(tab.url);
32+
const tabFromStorage = this.getTab(domain);
33+
const isInBlackListFlag = await isInBlackList(domain);
3434

35-
if (this.isNewUrl(domain) && !await isInBlackList(domain)) {
36-
var favicon = tab.favIconUrl;
37-
if (favicon === undefined) {
38-
favicon = 'chrome://favicon/' + domain;
35+
if (!isInBlackListFlag){
36+
if (!tabFromStorage) {
37+
let favicon = tab.favIconUrl;
38+
if (!favicon) {
39+
favicon = 'chrome://favicon/' + domain;
40+
}
41+
this.tabs.push(new Tab(domain, favicon));
42+
}
43+
else {
44+
tabFromStorage.incCounter();
45+
if (this.currentTabDomain != domain) this.setCurrentActiveTab(domain);
46+
await closeInterval(this.currentTabDomain);
47+
await addInterval(this.currentTabDomain);
3948
}
40-
var newTab = new Tab(domain, favicon);
41-
tabs.push(newTab);
42-
}
43-
44-
if (isDifferentUrl && await !isInBlackList(domain)) {
45-
this.setCurrentActiveTab(domain);
46-
var tabUrl = this.getTab(domain);
47-
if (tabUrl !== undefined)
48-
tabUrl.incCounter();
49-
this.addTimeInterval(domain);
5049
}
50+
else await closeInterval(this.currentTabDomain);
5151
}
52-
} else this.closeIntervalForCurrentTab();
52+
} else await closeInterval(this.currentTabDomain);
53+
}
54+
55+
private setCurrentActiveTab(domain:string){
56+
this.currentTabDomain = domain;
5357
}
5458
}

0 commit comments

Comments
 (0)