Skip to content

Commit 957f482

Browse files
committed
add skipInitialDelay() public method and action
1 parent fdf20ce commit 957f482

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

packages/profile-metrics-controller/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- `ProfileMetricsController` contructor now accepts an optional `initialDelayDuration` parameter ([#7624](https://github.com/MetaMask/core/pull/7624))
1313
- 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
1416

1517
### Changed
1618

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
3+
* Do not edit manually.
4+
*/
5+
6+
import type { ProfileMetricsController } from './ProfileMetricsController';
7+
8+
/**
9+
* Skip the initial delay period by setting the end timestamp to the current time.
10+
*/
11+
export type ProfileMetricsControllerSkipInitialDelayAction = {
12+
type: `ProfileMetricsController:skipInitialDelay`;
13+
handler: ProfileMetricsController['skipInitialDelay'];
14+
};
15+
16+
/**
17+
* Union of all ProfileMetricsController action types.
18+
*/
19+
export type ProfileMetricsControllerMethodActions =
20+
ProfileMetricsControllerSkipInitialDelayAction;

packages/profile-metrics-controller/src/ProfileMetricsController.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,24 @@ describe('ProfileMetricsController', () => {
333333
});
334334
});
335335

336+
describe('skipInitialDelay', () => {
337+
it('sets the initial delay end timestamp to the current time', async () => {
338+
const pastTimestamp = Date.now() - 10000;
339+
await withController(
340+
{
341+
options: {
342+
state: { initialDelayEndTimestamp: pastTimestamp },
343+
},
344+
},
345+
async ({ controller }) => {
346+
controller.skipInitialDelay();
347+
348+
expect(controller.state.initialDelayEndTimestamp).toBe(Date.now());
349+
},
350+
);
351+
});
352+
});
353+
336354
describe('_executePoll', () => {
337355
describe('when the user has not opted in to profile metrics', () => {
338356
it('does not process the sync queue', async () => {

packages/profile-metrics-controller/src/ProfileMetricsController.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { TransactionControllerTransactionSubmittedEvent } from '@metamask/transa
1919
import { Mutex } from 'async-mutex';
2020

2121
import type { ProfileMetricsServiceMethodActions } from '.';
22+
import type { ProfileMetricsControllerMethodActions } from '.';
2223
import type { AccountWithScopes } from './ProfileMetricsService';
2324

2425
/**
@@ -94,7 +95,7 @@ export function getDefaultProfileMetricsControllerState(): ProfileMetricsControl
9495
};
9596
}
9697

97-
const MESSENGER_EXPOSED_METHODS = [] as const;
98+
const MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;
9899

99100
/**
100101
* Retrieves the state of the {@link ProfileMetricsController}.
@@ -109,7 +110,7 @@ export type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<
109110
*/
110111
export type ProfileMetricsControllerActions =
111112
| ProfileMetricsControllerGetStateAction
112-
| ProfileMetricsServiceMethodActions;
113+
| ProfileMetricsControllerMethodActions;
113114

114115
/**
115116
* Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.
@@ -222,7 +223,7 @@ export class ProfileMetricsController extends StaticIntervalPollingController()<
222223
if (this.#assertUserOptedIn()) {
223224
// If the user has already opted in at the start of the session,
224225
// it must have opted in during onboarding, or during a previous session.
225-
this.#skipInitialDelay();
226+
this.skipInitialDelay();
226227
}
227228
this.#queueFirstSyncIfNeeded().catch(console.error);
228229
this.startPolling(null);
@@ -233,7 +234,7 @@ export class ProfileMetricsController extends StaticIntervalPollingController()<
233234
);
234235

235236
this.messenger.subscribe('TransactionController:transactionSubmitted', () =>
236-
this.#skipInitialDelay(),
237+
this.skipInitialDelay(),
237238
);
238239

239240
this.messenger.subscribe('AccountsController:accountAdded', (account) => {
@@ -247,6 +248,16 @@ export class ProfileMetricsController extends StaticIntervalPollingController()<
247248
this.setIntervalLength(interval);
248249
}
249250

251+
/**
252+
* Skip the initial delay period by setting the end timestamp to the current time.
253+
* Metrics will be sent on the next poll.
254+
*/
255+
skipInitialDelay(): void {
256+
this.update((state) => {
257+
state.initialDelayEndTimestamp = Date.now();
258+
});
259+
}
260+
250261
/**
251262
* Execute a single poll to sync user profile data.
252263
*
@@ -328,15 +339,6 @@ export class ProfileMetricsController extends StaticIntervalPollingController()<
328339
});
329340
}
330341

331-
/**
332-
* Skip the initial delay period by setting the end timestamp to the current time.
333-
*/
334-
#skipInitialDelay(): void {
335-
this.update((state) => {
336-
state.initialDelayEndTimestamp = Date.now();
337-
});
338-
}
339-
340342
/**
341343
* Check if the initial delay end timestamp is in the past.
342344
*

packages/profile-metrics-controller/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export type {
1818
} from './ProfileMetricsService';
1919
export { ProfileMetricsService, serviceName } from './ProfileMetricsService';
2020
export type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';
21+
export type { ProfileMetricsControllerMethodActions } from './ProfileMetricsController-method-action-types';

0 commit comments

Comments
 (0)