@@ -12,8 +12,6 @@ const log = debug('ipfs-companion:redirect-handler:blockOrObserve')
12
12
log . error = debug ( 'ipfs-companion:redirect-handler:blockOrObserve:error' )
13
13
14
14
export const DEFAULT_NAMESPACES = new Set ( [ 'ipfs' , 'ipns' ] )
15
-
16
- export const GLOBAL_STATE_CHANGE = 'GLOBAL_STATE_CHANGE'
17
15
export const GLOBAL_STATE_OPTION_CHANGE = 'GLOBAL_STATE_OPTION_CHANGE'
18
16
export const DELETE_RULE_REQUEST = 'DELETE_RULE_REQUEST'
19
17
export const DELETE_RULE_REQUEST_SUCCESS = 'DELETE_RULE_REQUEST_SUCCESS'
@@ -32,7 +30,7 @@ interface redirectHandlerInput {
32
30
getPort : ( state : CompanionState ) => string
33
31
}
34
32
35
- type messageToSelfType = typeof GLOBAL_STATE_CHANGE | typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
33
+ type messageToSelfType = typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
36
34
interface messageToSelf {
37
35
type : messageToSelfType
38
36
value ?: string | Record < string , unknown >
@@ -50,36 +48,38 @@ export const defaultNSRegexStr = `(${[...DEFAULT_NAMESPACES].join('|')})`
50
48
export const supportsBlock = ( ) : boolean => ! ( browser . declarativeNetRequest ?. MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES > 0 )
51
49
52
50
/**
53
- * Notify self about state change.
54
- * @returns void
51
+ * Sends message to self to notify about change.
52
+ *
53
+ * @param msg
55
54
*/
56
- export async function notifyStateChange ( ) : Promise < void > {
57
- return await sendMessageToSelf ( GLOBAL_STATE_CHANGE )
55
+ async function sendMessageToSelf ( msg : messageToSelfType , value ?: any ) : Promise < void > {
56
+ // this check ensures we don't send messages to ourselves if blocking mode is enabled.
57
+ if ( ! supportsBlock ( ) ) {
58
+ const message : messageToSelf = { type : msg , value }
59
+ // on FF, this call waits for the response from the listener.
60
+ // on Chrome, this needs a callback.
61
+ await browser . runtime . sendMessage ( message )
62
+ }
58
63
}
59
64
60
65
/**
61
66
* Notify self about option change.
67
+ *
62
68
* @returns void
63
69
*/
64
70
export async function notifyOptionChange ( ) : Promise < void > {
71
+ log ( 'notifyOptionChange' )
65
72
return await sendMessageToSelf ( GLOBAL_STATE_OPTION_CHANGE )
66
73
}
67
74
68
- export async function notifyDeleteRule ( id : number ) : Promise < void > {
69
- return await sendMessageToSelf ( DELETE_RULE_REQUEST , id )
70
- }
71
-
72
75
/**
73
- * Sends message to self to notify about change .
76
+ * Notify self about rule deletion .
74
77
*
75
- * @param msg
78
+ * @param id number
79
+ * @returns void
76
80
*/
77
- async function sendMessageToSelf ( msg : messageToSelfType , value ?: any ) : Promise < void > {
78
- // this check ensures we don't send messages to ourselves if blocking mode is enabled.
79
- if ( ! supportsBlock ( ) ) {
80
- const message : messageToSelf = { type : msg }
81
- await browser . runtime . sendMessage ( message )
82
- }
81
+ export async function notifyDeleteRule ( id : number ) : Promise < void > {
82
+ return await sendMessageToSelf ( DELETE_RULE_REQUEST , id )
83
83
}
84
84
85
85
const savedRegexFilters : Map < string , regexFilterMap > = new Map ( )
@@ -205,7 +205,8 @@ async function cleanupRuleById (id: number): Promise<void> {
205
205
* @param {function } handlerFn
206
206
*/
207
207
function setupListeners ( handlers : Record < messageToSelfType , ( value : any ) => Promise < void > > ) : void {
208
- browser . runtime . onMessage . addListener ( async ( { message : { type, value } } : { message : messageToSelf } ) : Promise < void > => {
208
+ browser . runtime . onMessage . addListener ( async ( message : messageToSelf ) : Promise < void > => {
209
+ const { type, value } = message
209
210
if ( type in handlers ) {
210
211
await handlers [ type ] ( value )
211
212
}
@@ -340,6 +341,23 @@ export function generateAddRule (
340
341
*/
341
342
export function addRuleToDynamicRuleSetGenerator (
342
343
getState : ( ) => CompanionState ) : ( input : redirectHandlerInput ) => Promise < void > {
344
+ // setup listeners for the extension.
345
+ setupListeners ( {
346
+ [ GLOBAL_STATE_OPTION_CHANGE ] : async ( ) : Promise < void > => {
347
+ log ( 'GLOBAL_STATE_OPTION_CHANGE' )
348
+ await cleanupRules ( true )
349
+ await reconcileRulesAndRemoveOld ( getState ( ) )
350
+ } ,
351
+ [ DELETE_RULE_REQUEST ] : async ( value : number ) : Promise < void > => {
352
+ if ( value != null ) {
353
+ await cleanupRuleById ( value )
354
+ await browser . runtime . sendMessage ( { type : DELETE_RULE_REQUEST_SUCCESS } )
355
+ } else {
356
+ await cleanupRules ( true )
357
+ }
358
+ }
359
+ } )
360
+
343
361
// returning a closure to avoid passing `getState` as an argument to `addRuleToDynamicRuleSet`.
344
362
return async function ( { originUrl, redirectUrl } : redirectHandlerInput ) : Promise < void > {
345
363
// update the rules so that the next request is handled correctly.
@@ -378,24 +396,6 @@ export function addRuleToDynamicRuleSetGenerator (
378
396
}
379
397
)
380
398
}
381
-
382
- setupListeners ( {
383
- [ GLOBAL_STATE_CHANGE ] : async ( ) : Promise < void > => {
384
- await reconcileRulesAndRemoveOld ( getState ( ) )
385
- } ,
386
- [ GLOBAL_STATE_OPTION_CHANGE ] : async ( ) : Promise < void > => {
387
- await cleanupRules ( true )
388
- await reconcileRulesAndRemoveOld ( getState ( ) )
389
- } ,
390
- [ DELETE_RULE_REQUEST ] : async ( value : number ) : Promise < void > => {
391
- if ( value != null ) {
392
- await cleanupRuleById ( value )
393
- await browser . runtime . sendMessage ( { type : DELETE_RULE_REQUEST_SUCCESS } )
394
- } else {
395
- await cleanupRules ( true )
396
- }
397
- }
398
- } )
399
399
// call to reconcile rules and remove old ones.
400
400
await reconcileRulesAndRemoveOld ( state )
401
401
}
0 commit comments