@@ -106,48 +106,58 @@ class AdguardEngine implements IAdblockEngine {
106106 private config : coreConfig . Config
107107 private state : EngineState = EngineState . NOT_STARTED
108108
109+ constructor ( ) {
110+ const _this = this
111+ message . addListener ( {
112+ type : 'refresh-config' ,
113+ callback : ( msg , sender , sendResponse ) => {
114+ _this . refresh ( )
115+ sendResponse ( { } )
116+ return true
117+ } ,
118+ } )
119+
120+ window . adguardApi . onFilterDownloadSuccess . addListener ( ( ) => {
121+ store . set ( 'latest_filters_updated_at' , Date . now ( ) )
122+ } )
123+
124+ message . addListener ( {
125+ type : 'assistant-create-rule' ,
126+ async callback ( msg ) {
127+ _this . config = await coreConfig . get ( )
128+ _this . config . customRule . push ( msg . ruleText )
129+ await coreConfig . set ( _this . config )
130+ _this . refresh ( )
131+ } ,
132+ } )
133+
134+ message . addListener ( {
135+ type : 'check-filters-update' ,
136+ async callback ( msg , sender , sendResponse ) {
137+ if ( _this . state !== EngineState . NOT_STARTED ) {
138+ _this . start ( )
139+ }
140+ _this . config = await coreConfig . get ( )
141+ if ( ! _this . config . adblockActivating ) {
142+ _this . stop ( )
143+ }
144+ window . adguardApi . checkFiltersUpdates ( ( ) => {
145+ store . set ( 'latest_filters_updated_at' , Date . now ( ) )
146+ sendResponse ( 'true' )
147+ } , ( ) => {
148+ sendResponse ( 'false' )
149+ } )
150+ } ,
151+ } )
152+ }
153+
109154 public start ( ) : boolean {
110155 if ( this . state === EngineState . STARTED ) {
111156 return true
112157 } else if ( this . state === EngineState . NOT_STARTED ) {
113- message . addListener ( {
114- type : 'assistant-create-rule' ,
115- async callback ( msg ) {
116- this . config = await coreConfig . get ( )
117- this . config . customRule . push ( msg . ruleText )
118- await coreConfig . set ( this . config )
119- this . refresh ( )
120- } ,
121- } )
122-
123- message . addListener ( {
124- type : 'refresh-config' ,
125- callback : ( msg , sender , sendResponse ) => {
126- this . refresh ( )
127- sendResponse ( { } )
128- return true
129- } ,
130- } )
131-
132- message . addListener ( {
133- type : 'check-filters-update' ,
134- callback ( msg , sender , sendResponse ) {
135- window . adguardApi . checkFiltersUpdates ( ( ) => {
136- store . set ( 'latest_filters_updated_at' , Date . now ( ) )
137- sendResponse ( 'true' )
138- } , ( ) => {
139- sendResponse ( 'false' )
140- } )
141- } ,
142- } )
143-
144158 this . startTabsBlockCount ( )
145159 }
146160
147- window . adguardApi . onFilterDownloadSuccess . addListener ( ( ) => {
148- store . set ( 'latest_filters_updated_at' , Date . now ( ) )
149- } )
150-
151161 store . get ( 'firstAutoAddAllowList' ) . then ( ( isFirst ) => {
152162 if ( ! isFirst ) {
153163 autoAddallowListAds ( )
@@ -162,21 +172,28 @@ class AdguardEngine implements IAdblockEngine {
162172
163173 public async refresh ( ) : Promise < boolean > {
164174 this . config = await coreConfig . get ( )
165- if ( ! this . config . adblockActivating && this . state === EngineState . STARTED ) {
166- return this . stop ( )
175+ if ( ! this . config . adblockActivating ) {
176+ if ( this . state === EngineState . STARTED ) {
177+ return this . stop ( )
178+ } else {
179+ return true
180+ }
167181 }
182+
168183 if ( this . state === EngineState . NOT_STARTED ) {
169- return this . start ( )
184+ this . start ( )
185+ } else { // started, reconfigure adugard
186+ window . adguardApi . stop ( ( ) => {
187+ const adguardConfig = getAdguardConfig ( this . config )
188+ if ( adguardConfig . filters . length > 0 ) {
189+ window . adguardApi . start ( adguardConfig , ( ) => {
190+ logger . debug ( 'Adguard api started.' )
191+ this . customSubscription ( )
192+ } )
193+ }
194+ } )
170195 }
171- window . adguardApi . stop ( ( ) => {
172- const adguardConfig = getAdguardConfig ( this . config )
173- if ( adguardConfig . filters . length > 0 ) {
174- window . adguardApi . start ( adguardConfig , ( ) => {
175- logger . debug ( 'Adguard api started.' )
176- this . customSubscription ( )
177- } )
178- }
179- } )
196+
180197 this . state = EngineState . STARTED
181198 return true
182199 }
0 commit comments