Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Decouple the data deletion controller #24870

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
BaseController,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import { ObservableStore } from '@metamask/obs-store';
import { MetaMetricsControllerState } from '../metametrics';
import type { DataDeletionService } from '../../services/data-deletion-service';

// Unique name for the controller
Expand Down Expand Up @@ -104,29 +102,29 @@ export default class MetaMetricsDataDeletionController extends BaseController<
MetaMetricsDataDeletionState,
MetaMetricsDataDeletionControllerMessenger
> {
private metaMetricsId;

#dataDeletionService: DataDeletionService;

#getMetaMetricsId: () => string | null;

/**
* Creates a MetaMetricsDataDeletionController instance.
*
* @param args - The arguments to this function.
* @param args.dataDeletionService - The service used for deleting data.
* @param args.messenger - Messenger used to communicate with BaseV2 controller.
* @param args.state - Initial state to set on this controller.
* @param args.metaMetricsStore
* @param args.getMetaMetricsId - A function that returns the current MetaMetrics ID.
*/
constructor({
dataDeletionService,
messenger,
state,
metaMetricsStore,
getMetaMetricsId,
}: {
dataDeletionService: DataDeletionService;
messenger: MetaMetricsDataDeletionControllerMessenger;
state?: MetaMetricsDataDeletionState;
metaMetricsStore: ObservableStore<MetaMetricsControllerState>;
getMetaMetricsId: () => string | null;
}) {
// Call the constructor of BaseControllerV2
super({
Expand All @@ -135,7 +133,7 @@ export default class MetaMetricsDataDeletionController extends BaseController<
name: controllerName,
state: { ...defaultState, ...state },
});
this.metaMetricsId = metaMetricsStore.getState().metaMetricsId;
this.#getMetaMetricsId = getMetaMetricsId;
this.#dataDeletionService = dataDeletionService;
}

Expand All @@ -154,13 +152,14 @@ export default class MetaMetricsDataDeletionController extends BaseController<
*
*/
async createMetaMetricsDataDeletionTask(): Promise<void> {
if (!this.metaMetricsId) {
const metaMetricsId = this.#getMetaMetricsId();
if (!metaMetricsId) {
throw new Error('MetaMetrics ID not found');
}

const { data } =
await this.#dataDeletionService.createDataDeletionRegulationTask(
this.metaMetricsId,
metaMetricsId,
);
this.update((state) => {
state.metaMetricsDataDeletionId = data?.regulateId;
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,8 @@ export default class MetamaskController extends EventEmitter {
dataDeletionService,
messenger: metaMetricsDataDeletionMessenger,
state: initState.metaMetricsDataDeletionController,
metaMetricsStore: this.metaMetricsController.store,
getMetaMetricsId: () =>
this.metaMetricsController.store.getState().metaMetricsId,
});

const gasFeeMessenger = this.controllerMessenger.getRestricted({
Expand Down