Skip to content

Commit c18c62c

Browse files
committed
Change store for current active tab
1 parent 5be32dd commit c18c62c

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/compositions/activeTab.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export class ActiveTab {
2+
private static instance: ActiveTab;
3+
private _activeTab: string | null;
4+
5+
constructor() {
6+
if (ActiveTab.instance) {
7+
throw new Error("Error - use ActiveTab.getInstance()");
8+
}
9+
this._activeTab = null;
10+
}
11+
12+
static getInstance(): ActiveTab {
13+
ActiveTab.instance = ActiveTab.instance || new ActiveTab();
14+
return ActiveTab.instance;
15+
}
16+
17+
public setActiveTab(value:string | null): void
18+
{
19+
this._activeTab = value;
20+
}
21+
22+
public getActiveTab(): string | null
23+
{
24+
return this._activeTab;
25+
}
26+
}

src/repository/tabs-repository.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ import { isValidPage } from '../compositions/valid-page';
66
import { isInBlackList } from "../compositions/black-list";
77
import { extractHostname } from "../compositions/extract-hostname";
88
import { addInterval, closeInterval } from "../compositions/daily-intervals";
9+
import { ActiveTab } from "../compositions/activeTab"
910

1011
export class TabsRepository implements ITabsRepository {
1112
private tabs: Tab[];
12-
private currentTabDomain: string | null;
13+
private activeTab = ActiveTab.getInstance();
1314

1415
constructor() {
1516
this.tabs = [];
16-
this.currentTabDomain = null;
1717
}
1818

1919
async initAsync(){
2020
this.tabs = await injecStorage().getTabs();
2121
}
2222

2323
getTab(domain: string): Tab | undefined {
24-
const tab = this.tabs?.find(x => x.url === domain);
25-
return !tab ? tab : undefined;
24+
return this.tabs?.find(x => x.url === domain);
2625
}
2726

2827
async addTab(tab: Browser.Tabs.Tab): Promise<void> {
@@ -42,17 +41,17 @@ export class TabsRepository implements ITabsRepository {
4241
}
4342
else {
4443
tabFromStorage.incCounter();
45-
if (this.currentTabDomain != domain) this.setCurrentActiveTab(domain);
46-
await closeInterval(this.currentTabDomain);
47-
await addInterval(this.currentTabDomain);
44+
if (this.activeTab.getActiveTab() != domain) this.setCurrentActiveTab(domain);
45+
await closeInterval(this.activeTab.getActiveTab());
46+
await addInterval(this.activeTab.getActiveTab());
4847
}
4948
}
50-
else await closeInterval(this.currentTabDomain);
49+
else await closeInterval(this.activeTab.getActiveTab());
5150
}
52-
} else await closeInterval(this.currentTabDomain);
51+
} else await closeInterval(this.activeTab.getActiveTab());
5352
}
5453

5554
private setCurrentActiveTab(domain:string){
56-
this.currentTabDomain = domain;
55+
this.activeTab.setActiveTab(domain);
5756
}
5857
}

src/store/appStore.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)