Skip to content

Commit d7e1569

Browse files
committed
Replace controller with hook approach
1 parent 14e9e3d commit d7e1569

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

packages/snaps-controllers/src/snaps/SnapController.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ import type {
167167
ExportableKeyEncryptor,
168168
KeyDerivationOptions,
169169
} from '../types';
170-
import type { MetaMetricsController } from '../utils';
171170
import {
172171
debouncePersistState,
173172
fetchSnap,
@@ -777,9 +776,9 @@ type SnapControllerArgs = {
777776
clientCryptography?: CryptographicFunctions;
778777

779778
/**
780-
* MetaMetrics controller instance. Used for event tracking.
779+
* MetaMetrics event tracking hook.
781780
*/
782-
metaMetricsController: MetaMetricsController;
781+
trackEvent: TrackEventHook;
783782
};
784783

785784
type AddSnapArgs = {
@@ -800,6 +799,14 @@ type SetSnapArgs = Omit<AddSnapArgs, 'location' | 'versionRange'> & {
800799
hideSnapBranding?: boolean;
801800
};
802801

802+
type TrackingEventPayload = {
803+
event: string;
804+
category: string;
805+
properties: Record<string, Json>;
806+
};
807+
808+
type TrackEventHook = (event: TrackingEventPayload) => void;
809+
803810
const defaultState: SnapControllerState = {
804811
snaps: {},
805812
snapStates: {},
@@ -885,31 +892,9 @@ export class SnapController extends BaseController<
885892

886893
readonly #preinstalledSnaps: PreinstalledSnap[] | null;
887894

888-
readonly #metaMetricsController: MetaMetricsController;
895+
readonly #trackEvent: TrackEventHook;
889896

890-
readonly #trackSnapExport = throttleTracking(
891-
async (
892-
snapId: SnapId,
893-
handler: string,
894-
success: boolean,
895-
origin: string,
896-
) => {
897-
const snapMetadata = await this.getRegistryMetadata(snapId);
898-
this.#metaMetricsController.trackEvent({
899-
event: 'SnapExportUsed',
900-
category: 'Snaps',
901-
properties: {
902-
// eslint-disable-next-line @typescript-eslint/naming-convention
903-
snap_id: snapId,
904-
export: handler,
905-
// eslint-disable-next-line @typescript-eslint/naming-convention
906-
snap_category: snapMetadata?.category,
907-
success,
908-
origin,
909-
},
910-
});
911-
},
912-
);
897+
readonly #trackSnapExport: ReturnType<typeof throttleTracking>;
913898

914899
constructor({
915900
closeAllConnections,
@@ -929,7 +914,7 @@ export class SnapController extends BaseController<
929914
getMnemonicSeed,
930915
getFeatureFlags = () => ({}),
931916
clientCryptography,
932-
metaMetricsController,
917+
trackEvent,
933918
}: SnapControllerArgs) {
934919
super({
935920
messenger,
@@ -992,7 +977,7 @@ export class SnapController extends BaseController<
992977
this._onOutboundResponse = this._onOutboundResponse.bind(this);
993978
this.#rollbackSnapshots = new Map();
994979
this.#snapsRuntimeData = new Map();
995-
this.#metaMetricsController = metaMetricsController;
980+
this.#trackEvent = trackEvent;
996981

997982
this.#pollForLastRequestStatus();
998983

@@ -1058,6 +1043,30 @@ export class SnapController extends BaseController<
10581043
Object.values(this.state?.snaps ?? {}).forEach((snap) =>
10591044
this.#setupRuntime(snap.id),
10601045
);
1046+
1047+
this.#trackSnapExport = throttleTracking(
1048+
async (
1049+
snapId: SnapId,
1050+
handler: string,
1051+
success: boolean,
1052+
origin: string,
1053+
) => {
1054+
const snapMetadata = await this.getRegistryMetadata(snapId);
1055+
this.#trackEvent({
1056+
event: 'SnapExportUsed',
1057+
category: 'Snaps',
1058+
properties: {
1059+
// eslint-disable-next-line @typescript-eslint/naming-convention
1060+
snap_id: snapId,
1061+
export: handler,
1062+
// eslint-disable-next-line @typescript-eslint/naming-convention
1063+
snap_category: snapMetadata?.category ?? null,
1064+
success,
1065+
origin,
1066+
},
1067+
});
1068+
},
1069+
);
10611070
}
10621071

10631072
/**

packages/snaps-controllers/src/utils.ts

-11
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,6 @@ export function debouncePersistState(
377377
};
378378
}
379379

380-
/**
381-
* Scaled MetaMetrics controller type.
382-
*/
383-
export type MetaMetricsController = {
384-
trackEvent: (payload: {
385-
event: string;
386-
category: string;
387-
properties?: Record<string, unknown>;
388-
}) => void;
389-
};
390-
391380
/**
392381
* Handlers allowed for tracking.
393382
*/

0 commit comments

Comments
 (0)