19
19
import {
20
20
type TsWebExtension ,
21
21
type ConfigurationMV2 as TsWebExtensionConfiguration ,
22
- type EventChannel ,
23
22
type MessageHandlerMV2 ,
23
+ EventChannel ,
24
24
createTsWebExtension ,
25
25
} from "@adguard/tswebextension" ;
26
26
27
27
import { Network } from "./network" ;
28
28
import { Storage } from "./storage" ;
29
29
import { FiltersApi , FiltersUpdateService , LocaleDetectService } from "./filters" ;
30
30
import { Configuration , configurationValidator } from "./schemas" ;
31
- import { DetectFiltersEvent , notifier , NotifierEventType } from "./notifier" ;
31
+ import { DeleteFiltersEvent , DetectFiltersEvent , notifier , NotifierEventType } from "./notifier" ;
32
32
import { RequestBlockingLogger } from "./request-blocking-logger" ;
33
33
import { Logger } from "./logger" ;
34
34
@@ -79,6 +79,11 @@ export class AdguardApi {
79
79
*/
80
80
public onAssistantCreateRule : EventChannel < string > ;
81
81
82
+ /**
83
+ * {@link TsWebExtension } {@link EventChannel }, which fires event on obsoleted filters deletion.
84
+ */
85
+ public onFiltersDeletion : EventChannel < number [ ] > ;
86
+
82
87
/**
83
88
* API for adding and removing listeners for request blocking events.
84
89
*
@@ -91,6 +96,8 @@ export class AdguardApi {
91
96
92
97
this . onAssistantCreateRule = this . tswebextension . onAssistantCreateRule ;
93
98
99
+ this . onFiltersDeletion = new EventChannel < number [ ] > ( ) ;
100
+
94
101
this . network = new Network ( ) ;
95
102
96
103
const storage = new Storage ( ) ;
@@ -106,6 +113,7 @@ export class AdguardApi {
106
113
this . openAssistant = this . openAssistant . bind ( this ) ;
107
114
this . handleDetectFilters = this . handleDetectFilters . bind ( this ) ;
108
115
this . handleUpdateFilters = this . handleUpdateFilters . bind ( this ) ;
116
+ this . handleDeleteFilters = this . handleDeleteFilters . bind ( this ) ;
109
117
}
110
118
111
119
/**
@@ -121,25 +129,28 @@ export class AdguardApi {
121
129
* Initializes AdGuard with specified {@link Configuration} and starts it immediately.
122
130
*
123
131
* @param configuration - api {@link Configuration}
124
- * @returns applied {@link Configuration} promise
132
+ *
133
+ * @returns applied {@link Configuration} promise.
125
134
*/
126
135
public async start ( configuration : Configuration ) : Promise < Configuration > {
127
136
this . configuration = configurationValidator . parse ( configuration ) ;
128
137
129
138
this . network . configure ( this . configuration ) ;
130
139
131
- await this . filtersApi . init ( ) ;
140
+ const obsoletedFiltersIds = await this . filtersApi . init ( configuration . filters ) ;
141
+ this . configuration . filters = this . configuration . filters . filter ( ( id ) => ! obsoletedFiltersIds . includes ( id ) ) ;
132
142
this . filtersUpdateService . start ( ) ;
133
143
this . localeDetectService . start ( ) ;
134
144
135
145
notifier . addListener ( NotifierEventType . UpdateFilters , this . handleUpdateFilters ) ;
136
146
notifier . addListener ( NotifierEventType . DetectFilters , this . handleDetectFilters ) ;
147
+ notifier . addListener ( NotifierEventType . DeleteFilters , this . handleDeleteFilters ) ;
137
148
138
149
const tsWebExtensionConfiguration = await this . createTsWebExtensionConfiguration ( ) ;
139
150
140
151
await this . tswebextension . start ( tsWebExtensionConfiguration ) ;
141
152
142
- return configuration ;
153
+ return this . configuration ;
143
154
}
144
155
145
156
/**
@@ -155,7 +166,8 @@ export class AdguardApi {
155
166
* Modifies AdGuard {@link Configuration}. Please note, that Adguard must be already started.
156
167
*
157
168
* @param configuration - api {@link Configuration}
158
- * @returns applied {@link Configuration} promise
169
+ *
170
+ * @returns applied {@link Configuration} promise.
159
171
*/
160
172
public async configure ( configuration : Configuration ) : Promise < Configuration > {
161
173
this . configuration = configurationValidator . parse ( configuration ) ;
@@ -166,7 +178,7 @@ export class AdguardApi {
166
178
167
179
await this . tswebextension . configure ( tsWebExtensionConfiguration ) ;
168
180
169
- return configuration ;
181
+ return this . configuration ;
170
182
}
171
183
172
184
/**
@@ -271,6 +283,23 @@ export class AdguardApi {
271
283
this . logger . info ( "Reload engine with updated filter ids list" ) ;
272
284
}
273
285
286
+ /**
287
+ * Handles fired {@link DeleteFiltersEvent}
288
+ *
289
+ * @param event - fired {@link DeleteFiltersEvent}
290
+ */
291
+ private async handleDeleteFilters ( event : DeleteFiltersEvent ) : Promise < void > {
292
+ const deletedFiltersIds = event . data . filtersIds ;
293
+
294
+ if ( deletedFiltersIds . length === 0 ) {
295
+ return ;
296
+ }
297
+
298
+ this . onFiltersDeletion . dispatch ( deletedFiltersIds ) ;
299
+
300
+ this . logger . info ( `Filters with ids ${ deletedFiltersIds } has been removed.` ) ;
301
+ }
302
+
274
303
/**
275
304
* Handles fired {@link DetectFiltersEvent}
276
305
*
0 commit comments