@metamask-previews/profile-metrics-controller 2.0.0-preview-c72fc191 → 2.0.0-preview-1137ade3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,21 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Added
11
-
12
- - `ProfileMetricsController` contructor now accepts an optional `initialDelayDuration` parameter ([#7624](https://github.com/MetaMask/core/pull/7624))
13
- - The parameter can be used to override the default time-based delay for the first data collection after opt-in
14
- - Add `skipInitialDelay()` method to `ProfileMetricsController` ([#7624](https://github.com/MetaMask/core/pull/7624))
15
- - The method can be also called through the `ProfileMetricsController:skipInitialDelay` action via messenger
16
-
17
10
  ### Changed
18
11
 
19
- - **BREAKING:** `ProileMetricsControllerMessenger` now requires the `TransactionController:transactionSubmitted` action to be allowed ([#7624](https://github.com/MetaMask/core/pull/7624))
20
- - Set time-based delay for first `ProfileMetricsController` data collection after opt-in ([#7624](https://github.com/MetaMask/core/pull/7624))
21
12
  - Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
22
13
  - Bump `@metamask/controller-utils` from `^11.16.0` to `^11.18.0` ([#7534](https://github.com/MetaMask/core/pull/7534), [#7583](https://github.com/MetaMask/core/pull/7583))
23
- - Bump `@metamask/accounts-controller` from `^35.0.0` to `^35.0.1` ([#7604](https://github.com/MetaMask/core/pull/7604))
24
- - Bump `@metamask/polling-controller` from `^16.0.0` to `^16.0.1` ([#7604](https://github.com/MetaMask/core/pull/7604))
25
14
 
26
15
  ## [2.0.0]
27
16
 
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
13
+ var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ProfileMetricsController = exports.getDefaultProfileMetricsControllerState = exports.DEFAULT_INITIAL_DELAY_DURATION = exports.controllerName = void 0;
15
+ exports.ProfileMetricsController = exports.getDefaultProfileMetricsControllerState = exports.controllerName = void 0;
16
16
  const polling_controller_1 = require("@metamask/polling-controller");
17
17
  const async_mutex_1 = require("async-mutex");
18
18
  /**
@@ -21,10 +21,6 @@ const async_mutex_1 = require("async-mutex");
21
21
  * when composed with other controllers.
22
22
  */
23
23
  exports.controllerName = 'ProfileMetricsController';
24
- /**
25
- * The default delay duration before data is sent for the first time, in milliseconds.
26
- */
27
- exports.DEFAULT_INITIAL_DELAY_DURATION = 10 * 60 * 1000; // 10 minutes
28
24
  /**
29
25
  * The metadata for each property in {@link ProfileMetricsControllerState}.
30
26
  */
@@ -41,12 +37,6 @@ const profileMetricsControllerMetadata = {
41
37
  includeInStateLogs: true,
42
38
  usedInUi: false,
43
39
  },
44
- initialDelayEndTimestamp: {
45
- persist: true,
46
- includeInDebugSnapshot: true,
47
- includeInStateLogs: true,
48
- usedInUi: false,
49
- },
50
40
  };
51
41
  /**
52
42
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -63,7 +53,7 @@ function getDefaultProfileMetricsControllerState() {
63
53
  };
64
54
  }
65
55
  exports.getDefaultProfileMetricsControllerState = getDefaultProfileMetricsControllerState;
66
- const MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'];
56
+ const MESSENGER_EXPOSED_METHODS = [];
67
57
  class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPollingController)() {
68
58
  /**
69
59
  * Constructs a new {@link ProfileMetricsController}.
@@ -79,10 +69,8 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
79
69
  * of the user.
80
70
  * @param args.interval - The interval, in milliseconds, at which the controller will
81
71
  * attempt to send user profile data. Defaults to 10 seconds.
82
- * @param args.initialDelayDuration - The delay duration before data is sent
83
- * for the first time, in milliseconds. Defaults to 10 minutes.
84
72
  */
85
- constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval = 10 * 1000, initialDelayDuration = exports.DEFAULT_INITIAL_DELAY_DURATION, }) {
73
+ constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval = 10 * 1000, }) {
86
74
  super({
87
75
  messenger,
88
76
  metadata: profileMetricsControllerMetadata,
@@ -96,22 +84,18 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
96
84
  _ProfileMetricsController_mutex.set(this, new async_mutex_1.Mutex());
97
85
  _ProfileMetricsController_assertUserOptedIn.set(this, void 0);
98
86
  _ProfileMetricsController_getMetaMetricsId.set(this, void 0);
99
- _ProfileMetricsController_initialDelayDuration.set(this, void 0);
100
87
  __classPrivateFieldSet(this, _ProfileMetricsController_assertUserOptedIn, assertUserOptedIn, "f");
101
88
  __classPrivateFieldSet(this, _ProfileMetricsController_getMetaMetricsId, getMetaMetricsId, "f");
102
- __classPrivateFieldSet(this, _ProfileMetricsController_initialDelayDuration, initialDelayDuration, "f");
103
89
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
104
90
  this.messenger.subscribe('KeyringController:unlock', () => {
91
+ __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(console.error);
105
92
  if (__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
106
- // If the user has already opted in at the start of the session,
107
- // it must have opted in during onboarding, or during a previous session.
108
- this.skipInitialDelay();
93
+ this.startPolling(null);
109
94
  }
110
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(console.error);
111
- this.startPolling(null);
112
95
  });
113
- this.messenger.subscribe('KeyringController:lock', () => this.stopAllPolling());
114
- this.messenger.subscribe('TransactionController:transactionSubmitted', () => this.skipInitialDelay());
96
+ this.messenger.subscribe('KeyringController:lock', () => {
97
+ this.stopAllPolling();
98
+ });
115
99
  this.messenger.subscribe('AccountsController:accountAdded', (account) => {
116
100
  __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_addAccountToQueue).call(this, account).catch(console.error);
117
101
  });
@@ -120,15 +104,6 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
120
104
  });
121
105
  this.setIntervalLength(interval);
122
106
  }
123
- /**
124
- * Skip the initial delay period by setting the end timestamp to the current time.
125
- * Metrics will be sent on the next poll.
126
- */
127
- skipInitialDelay() {
128
- this.update((state) => {
129
- state.initialDelayEndTimestamp = Date.now();
130
- });
131
- }
132
107
  /**
133
108
  * Execute a single poll to sync user profile data.
134
109
  *
@@ -143,10 +118,6 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
143
118
  if (!__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
144
119
  return;
145
120
  }
146
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_setInitialDelayEndTimestampIfNull).call(this);
147
- if (!__classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_isInitialDelayComplete).call(this)) {
148
- return;
149
- }
150
121
  for (const [entropySourceId, accounts] of Object.entries(this.state.syncQueue)) {
151
122
  try {
152
123
  await this.messenger.call('ProfileMetricsService:submitMetrics', {
@@ -167,7 +138,7 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
167
138
  }
168
139
  }
169
140
  exports.ProfileMetricsController = ProfileMetricsController;
170
- _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
141
+ _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
171
142
  /**
172
143
  * Add existing accounts to the sync queue if it has not been done yet.
173
144
  *
@@ -191,19 +162,6 @@ async function _ProfileMetricsController_queueFirstSyncIfNeeded() {
191
162
  state.initialEnqueueCompleted = true;
192
163
  });
193
164
  });
194
- }, _ProfileMetricsController_setInitialDelayEndTimestampIfNull = function _ProfileMetricsController_setInitialDelayEndTimestampIfNull() {
195
- this.update((state) => {
196
- state.initialDelayEndTimestamp ?? (state.initialDelayEndTimestamp = Date.now() + __classPrivateFieldGet(this, _ProfileMetricsController_initialDelayDuration, "f"));
197
- });
198
- }, _ProfileMetricsController_isInitialDelayComplete = function _ProfileMetricsController_isInitialDelayComplete() {
199
- // The following check should never be true due to the initialization logic,
200
- // as the `initialDelayEndTimestamp` is always set in the constructor,
201
- // but is included for type safety. Ignoring for code coverage purposes.
202
- // istanbul ignore if
203
- if (this.state.initialDelayEndTimestamp === undefined) {
204
- return false;
205
- }
206
- return Date.now() >= this.state.initialDelayEndTimestamp;
207
165
  }, _ProfileMetricsController_addAccountToQueue =
208
166
  /**
209
167
  * Queue the given account to be synced at the next poll.
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.cjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgBA,qEAA+E;AAE/E,6CAAoC;AAMpC;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAEzD;;GAEG;AACU,QAAA,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,aAAa;AAwB3E;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,SAAgB,uCAAuC;IACrD,OAAO;QACL,uBAAuB,EAAE,KAAK;QAC9B,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC;AALD,0FAKC;AAED,MAAM,yBAAyB,GAAG,CAAC,kBAAkB,CAAU,CAAC;AA4DhE,MAAa,wBAAyB,SAAQ,IAAA,oDAA+B,GAI5E;IASC;;;;;;;;;;;;;;;;OAgBG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAQ,GAAG,EAAE,GAAG,IAAI,EACpB,oBAAoB,GAAG,sCAA8B,GAQtD;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YAC1C,IAAI,EAAE,sBAAc;YACpB,KAAK,E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J,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsServiceMethodActions } from '.';\nimport type { ProfileMetricsControllerMethodActions } from '.';\nimport type { AccountWithScopes } from './ProfileMetricsService';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time, in milliseconds.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = 10 * 60 * 1000; // 10 minutes\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#queueFirstSyncIfNeeded().catch(console.error);\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n // The following check should never be true due to the initialization logic,\n // as the `initialDelayEndTimestamp` is always set in the constructor,\n // but is included for type safety. Ignoring for code coverage purposes.\n // istanbul ignore if\n if (this.state.initialDelayEndTimestamp === undefined) {\n return false;\n }\n return Date.now() >= this.state.initialDelayEndTimestamp;\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
1
+ {"version":3,"file":"ProfileMetricsController.cjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgBA,qEAA+E;AAC/E,6CAAoC;AAKpC;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAoBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,SAAgB,uCAAuC;IACrD,OAAO;QACL,uBAAuB,EAAE,KAAK;QAC9B,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC;AALD,0FAKC;AAED,MAAM,yBAAyB,GAA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C,QAA2B;IAE3B,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,MAA2C,EAAE,OAAO,EAAE,EAAE;QACvD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,eAAe,IAAI,MAAM,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsServiceMethodActions } from '.';\nimport type { AccountWithScopes } from './ProfileMetricsService';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n this.#queueFirstSyncIfNeeded().catch(console.error);\n if (this.#assertUserOptedIn()) {\n this.startPolling(null);\n }\n });\n\n this.messenger.subscribe('KeyringController:lock', () => {\n this.stopAllPolling();\n });\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
@@ -3,9 +3,7 @@ import type { AccountsControllerAccountAddedEvent, AccountsControllerAccountRemo
3
3
  import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
4
4
  import type { KeyringControllerLockEvent, KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
5
5
  import type { Messenger } from "@metamask/messenger";
6
- import { TransactionControllerTransactionSubmittedEvent } from "@metamask/transaction-controller";
7
6
  import type { ProfileMetricsServiceMethodActions } from "./index.cjs";
8
- import type { ProfileMetricsControllerMethodActions } from "./index.cjs";
9
7
  import type { AccountWithScopes } from "./ProfileMetricsService.cjs";
10
8
  /**
11
9
  * The name of the {@link ProfileMetricsController}, used to namespace the
@@ -13,10 +11,6 @@ import type { AccountWithScopes } from "./ProfileMetricsService.cjs";
13
11
  * when composed with other controllers.
14
12
  */
15
13
  export declare const controllerName = "ProfileMetricsController";
16
- /**
17
- * The default delay duration before data is sent for the first time, in milliseconds.
18
- */
19
- export declare const DEFAULT_INITIAL_DELAY_DURATION: number;
20
14
  /**
21
15
  * Describes the shape of the state object for {@link ProfileMetricsController}.
22
16
  */
@@ -33,10 +27,6 @@ export type ProfileMetricsControllerState = {
33
27
  * source ID are grouped under the key "null".
34
28
  */
35
29
  syncQueue: Record<string, AccountWithScopes[]>;
36
- /**
37
- * The timestamp when the first data sending can be attempted.
38
- */
39
- initialDelayEndTimestamp?: number;
40
30
  };
41
31
  /**
42
32
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -54,7 +44,7 @@ export type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<ty
54
44
  /**
55
45
  * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.
56
46
  */
57
- export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAction | ProfileMetricsControllerMethodActions;
47
+ export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAction | ProfileMetricsServiceMethodActions;
58
48
  /**
59
49
  * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.
60
50
  */
@@ -71,7 +61,7 @@ export type ProfileMetricsControllerEvents = ProfileMetricsControllerStateChange
71
61
  * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes
72
62
  * to.
73
63
  */
74
- type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent | TransactionControllerTransactionSubmittedEvent;
64
+ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent;
75
65
  /**
76
66
  * The messenger restricted to actions and events accessed by
77
67
  * {@link ProfileMetricsController}.
@@ -79,6 +69,12 @@ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent |
79
69
  export type ProfileMetricsControllerMessenger = Messenger<typeof controllerName, ProfileMetricsControllerActions | AllowedActions, ProfileMetricsControllerEvents | AllowedEvents>;
80
70
  declare const ProfileMetricsController_base: (abstract new (...args: any[]) => {
81
71
  readonly "__#14@#intervalIds": Record<string, NodeJS.Timeout>;
72
+ /**
73
+ * The queue of accounts to be synced.
74
+ * Each key is an entropy source ID, and each value is an array of account
75
+ * addresses associated with that entropy source. Accounts with no entropy
76
+ * source ID are grouped under the key "null".
77
+ */
82
78
  "__#14@#intervalLength": number | undefined;
83
79
  setIntervalLength(intervalLength: number): void;
84
80
  getIntervalLength(): number | undefined;
@@ -108,22 +104,14 @@ export declare class ProfileMetricsController extends ProfileMetricsController_b
108
104
  * of the user.
109
105
  * @param args.interval - The interval, in milliseconds, at which the controller will
110
106
  * attempt to send user profile data. Defaults to 10 seconds.
111
- * @param args.initialDelayDuration - The delay duration before data is sent
112
- * for the first time, in milliseconds. Defaults to 10 minutes.
113
107
  */
114
- constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval, initialDelayDuration, }: {
108
+ constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval, }: {
115
109
  messenger: ProfileMetricsControllerMessenger;
116
110
  state?: Partial<ProfileMetricsControllerState>;
117
111
  interval?: number;
118
112
  assertUserOptedIn: () => boolean;
119
113
  getMetaMetricsId: () => string;
120
- initialDelayDuration?: number;
121
114
  });
122
- /**
123
- * Skip the initial delay period by setting the end timestamp to the current time.
124
- * Metrics will be sent on the next poll.
125
- */
126
- skipInitialDelay(): void;
127
115
  /**
128
116
  * Execute a single poll to sync user profile data.
129
117
  *
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.d.cts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,KAAK,EACV,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,8CAA8C,EAAE,yCAAyC;AAGlG,OAAO,KAAK,EAAE,kCAAkC,EAAE,oBAAU;AAC5D,OAAO,KAAK,EAAE,qCAAqC,EAAE,oBAAU;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAgC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,uBAAuB,EAAE,OAAO,CAAC;IACjC;;;;;OAKG;IACH,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;IAC/C;;OAEG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC,CAAC;AA0BF;;;;;;;GAOG;AACH,wBAAgB,uCAAuC,IAAI,6BAA6B,CAKvF;AAID;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,wBAAwB,CAC3E,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACvC,sCAAsC,GACtC,qCAAqC,CAAC;AAE1C;;GAEG;AACH,KAAK,cAAc,GACf,kCAAkC,GAClC,gCAAgC,CAAC;AAErC;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GACd,4BAA4B,GAC5B,0BAA0B,GAC1B,mCAAmC,GACnC,qCAAqC,GACrC,8CAA8C,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;;;;;;;;;;;;;;AAEF,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IASC;;;;;;;;;;;;;;;;OAgBG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,EACpB,oBAAqD,GACtD,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;QAC/B,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B;IAiDD;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAsIpC"}
1
+ {"version":3,"file":"ProfileMetricsController.d.cts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0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,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IAOC;;;;;;;;;;;;;;OAcG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,GACrB,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;KAChC;IAyCD;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAwGpC"}
@@ -3,9 +3,7 @@ import type { AccountsControllerAccountAddedEvent, AccountsControllerAccountRemo
3
3
  import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
4
4
  import type { KeyringControllerLockEvent, KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
5
5
  import type { Messenger } from "@metamask/messenger";
6
- import { TransactionControllerTransactionSubmittedEvent } from "@metamask/transaction-controller";
7
6
  import type { ProfileMetricsServiceMethodActions } from "./index.mjs";
8
- import type { ProfileMetricsControllerMethodActions } from "./index.mjs";
9
7
  import type { AccountWithScopes } from "./ProfileMetricsService.mjs";
10
8
  /**
11
9
  * The name of the {@link ProfileMetricsController}, used to namespace the
@@ -13,10 +11,6 @@ import type { AccountWithScopes } from "./ProfileMetricsService.mjs";
13
11
  * when composed with other controllers.
14
12
  */
15
13
  export declare const controllerName = "ProfileMetricsController";
16
- /**
17
- * The default delay duration before data is sent for the first time, in milliseconds.
18
- */
19
- export declare const DEFAULT_INITIAL_DELAY_DURATION: number;
20
14
  /**
21
15
  * Describes the shape of the state object for {@link ProfileMetricsController}.
22
16
  */
@@ -33,10 +27,6 @@ export type ProfileMetricsControllerState = {
33
27
  * source ID are grouped under the key "null".
34
28
  */
35
29
  syncQueue: Record<string, AccountWithScopes[]>;
36
- /**
37
- * The timestamp when the first data sending can be attempted.
38
- */
39
- initialDelayEndTimestamp?: number;
40
30
  };
41
31
  /**
42
32
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -54,7 +44,7 @@ export type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<ty
54
44
  /**
55
45
  * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.
56
46
  */
57
- export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAction | ProfileMetricsControllerMethodActions;
47
+ export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAction | ProfileMetricsServiceMethodActions;
58
48
  /**
59
49
  * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.
60
50
  */
@@ -71,7 +61,7 @@ export type ProfileMetricsControllerEvents = ProfileMetricsControllerStateChange
71
61
  * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes
72
62
  * to.
73
63
  */
74
- type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent | TransactionControllerTransactionSubmittedEvent;
64
+ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent;
75
65
  /**
76
66
  * The messenger restricted to actions and events accessed by
77
67
  * {@link ProfileMetricsController}.
@@ -79,6 +69,12 @@ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent |
79
69
  export type ProfileMetricsControllerMessenger = Messenger<typeof controllerName, ProfileMetricsControllerActions | AllowedActions, ProfileMetricsControllerEvents | AllowedEvents>;
80
70
  declare const ProfileMetricsController_base: (abstract new (...args: any[]) => {
81
71
  readonly "__#14@#intervalIds": Record<string, NodeJS.Timeout>;
72
+ /**
73
+ * The queue of accounts to be synced.
74
+ * Each key is an entropy source ID, and each value is an array of account
75
+ * addresses associated with that entropy source. Accounts with no entropy
76
+ * source ID are grouped under the key "null".
77
+ */
82
78
  "__#14@#intervalLength": number | undefined;
83
79
  setIntervalLength(intervalLength: number): void;
84
80
  getIntervalLength(): number | undefined;
@@ -108,22 +104,14 @@ export declare class ProfileMetricsController extends ProfileMetricsController_b
108
104
  * of the user.
109
105
  * @param args.interval - The interval, in milliseconds, at which the controller will
110
106
  * attempt to send user profile data. Defaults to 10 seconds.
111
- * @param args.initialDelayDuration - The delay duration before data is sent
112
- * for the first time, in milliseconds. Defaults to 10 minutes.
113
107
  */
114
- constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval, initialDelayDuration, }: {
108
+ constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval, }: {
115
109
  messenger: ProfileMetricsControllerMessenger;
116
110
  state?: Partial<ProfileMetricsControllerState>;
117
111
  interval?: number;
118
112
  assertUserOptedIn: () => boolean;
119
113
  getMetaMetricsId: () => string;
120
- initialDelayDuration?: number;
121
114
  });
122
- /**
123
- * Skip the initial delay period by setting the end timestamp to the current time.
124
- * Metrics will be sent on the next poll.
125
- */
126
- skipInitialDelay(): void;
127
115
  /**
128
116
  * Execute a single poll to sync user profile data.
129
117
  *
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.d.mts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,KAAK,EACV,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,8CAA8C,EAAE,yCAAyC;AAGlG,OAAO,KAAK,EAAE,kCAAkC,EAAE,oBAAU;AAC5D,OAAO,KAAK,EAAE,qCAAqC,EAAE,oBAAU;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAgC;AAEjE;;;;GAIG;AACH,eAAO,M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;AACH,MAAM,MAAM,+BAA+B,GACvC,sCAAsC,GACtC,kCAAkC,CAAC;AAEvC;;GAEG;AACH,KAAK,cAAc,GACf,kCAAkC,GAClC,gCAAgC,CAAC;AAErC;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GACd,4BAA4B,GAC5B,0BAA0B,GAC1B,mCAAmC,GACnC,qCAAqC,CAAC;AAE1C;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;IAnGA;;;;;OAKG;;;;;;;;;;;;;;AAgGL,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IAOC;;;;;;;;;;;;;;OAcG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,GACrB,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;KAChC;IAyCD;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAwGpC"}
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
12
+ var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
13
13
  import { StaticIntervalPollingController } from "@metamask/polling-controller";
14
14
  import { Mutex } from "async-mutex";
15
15
  /**
@@ -18,10 +18,6 @@ import { Mutex } from "async-mutex";
18
18
  * when composed with other controllers.
19
19
  */
20
20
  export const controllerName = 'ProfileMetricsController';
21
- /**
22
- * The default delay duration before data is sent for the first time, in milliseconds.
23
- */
24
- export const DEFAULT_INITIAL_DELAY_DURATION = 10 * 60 * 1000; // 10 minutes
25
21
  /**
26
22
  * The metadata for each property in {@link ProfileMetricsControllerState}.
27
23
  */
@@ -38,12 +34,6 @@ const profileMetricsControllerMetadata = {
38
34
  includeInStateLogs: true,
39
35
  usedInUi: false,
40
36
  },
41
- initialDelayEndTimestamp: {
42
- persist: true,
43
- includeInDebugSnapshot: true,
44
- includeInStateLogs: true,
45
- usedInUi: false,
46
- },
47
37
  };
48
38
  /**
49
39
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -59,7 +49,7 @@ export function getDefaultProfileMetricsControllerState() {
59
49
  syncQueue: {},
60
50
  };
61
51
  }
62
- const MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'];
52
+ const MESSENGER_EXPOSED_METHODS = [];
63
53
  export class ProfileMetricsController extends StaticIntervalPollingController() {
64
54
  /**
65
55
  * Constructs a new {@link ProfileMetricsController}.
@@ -75,10 +65,8 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
75
65
  * of the user.
76
66
  * @param args.interval - The interval, in milliseconds, at which the controller will
77
67
  * attempt to send user profile data. Defaults to 10 seconds.
78
- * @param args.initialDelayDuration - The delay duration before data is sent
79
- * for the first time, in milliseconds. Defaults to 10 minutes.
80
68
  */
81
- constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval = 10 * 1000, initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION, }) {
69
+ constructor({ messenger, state, assertUserOptedIn, getMetaMetricsId, interval = 10 * 1000, }) {
82
70
  super({
83
71
  messenger,
84
72
  metadata: profileMetricsControllerMetadata,
@@ -92,22 +80,18 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
92
80
  _ProfileMetricsController_mutex.set(this, new Mutex());
93
81
  _ProfileMetricsController_assertUserOptedIn.set(this, void 0);
94
82
  _ProfileMetricsController_getMetaMetricsId.set(this, void 0);
95
- _ProfileMetricsController_initialDelayDuration.set(this, void 0);
96
83
  __classPrivateFieldSet(this, _ProfileMetricsController_assertUserOptedIn, assertUserOptedIn, "f");
97
84
  __classPrivateFieldSet(this, _ProfileMetricsController_getMetaMetricsId, getMetaMetricsId, "f");
98
- __classPrivateFieldSet(this, _ProfileMetricsController_initialDelayDuration, initialDelayDuration, "f");
99
85
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
100
86
  this.messenger.subscribe('KeyringController:unlock', () => {
87
+ __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(console.error);
101
88
  if (__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
102
- // If the user has already opted in at the start of the session,
103
- // it must have opted in during onboarding, or during a previous session.
104
- this.skipInitialDelay();
89
+ this.startPolling(null);
105
90
  }
106
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(console.error);
107
- this.startPolling(null);
108
91
  });
109
- this.messenger.subscribe('KeyringController:lock', () => this.stopAllPolling());
110
- this.messenger.subscribe('TransactionController:transactionSubmitted', () => this.skipInitialDelay());
92
+ this.messenger.subscribe('KeyringController:lock', () => {
93
+ this.stopAllPolling();
94
+ });
111
95
  this.messenger.subscribe('AccountsController:accountAdded', (account) => {
112
96
  __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_addAccountToQueue).call(this, account).catch(console.error);
113
97
  });
@@ -116,15 +100,6 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
116
100
  });
117
101
  this.setIntervalLength(interval);
118
102
  }
119
- /**
120
- * Skip the initial delay period by setting the end timestamp to the current time.
121
- * Metrics will be sent on the next poll.
122
- */
123
- skipInitialDelay() {
124
- this.update((state) => {
125
- state.initialDelayEndTimestamp = Date.now();
126
- });
127
- }
128
103
  /**
129
104
  * Execute a single poll to sync user profile data.
130
105
  *
@@ -139,10 +114,6 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
139
114
  if (!__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
140
115
  return;
141
116
  }
142
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_setInitialDelayEndTimestampIfNull).call(this);
143
- if (!__classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_isInitialDelayComplete).call(this)) {
144
- return;
145
- }
146
117
  for (const [entropySourceId, accounts] of Object.entries(this.state.syncQueue)) {
147
118
  try {
148
119
  await this.messenger.call('ProfileMetricsService:submitMetrics', {
@@ -162,7 +133,7 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
162
133
  });
163
134
  }
164
135
  }
165
- _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
136
+ _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
166
137
  /**
167
138
  * Add existing accounts to the sync queue if it has not been done yet.
168
139
  *
@@ -186,19 +157,6 @@ async function _ProfileMetricsController_queueFirstSyncIfNeeded() {
186
157
  state.initialEnqueueCompleted = true;
187
158
  });
188
159
  });
189
- }, _ProfileMetricsController_setInitialDelayEndTimestampIfNull = function _ProfileMetricsController_setInitialDelayEndTimestampIfNull() {
190
- this.update((state) => {
191
- state.initialDelayEndTimestamp ?? (state.initialDelayEndTimestamp = Date.now() + __classPrivateFieldGet(this, _ProfileMetricsController_initialDelayDuration, "f"));
192
- });
193
- }, _ProfileMetricsController_isInitialDelayComplete = function _ProfileMetricsController_isInitialDelayComplete() {
194
- // The following check should never be true due to the initialization logic,
195
- // as the `initialDelayEndTimestamp` is always set in the constructor,
196
- // but is included for type safety. Ignoring for code coverage purposes.
197
- // istanbul ignore if
198
- if (this.state.initialDelayEndTimestamp === undefined) {
199
- return false;
200
- }
201
- return Date.now() >= this.state.initialDelayEndTimestamp;
202
160
  }, _ProfileMetricsController_addAccountToQueue =
203
161
  /**
204
162
  * Queue the given account to be synced at the next poll.
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.mjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAE/E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAMpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAEzD;;GAEG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CA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BAClC,OAAO,KAAK,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;gBAC1C,CAAC;gBACD,MAAM;YACR,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAGH;;;;;GAKG;AACH,SAAS,yBAAyB,CAAC,OAAwB;IACzD,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,KAAK,UAAU,EAAE,CAAC;QACjD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,8BAA8B,CACrC,QAA2B;IAE3B,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,MAA2C,EAAE,OAAO,EAAE,EAAE;QACvD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,eAAe,IAAI,MAAM,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsServiceMethodActions } from '.';\nimport type { ProfileMetricsControllerMethodActions } from '.';\nimport type { AccountWithScopes } from './ProfileMetricsService';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time, in milliseconds.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = 10 * 60 * 1000; // 10 minutes\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#queueFirstSyncIfNeeded().catch(console.error);\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n // The following check should never be true due to the initialization logic,\n // as the `initialDelayEndTimestamp` is always set in the constructor,\n // but is included for type safety. Ignoring for code coverage purposes.\n // istanbul ignore if\n if (this.state.initialDelayEndTimestamp === undefined) {\n return false;\n }\n return Date.now() >= this.state.initialDelayEndTimestamp;\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
1
+ {"version":3,"file":"ProfileMetricsController.mjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAC/E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAKpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAoBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,UAAU,uCAAuC;IACrD,OAAO;QACL,uBAAuB,EAAE,KAAK;QAC9B,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC;AAED,MAAM,yBAAyB,GAAG,EAAW,CAAC;AA2D9C,MAAM,OAAO,wBAAyB,SAAQ,+BAA+B,EAI5E;IAOC;;;;;;;;;;;;;;OAcG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAQ,GAAG,EAAE,GAAG,IAAI,GAOrB;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YA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,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsServiceMethodActions } from '.';\nimport type { AccountWithScopes } from './ProfileMetricsService';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n this.#queueFirstSyncIfNeeded().catch(console.error);\n if (this.#assertUserOptedIn()) {\n this.startPolling(null);\n }\n });\n\n this.messenger.subscribe('KeyringController:lock', () => {\n this.stopAllPolling();\n });\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,2EAGoC;AAFlC,oIAAA,wBAAwB,OAAA;AACxB,mJAAA,uCAAuC,OAAA;AAQzC,qEAA6E;AAApE,8HAAA,qBAAqB,OAAA;AAAE,oHAAA,WAAW,OAAA","sourcesContent":["export type {\n ProfileMetricsControllerActions,\n ProfileMetricsControllerEvents,\n ProfileMetricsControllerGetStateAction,\n ProfileMetricsControllerMessenger,\n ProfileMetricsControllerState,\n ProfileMetricsControllerStateChangeEvent,\n} from './ProfileMetricsController';\nexport {\n ProfileMetricsController,\n getDefaultProfileMetricsControllerState,\n} from './ProfileMetricsController';\nexport type {\n ProfileMetricsServiceActions,\n ProfileMetricsServiceEvents,\n ProfileMetricsServiceMessenger,\n ProfileMetricsSubmitMetricsRequest,\n} from './ProfileMetricsService';\nexport { ProfileMetricsService, serviceName } from './ProfileMetricsService';\nexport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\nexport type {\n ProfileMetricsControllerMethodActions,\n ProfileMetricsControllerSkipInitialDelayAction,\n} from './ProfileMetricsController-method-action-types';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,2EAGoC;AAFlC,oIAAA,wBAAwB,OAAA;AACxB,mJAAA,uCAAuC,OAAA;AAQzC,qEAA6E;AAApE,8HAAA,qBAAqB,OAAA;AAAE,oHAAA,WAAW,OAAA","sourcesContent":["export type {\n ProfileMetricsControllerActions,\n ProfileMetricsControllerEvents,\n ProfileMetricsControllerGetStateAction,\n ProfileMetricsControllerMessenger,\n ProfileMetricsControllerState,\n ProfileMetricsControllerStateChangeEvent,\n} from './ProfileMetricsController';\nexport {\n ProfileMetricsController,\n getDefaultProfileMetricsControllerState,\n} from './ProfileMetricsController';\nexport type {\n ProfileMetricsServiceActions,\n ProfileMetricsServiceEvents,\n ProfileMetricsServiceMessenger,\n ProfileMetricsSubmitMetricsRequest,\n} from './ProfileMetricsService';\nexport { ProfileMetricsService, serviceName } from './ProfileMetricsService';\nexport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\n"]}
package/dist/index.d.cts CHANGED
@@ -3,5 +3,4 @@ export { ProfileMetricsController, getDefaultProfileMetricsControllerState, } fr
3
3
  export type { ProfileMetricsServiceActions, ProfileMetricsServiceEvents, ProfileMetricsServiceMessenger, ProfileMetricsSubmitMetricsRequest, } from "./ProfileMetricsService.cjs";
4
4
  export { ProfileMetricsService, serviceName } from "./ProfileMetricsService.cjs";
5
5
  export type { ProfileMetricsServiceMethodActions } from "./ProfileMetricsService-method-action-types.cjs";
6
- export type { ProfileMetricsControllerMethodActions, ProfileMetricsControllerSkipInitialDelayAction, } from "./ProfileMetricsController-method-action-types.cjs";
7
6
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,sCAAsC,EACtC,iCAAiC,EACjC,6BAA6B,EAC7B,wCAAwC,GACzC,uCAAmC;AACpC,OAAO,EACL,wBAAwB,EACxB,uCAAuC,GACxC,uCAAmC;AACpC,YAAY,EACV,4BAA4B,EAC5B,2BAA2B,EAC3B,8BAA8B,EAC9B,kCAAkC,GACnC,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC;AAC7E,YAAY,EAAE,kCAAkC,EAAE,wDAAoD;AACtG,YAAY,EACV,qCAAqC,EACrC,8CAA8C,GAC/C,2DAAuD"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,sCAAsC,EACtC,iCAAiC,EACjC,6BAA6B,EAC7B,wCAAwC,GACzC,uCAAmC;AACpC,OAAO,EACL,wBAAwB,EACxB,uCAAuC,GACxC,uCAAmC;AACpC,YAAY,EACV,4BAA4B,EAC5B,2BAA2B,EAC3B,8BAA8B,EAC9B,kCAAkC,GACnC,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC;AAC7E,YAAY,EAAE,kCAAkC,EAAE,wDAAoD"}
package/dist/index.d.mts CHANGED
@@ -3,5 +3,4 @@ export { ProfileMetricsController, getDefaultProfileMetricsControllerState, } fr
3
3
  export type { ProfileMetricsServiceActions, ProfileMetricsServiceEvents, ProfileMetricsServiceMessenger, ProfileMetricsSubmitMetricsRequest, } from "./ProfileMetricsService.mjs";
4
4
  export { ProfileMetricsService, serviceName } from "./ProfileMetricsService.mjs";
5
5
  export type { ProfileMetricsServiceMethodActions } from "./ProfileMetricsService-method-action-types.mjs";
6
- export type { ProfileMetricsControllerMethodActions, ProfileMetricsControllerSkipInitialDelayAction, } from "./ProfileMetricsController-method-action-types.mjs";
7
6
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,sCAAsC,EACtC,iCAAiC,EACjC,6BAA6B,EAC7B,wCAAwC,GACzC,uCAAmC;AACpC,OAAO,EACL,wBAAwB,EACxB,uCAAuC,GACxC,uCAAmC;AACpC,YAAY,EACV,4BAA4B,EAC5B,2BAA2B,EAC3B,8BAA8B,EAC9B,kCAAkC,GACnC,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC;AAC7E,YAAY,EAAE,kCAAkC,EAAE,wDAAoD;AACtG,YAAY,EACV,qCAAqC,EACrC,8CAA8C,GAC/C,2DAAuD"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,sCAAsC,EACtC,iCAAiC,EACjC,6BAA6B,EAC7B,wCAAwC,GACzC,uCAAmC;AACpC,OAAO,EACL,wBAAwB,EACxB,uCAAuC,GACxC,uCAAmC;AACpC,YAAY,EACV,4BAA4B,EAC5B,2BAA2B,EAC3B,8BAA8B,EAC9B,kCAAkC,GACnC,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC;AAC7E,YAAY,EAAE,kCAAkC,EAAE,wDAAoD"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,wBAAwB,EACxB,uCAAuC,EACxC,uCAAmC;AAOpC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC","sourcesContent":["export type {\n ProfileMetricsControllerActions,\n ProfileMetricsControllerEvents,\n ProfileMetricsControllerGetStateAction,\n ProfileMetricsControllerMessenger,\n ProfileMetricsControllerState,\n ProfileMetricsControllerStateChangeEvent,\n} from './ProfileMetricsController';\nexport {\n ProfileMetricsController,\n getDefaultProfileMetricsControllerState,\n} from './ProfileMetricsController';\nexport type {\n ProfileMetricsServiceActions,\n ProfileMetricsServiceEvents,\n ProfileMetricsServiceMessenger,\n ProfileMetricsSubmitMetricsRequest,\n} from './ProfileMetricsService';\nexport { ProfileMetricsService, serviceName } from './ProfileMetricsService';\nexport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\nexport type {\n ProfileMetricsControllerMethodActions,\n ProfileMetricsControllerSkipInitialDelayAction,\n} from './ProfileMetricsController-method-action-types';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,wBAAwB,EACxB,uCAAuC,EACxC,uCAAmC;AAOpC,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,oCAAgC","sourcesContent":["export type {\n ProfileMetricsControllerActions,\n ProfileMetricsControllerEvents,\n ProfileMetricsControllerGetStateAction,\n ProfileMetricsControllerMessenger,\n ProfileMetricsControllerState,\n ProfileMetricsControllerStateChangeEvent,\n} from './ProfileMetricsController';\nexport {\n ProfileMetricsController,\n getDefaultProfileMetricsControllerState,\n} from './ProfileMetricsController';\nexport type {\n ProfileMetricsServiceActions,\n ProfileMetricsServiceEvents,\n ProfileMetricsServiceMessenger,\n ProfileMetricsSubmitMetricsRequest,\n} from './ProfileMetricsService';\nexport { ProfileMetricsService, serviceName } from './ProfileMetricsService';\nexport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/profile-metrics-controller",
3
- "version": "2.0.0-preview-c72fc191",
3
+ "version": "2.0.0-preview-1137ade3",
4
4
  "description": "Sample package to illustrate best practices for controllers",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -48,14 +48,13 @@
48
48
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
49
49
  },
50
50
  "dependencies": {
51
- "@metamask/accounts-controller": "^35.0.1",
51
+ "@metamask/accounts-controller": "^35.0.0",
52
52
  "@metamask/base-controller": "^9.0.0",
53
53
  "@metamask/controller-utils": "^11.18.0",
54
54
  "@metamask/keyring-controller": "^25.0.0",
55
55
  "@metamask/messenger": "^0.3.0",
56
- "@metamask/polling-controller": "^16.0.1",
56
+ "@metamask/polling-controller": "^16.0.0",
57
57
  "@metamask/profile-sync-controller": "^27.0.0",
58
- "@metamask/transaction-controller": "^62.9.1",
59
58
  "@metamask/utils": "^11.9.0",
60
59
  "async-mutex": "^0.5.0"
61
60
  },
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
4
- * Do not edit manually.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- //# sourceMappingURL=ProfileMetricsController-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ProfileMetricsController-method-action-types.cjs","sourceRoot":"","sources":["../src/ProfileMetricsController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ProfileMetricsController } from './ProfileMetricsController';\n\n/**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\nexport type ProfileMetricsControllerSkipInitialDelayAction = {\n type: `ProfileMetricsController:skipInitialDelay`;\n handler: ProfileMetricsController['skipInitialDelay'];\n};\n\n/**\n * Union of all ProfileMetricsController action types.\n */\nexport type ProfileMetricsControllerMethodActions =\n ProfileMetricsControllerSkipInitialDelayAction;\n"]}
@@ -1,18 +0,0 @@
1
- /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
- * Do not edit manually.
4
- */
5
- import type { ProfileMetricsController } from "./ProfileMetricsController.cjs";
6
- /**
7
- * Skip the initial delay period by setting the end timestamp to the current time.
8
- * Metrics will be sent on the next poll.
9
- */
10
- export type ProfileMetricsControllerSkipInitialDelayAction = {
11
- type: `ProfileMetricsController:skipInitialDelay`;
12
- handler: ProfileMetricsController['skipInitialDelay'];
13
- };
14
- /**
15
- * Union of all ProfileMetricsController action types.
16
- */
17
- export type ProfileMetricsControllerMethodActions = ProfileMetricsControllerSkipInitialDelayAction;
18
- //# sourceMappingURL=ProfileMetricsController-method-action-types.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ProfileMetricsController-method-action-types.d.cts","sourceRoot":"","sources":["../src/ProfileMetricsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,uCAAmC;AAE3E;;;GAGG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,wBAAwB,CAAC,kBAAkB,CAAC,CAAC;CACvD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qCAAqC,GAC/C,8CAA8C,CAAC"}
@@ -1,18 +0,0 @@
1
- /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
- * Do not edit manually.
4
- */
5
- import type { ProfileMetricsController } from "./ProfileMetricsController.mjs";
6
- /**
7
- * Skip the initial delay period by setting the end timestamp to the current time.
8
- * Metrics will be sent on the next poll.
9
- */
10
- export type ProfileMetricsControllerSkipInitialDelayAction = {
11
- type: `ProfileMetricsController:skipInitialDelay`;
12
- handler: ProfileMetricsController['skipInitialDelay'];
13
- };
14
- /**
15
- * Union of all ProfileMetricsController action types.
16
- */
17
- export type ProfileMetricsControllerMethodActions = ProfileMetricsControllerSkipInitialDelayAction;
18
- //# sourceMappingURL=ProfileMetricsController-method-action-types.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ProfileMetricsController-method-action-types.d.mts","sourceRoot":"","sources":["../src/ProfileMetricsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,uCAAmC;AAE3E;;;GAGG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,wBAAwB,CAAC,kBAAkB,CAAC,CAAC;CACvD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qCAAqC,GAC/C,8CAA8C,CAAC"}
@@ -1,6 +0,0 @@
1
- /**
2
- * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
- * Do not edit manually.
4
- */
5
- export {};
6
- //# sourceMappingURL=ProfileMetricsController-method-action-types.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ProfileMetricsController-method-action-types.mjs","sourceRoot":"","sources":["../src/ProfileMetricsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { ProfileMetricsController } from './ProfileMetricsController';\n\n/**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\nexport type ProfileMetricsControllerSkipInitialDelayAction = {\n type: `ProfileMetricsController:skipInitialDelay`;\n handler: ProfileMetricsController['skipInitialDelay'];\n};\n\n/**\n * Union of all ProfileMetricsController action types.\n */\nexport type ProfileMetricsControllerMethodActions =\n ProfileMetricsControllerSkipInitialDelayAction;\n"]}