Commit 8f42d26
## Explanation
<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:
* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->
This PR adds `ProfileMetricsController` to
`@metamask/profile-metrics-controller`.
The new controller manages a queue of addresses that need to be sent to
`ProfileMetricsService`
The queue is updated in the following scenarios:
- When the wallet is being unlocked _AND_ `firstSyncCompleted` is
`false` (i.e., first sync has not been completed yet)
- When a new account is added
- When an account is removed
The queue is processed at regular intervals (every 10 seconds by
default). At each polling interval, if there are items in the queue, the
controller will attempt to call `ProfileMetricsService` through the
messenger to sync addresses in batches, grouped by their entropy source.
Accounts with no entropy source will be batched together.
The queue is persisted to storage, so that if the app is closed and
reopened, the sync process can continue from where it left off. If one
of the batch syncs fails, the same batch will be retried in the next
interval.
## References
<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?
For example:
* Fixes #12345
* Related to #67890
-->
* Related to https://consensyssoftware.atlassian.net/browse/WPC-179
## Checklist
- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs),
highlighting breaking changes as necessary
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Adds `ProfileMetricsController` that queues accounts and periodically
submits grouped metrics via messenger, with persistence, mutexing, and
comprehensive tests.
>
> - **New Controller**: `src/ProfileMetricsController.ts`
> - Polling via `StaticIntervalPollingController` (default 10s) to call
`ProfileMetricsService:submitMetrics`.
> - Maintains persistent `syncQueue` grouped by entropy source (`null`
for none); guarded by `async-mutex`.
> - Subscribes to `KeyringController:unlock/lock` and
`AccountsController:accountAdded/accountRemoved` to enqueue/dequeue;
initial enqueue on unlock if not completed.
> - Batches submissions per entropy source; logs and skips failed
batches while continuing others.
> - Exposes state with metadata (`initialEnqueueCompleted`,
`syncQueue`).
> - **Tests**: `src/ProfileMetricsController.test.ts` covering
subscriptions, polling behavior, batching, failure handling, and state
metadata.
> - **Exports**: Update `src/index.ts` to export controller types and
helpers.
> - **Project Setup**:
> - Add dependencies: `@metamask/accounts-controller`,
`@metamask/keyring-controller`, `@metamask/polling-controller`,
`async-mutex` (and dev `@metamask/keyring-internal-api`).
> - Update TS references in `tsconfig.json` and `tsconfig.build.json`.
> - Update `CHANGELOG.md` entry for initial release.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
bc80feb. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Daniel Rocha <[email protected]>
1 parent a779d4d commit 8f42d26
File tree
8 files changed
+984
-2
lines changed- packages/profile-metrics-controller
- src
8 files changed
+984
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
53 | 55 | | |
| 56 | + | |
54 | 57 | | |
55 | | - | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | 61 | | |
58 | 62 | | |
| 63 | + | |
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
| |||
0 commit comments