-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor custom `FilterEvents` integration into object returned by function. Remove usages of `hub`. Fix notification errors when changing metrics preference.
- Loading branch information
1 parent
4d299d8
commit b5bbdde
Showing
11 changed files
with
588 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,28 @@ | ||
import { | ||
Event as SentryEvent, | ||
EventProcessor, | ||
Hub, | ||
Integration, | ||
} from '@sentry/types'; | ||
import { Event as SentryEvent, Integration } from '@sentry/types'; | ||
import { logger } from '@sentry/utils'; | ||
|
||
const NAME = 'FilterEvents'; | ||
|
||
/** | ||
* Filter events when MetaMetrics is disabled. | ||
* | ||
* @param options - Options bag. | ||
* @param options.getMetaMetricsEnabled - Function that returns whether MetaMetrics is enabled. | ||
*/ | ||
export class FilterEvents implements Integration { | ||
/** | ||
* Property that holds the integration name. | ||
*/ | ||
public static id = 'FilterEvents'; | ||
|
||
/** | ||
* Another property that holds the integration name. | ||
* | ||
* I don't know why this exists, but the other Sentry integrations have it. | ||
*/ | ||
public name: string = FilterEvents.id; | ||
|
||
/** | ||
* A function that returns whether MetaMetrics is enabled. This should also | ||
* return `false` if state has not yet been initialzed. | ||
* | ||
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics | ||
* is enabled, `false` otherwise. | ||
*/ | ||
private getMetaMetricsEnabled: () => Promise<boolean>; | ||
|
||
/** | ||
* @param options - Constructor options. | ||
* @param options.getMetaMetricsEnabled - A function that returns whether | ||
* MetaMetrics is enabled. This should also return `false` if state has not | ||
* yet been initialzed. | ||
*/ | ||
constructor({ | ||
getMetaMetricsEnabled, | ||
}: { | ||
getMetaMetricsEnabled: () => Promise<boolean>; | ||
}) { | ||
this.getMetaMetricsEnabled = getMetaMetricsEnabled; | ||
} | ||
|
||
/** | ||
* Setup the integration. | ||
* | ||
* @param addGlobalEventProcessor - A function that allows adding a global | ||
* event processor. | ||
* @param getCurrentHub - A function that returns the current Sentry hub. | ||
*/ | ||
public setupOnce( | ||
addGlobalEventProcessor: (callback: EventProcessor) => void, | ||
getCurrentHub: () => Hub, | ||
): void { | ||
addGlobalEventProcessor(async (currentEvent: SentryEvent) => { | ||
// Sentry integrations use the Sentry hub to get "this" references, for | ||
// reasons I don't fully understand. | ||
// eslint-disable-next-line consistent-this | ||
const self = getCurrentHub().getIntegration(FilterEvents); | ||
if (self) { | ||
if (!(await self.getMetaMetricsEnabled())) { | ||
logger.warn(`Event dropped due to MetaMetrics setting.`); | ||
return null; | ||
} | ||
export function filterEvents({ | ||
getMetaMetricsEnabled, | ||
}: { | ||
getMetaMetricsEnabled: () => Promise<boolean>; | ||
}): Integration { | ||
return { | ||
name: NAME, | ||
processEvent: async (event: SentryEvent) => { | ||
if (!(await getMetaMetricsEnabled())) { | ||
logger.warn(`Event dropped due to MetaMetrics setting.`); | ||
return null; | ||
} | ||
return currentEvent; | ||
}); | ||
} | ||
|
||
return event; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.