11import { ITabsRepository } from "./tabs-repository-interface" ;
2- import { Tab } from "../storage /tab" ;
2+ import { Tab } from "../entity /tab" ;
33import Browser from 'webextension-polyfill' ;
44import { injecStorage } from "../storage/inject-storage" ;
55import { isValidPage } from '../compositions/valid-page' ;
66import { isInBlackList } from "../compositions/black-list" ;
77import { extractHostname } from "../compositions/extract-hostname" ;
8+ import { addInterval , closeInterval } from "../compositions/daily-intervals" ;
89
910export 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